-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.go
110 lines (94 loc) · 2.59 KB
/
build.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
package main
import (
"os"
"os/signal"
"github.com/Sirupsen/logrus"
"github.com/asteris-llc/hammer/hammer"
"github.com/asteris-llc/hammer/hammer/cache"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/net/context"
)
var (
buildCmd = &cobra.Command{
Use: "build [package...]",
Short: "build packages",
Long: "build all packages by default, unless specific packages are specified",
Run: func(cmd *cobra.Command, packageNames []string) {
loader := hammer.NewLoader(viper.GetString("search"))
loaded, err := loader.Load()
if err != nil {
logrus.WithField("error", err).Fatal("could not load packages")
}
// find packages specified in command line arguments
var packages []*hammer.Package
if len(packageNames) == 0 {
packages = loaded
} else {
packages = []*hammer.Package{}
for _, name := range packageNames {
found := false
for _, pkg := range loaded {
if pkg.Name == name {
packages = append(packages, pkg)
found = true
break
}
}
if !found {
logrus.WithField("name", name).Warn("could not find package")
}
}
}
if len(packages) == 0 {
logrus.Fatal("no packages selected")
}
// mark a single package to stream logs
if name := viper.GetString("stream-logs-for"); name != "" {
for _, pkg := range loaded {
if pkg.Name == name {
pkg.StreamLogs = true
}
}
}
// set up cache
fsCache, err := cache.NewFSCache(viper.GetString("cache"))
if err != nil {
logrus.WithField("error", err).Fatal("could not make cache")
}
for _, pkg := range packages {
pkg.SetCache(fsCache)
}
// handle interrupts so we can clean up nicely
ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for {
select {
case <-c:
logrus.Warn("interrupted, exiting cleanly")
cancel()
case <-ctx.Done():
return
}
}
}()
// start packaging
packager := hammer.NewPackager(packages)
// create directories needed in the packaging process
err = packager.EnsureOutputDir(viper.GetString("output"))
if err != nil {
logrus.WithField("error", err).Fatal("could not create output directory")
}
err = packager.EnsureOutputDir(viper.GetString("logs"))
if err != nil {
logrus.WithError(err).Fatal("could not create logs directory")
}
// build the packages!
if !packager.Build(ctx, viper.GetInt("concurrent-jobs")) { // Errors are already reported to the user from here
os.Exit(1)
}
},
}
)