-
Notifications
You must be signed in to change notification settings - Fork 1
/
status_command.go
115 lines (95 loc) · 2.45 KB
/
status_command.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 command
import (
"fmt"
"log"
"path/filepath"
"github.com/codegangsta/cli"
. "github.com/foostan/fileconsul/fileconsul"
)
var StatusFlags = []cli.Flag{
cli.StringFlag{
Name: "addr",
Value: "localhost:8500",
Usage: "consul HTTP API address with port",
},
cli.StringFlag{
Name: "dc",
Value: "dc1",
Usage: "consul datacenter, uses local if blank",
},
cli.StringFlag{
Name: "prefix",
Value: "fileconsul",
Usage: "reading file status from Consul's K/V store with the given prefix",
},
cli.StringFlag{
Name: "basepath",
Value: ".",
Usage: "base directory path of target files",
},
}
func StatusCommand(c *cli.Context) {
args := c.Args()
pattern := args.First()
addr := c.String("addr")
dc := c.String("dc")
prefix := c.String("prefix")
basepath := c.String("basepath")
client, err := NewClient(&ClientConfig{
ConsulAddr: addr,
ConsulDC: dc,
})
if err != nil {
log.Fatal(err)
}
lfList, err := ReadLFList(basepath)
if err != nil {
log.Fatal(err)
}
rfList, err := client.ReadRFList(prefix)
if err != nil {
log.Fatal(err)
}
lfrfList := lfList.ToRFList(prefix)
if pattern != "" {
rfList, err = rfList.Filter(pattern)
if err != nil {
log.Fatal(err)
}
lfrfList, err = lfrfList.Filter(pattern)
if err != nil {
log.Fatal(err)
}
}
rfDiff := lfrfList.Diff(rfList)
switch {
case lfrfList.Equal(rfList):
default:
fmt.Println("Changes to be pushed:\n (use \"fileconsul push [command options]\" to synchronize local files)")
for _, remotefile := range rfDiff.Add {
println("\tadd remote file:\t" + filepath.Join(basepath, remotefile.Path))
}
for _, remotefile := range rfDiff.Del {
println("\tdelete remote file:\t" + filepath.Join(basepath, remotefile.Path))
}
for _, remotefile := range rfDiff.New {
println("\tmodify remote file:\t" + filepath.Join(basepath, remotefile.Path))
}
}
rfDiff = rfList.Diff(lfrfList)
switch {
case lfrfList.Equal(rfList):
case rfList.Empty():
default:
fmt.Println("Changes to be pulled:\n (use \"fileconsul pull [command options]\" to synchronize remote files)")
for _, remotefile := range rfDiff.Add {
println("\tadd local file:\t" + filepath.Join(basepath, remotefile.Path))
}
for _, remotefile := range rfDiff.Del {
println("\tdelete local file:\t" + filepath.Join(basepath, remotefile.Path))
}
for _, remotefile := range rfDiff.New {
println("\tmodify local file:\t" + filepath.Join(basepath, remotefile.Path))
}
}
}