-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
115 lines (96 loc) · 2.33 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/ipfn/ipfs-sync/shell"
"github.com/ipfn/ipfs-sync/sync"
)
var (
verbose = flag.Bool("verbose", false, "Print logs to stderr")
ipnsKey = flag.String("ipns-key", "", "IPNS publish key or name")
// Ignore .git and .gitignore files by default
git = flag.Bool("git", true, "Ignores files from .gitignore and .git directory itself")
// IPFS ignore rules
ignore stringList
ignoreRulesPath = flag.String("ignore-rules-path", "", "Ignores files from .gitignore")
hidden = flag.Bool("hidden", false, "Include files that are hidden.")
)
func init() {
flag.Var(&ignore, "ignore", "List of paths to ignore")
}
func fatal(msg string, v ...interface{}) {
fmt.Printf(msg, v...)
os.Exit(1)
}
func main() {
flag.Parse()
if *verbose {
log.SetOutput(os.Stderr)
} else {
log.SetOutput(io.Discard)
}
path := flag.Arg(0)
if path == "" {
fatal("Usage: ipfs-sync <directory>")
}
if path == "." {
var err error
if path, err = os.Getwd(); err != nil {
fatal("Error: getwd: %v", err)
}
} else {
var err error
if path, err = filepath.Abs(path); err != nil {
fatal("Error: filepath: %v", err)
}
}
log.Printf("Starting in %s", path)
if _, err := exec.LookPath("ipfs"); err != nil {
fatal("Error: ipfs was not found in $PATH")
}
_, err := os.Stat(".gitignore")
if *git && !os.IsNotExist(err) {
// Since --git is true by default we only respect --ignore-rules-path flag.
if len(*ignoreRulesPath) == 0 {
*ignoreRulesPath = ".gitignore"
}
ignore = append(ignore, ".git")
}
snc, err := sync.Watch(path, shell.AddOptions{
IgnorePaths: ignore,
IgnoreRulesPath: *ignoreRulesPath,
})
if err != nil {
fatal("Error: watch: %v", err)
}
fmt.Println(snc.Hash())
var pubChan chan string
if *ipnsKey != "" {
pubChan = make(chan string, 1)
if err := shell.Publish(*ipnsKey, pubChan); err != nil {
fatal("Publish error: %s\n", err)
}
log.Printf("Publishing to key: %s", *ipnsKey)
pubChan <- snc.Hash()
}
for hash := range snc.Events() {
fmt.Println(hash)
if pubChan != nil {
pubChan <- hash
}
}
}
type stringList []string
func (i *stringList) String() string {
return strings.Join(*i, ", ")
}
func (i *stringList) Set(value string) error {
*i = append(*i, value)
return nil
}