-
Notifications
You must be signed in to change notification settings - Fork 3
/
commands.go
174 lines (142 loc) · 3.16 KB
/
commands.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"gopkg.in/yaml.v2"
"github.com/codegangsta/cli"
)
// save all dependency to file
func snapshotAction(c *cli.Context) error {
file := c.GlobalString("file")
file, err := filepath.Abs(file)
must(err)
bp := c.GlobalString("path")
verbose := c.GlobalBool("verbose")
snapshot(file, bp, true, verbose)
return nil
}
func snapshot(file, base string, force, verbose bool) (map[string]*Dependency, error) {
srlt := &Srlt{
Deps: make(map[string]*Dependency),
Base: base,
verbose: verbose,
}
// read the file
if buffer, err := ioutil.ReadFile(file); err == nil {
yaml.Unmarshal(buffer, &srlt)
} else if srlt.verbose {
log("creating file %s ...\n", file)
}
bp, err := filepath.Abs(srlt.Base)
if err != nil {
return srlt.Deps, err
}
bp += string(os.PathSeparator)
if srlt.verbose {
log("lookup at %s...\n", bp)
}
err = filepath.Walk(bp, func(p string, f os.FileInfo, err error) error {
dep := &Dependency{
path: p,
base: bp,
verbose: srlt.verbose,
}
if err := dep.parse(); err == nil {
if _, exist := srlt.Deps[dep.Name]; !exist && force {
srlt.Deps[dep.Name] = dep
}
}
return nil
})
if err != nil {
return srlt.Deps, err
}
buffer, _ := yaml.Marshal(srlt)
// save to the file
err = ioutil.WriteFile(file, buffer, 0644)
if err != nil {
return srlt.Deps, err
}
return srlt.Deps, nil
}
func restoreAction(c *cli.Context) error {
file := c.GlobalString("file")
file, err := filepath.Abs(file)
must(err)
verbose := c.GlobalBool("verbose")
restore(file, verbose)
return nil
}
func restore(file string, verbose bool) error {
srlt := &Srlt{verbose: verbose}
// read the file
if buffer, err := ioutil.ReadFile(file); err == nil {
err = yaml.Unmarshal(buffer, srlt)
if err != nil {
return err
}
} else {
return fmt.Errorf("file %s not found\n", file)
}
var wg sync.WaitGroup
for _, dep := range srlt.Deps {
dep.verbose = srlt.verbose
dep.base = srlt.Base
wg.Add(1)
go func(dep *Dependency) {
defer wg.Done()
if err := dep.Install(); err != nil {
panic(fmt.Sprintf("error: %v\ndep: %#v", err, dep))
}
}(dep)
}
wg.Wait()
return nil
}
func execAction(c *cli.Context) error {
file := c.GlobalString("file")
file, err := filepath.Abs(file)
must(err)
verbose := c.GlobalBool("verbose")
tmpl := strings.Join(c.Args(), " ")
execCommand(file, tmpl, verbose)
return nil
}
func execCommand(file, template string, verbose bool) {
srlt := &Srlt{verbose: verbose}
// read the file
if buffer, err := ioutil.ReadFile(file); err == nil {
yaml.Unmarshal(buffer, srlt)
// } else {
// return fmt.Errorf("file %s not found\n", file)
}
bp, err := filepath.Abs(srlt.Base)
must(err)
bp += string(os.PathSeparator)
var wg sync.WaitGroup
for _, dep := range srlt.Deps {
dep.base = bp
dep.verbose = srlt.verbose
wg.Add(1)
go func(dep *Dependency) {
defer wg.Done()
if err := dep.Exec(template); err != nil {
panic(err)
}
}(dep)
}
wg.Wait()
}
// utils
func log(format string, i ...interface{}) {
fmt.Printf(format, i...)
}
func must(err error) {
if err != nil {
panic(err)
}
}