-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
194 lines (172 loc) · 5.04 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"os/signal"
"path"
"runtime"
"strconv"
"strings"
)
const tagged = true
const version = "1.1.0"
var cmdLineArgs struct {
help bool
quiet bool
verbose bool
colour bool
reproduce bool
forceCopy bool
primaryManifest string
pinnedManifest string
}
func main() {
err := body()
if err != nil {
LogError("%v", err)
os.Exit(1)
}
}
func body() error {
// Set up command line args.
flag.BoolVar(&cmdLineArgs.help, "help", false, "show usage")
flag.BoolVar(&cmdLineArgs.quiet, "quiet", false, "don't output to stdout")
flag.BoolVar(&cmdLineArgs.verbose, "verbose", false, "output debug information")
flag.BoolVar(&cmdLineArgs.colour, "colour", runtime.GOOS != "windows", "use ANSI colour escape codes")
flag.BoolVar(&cmdLineArgs.reproduce, "reproduce", false, "read from the pinned manifest instead of the primary manifest")
flag.BoolVar(&cmdLineArgs.forceCopy, "force-copy", false, "force copying dependency even if unchanged/identical")
flag.StringVar(&cmdLineArgs.primaryManifest, "primary-manifest", "deps.json", "location of the primary manifest")
flag.StringVar(&cmdLineArgs.pinnedManifest, "pinned-manifest", "pins.json", "location of the pinned manifest")
flag.Parse()
// Show help.
if cmdLineArgs.help {
fmt.Printf("Courier %s\nUsage:\n", version)
flag.PrintDefaults()
return nil
}
// Set up logging.
if err := SetupLogging(cmdLineArgs.quiet, cmdLineArgs.verbose); err != nil {
return err
}
// Show version.
if tagged {
LogInfo("Courier version %s", version)
} else {
LogWarn("\u001b[41mCourier version %s\u001b[0m", version)
}
listenForCtrlC()
// Determine which manifest to read from.
var manifestFile string
if cmdLineArgs.reproduce {
manifestFile = cmdLineArgs.pinnedManifest
} else {
manifestFile = cmdLineArgs.primaryManifest
}
// Get the manifest.
LogInfo("Using manifest %q", manifestFile)
buf, err := ioutil.ReadFile(manifestFile)
if err != nil {
return err
}
m, err := LoadManifest(buf)
if err != nil {
enrErr := enrichJSONError(err, string(buf))
return enrErr
}
// Stage the dependencies.
stagedDeps, err := StageDependencies(m)
if err != nil {
return err
}
// Clean up the staged dependencies when we return.
defer func() {
for _, stagedDep := range stagedDeps {
_ = os.RemoveAll(stagedDep.StagingDir) // If we can't remove... then there's not much we can do.
}
}()
// Copy the dependencies.
for dir, stagedDep := range stagedDeps {
src := path.Join(stagedDep.StagingDir, stagedDep.Pinned.DirToCopy())
if cmdLineArgs.forceCopy {
LogInfo(`Copying dependency %q (forced)`, dir)
if err := CopyDirContents(src, dir, stagedDep.Pinned.IgnoreDir()); err != nil {
return err
}
} else {
// Calculate source and destination hashes; skip copying if equal
srcHash, err := CreateDirHash(src, stagedDep.Pinned.IgnoreDir())
if err != nil {
return err
}
dstHash, err := CreateDirHash(dir, stagedDep.Pinned.IgnoreDir())
if err != nil && !os.IsNotExist(err) {
return err
}
equalDirs := bytes.Equal(srcHash, dstHash)
if equalDirs {
LogInfo(`Skipping copying dependency %q (unchanged)`, dir)
} else {
LogDebug(`Src hash: %x, Dst hash: %x`, srcHash, dstHash)
LogInfo(`Copying dependency %q`, dir)
if err := CopyDirContents(src, dir, stagedDep.Pinned.IgnoreDir()); err != nil {
return err
}
}
}
}
// Save the pinned manifest to file.
if !cmdLineArgs.reproduce {
LogInfo("Saving pinned manifest to %q", cmdLineArgs.pinnedManifest)
var pinned Manifest = make(map[string]Dependency)
for dir, stagedDep := range stagedDeps {
pinned[dir] = stagedDep.Pinned
}
if raw, err := json.MarshalIndent(pinned, "", "\t"); err != nil {
return err
} else if err := ioutil.WriteFile(cmdLineArgs.pinnedManifest, append(raw, '\n'), 0644); err != nil {
return err
}
}
LogInfo("Finished!")
return nil
}
func listenForCtrlC() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, os.Kill)
go func() {
sig := <-sigCh
LogInfo("Received signal %s, panicking now!", sig)
panic(fmt.Sprintf("%s", sig))
}()
}
func enrichJSONError(err error, js string) error {
if s, ok := err.(*json.SyntaxError); ok {
line, pos, desc := findLineAndPos(s, js)
return fmt.Errorf("%v\nOccurred on line %v at pos %v: %v", err, line, pos, desc)
} else {
return err
}
}
func findLineAndPos(s *json.SyntaxError, js string) (line int, pos int, desc string) {
if s.Offset < 0 || s.Offset > int64(len(js)) {
return -1, -1, "Offset " + strconv.FormatInt(s.Offset, 10) + " is out of bounds."
}
if s.Offset == 0 {
return 0, 0, "Offset 0: Empty file"
}
// Syntax error: calculate line number
start, end := strings.LastIndex(js[:s.Offset], "\n")+1, len(js)
if idx := strings.Index(js[start:], "\n"); idx >= 0 {
end = start + idx
}
line, pos = strings.Count(js[:start], "\n")+1, int(s.Offset)-start-1
if pos < 0 {
pos = 0
}
// Return line that has error
return line, pos, strings.Trim(js[start:end], " \t")
}