-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdeps.go
141 lines (113 loc) · 3.57 KB
/
deps.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
package cmd
import (
"fmt"
"os"
"os/exec"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// depsCmd represents the deps command
var depsCmd = &cobra.Command{
Use: "deps",
Short: "Download and optionally update all abcweb dependencies",
Long: `Download and optionally update all abcweb dependencies used by
your generated app or the abcweb tool by executing "go get" commands`,
Example: "abcweb deps -u",
// Needs to be a persistentPreRunE to override root's config.Initialize call
// otherwise abcweb needs to be run from the abcweb project or the git rev-parse
// will cause a fatal error.
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
viper.BindPFlags(cmd.Flags())
return nil
},
RunE: depsCmdRun,
}
func init() {
depsCmd.Flags().BoolP("update", "u", false, "Also update already installed dependencies")
depsCmd.Flags().BoolP("verbose", "v", false, "Very noisy verbose output")
depsCmd.Flags().BoolP("assets-only", "a", false, "Only install gulp.js dependencies")
depsCmd.Flags().BoolP("go-only", "g", false, "Only install Go dependencies")
RootCmd.AddCommand(depsCmd)
}
func depsCmdRun(cmd *cobra.Command, args []string) error {
var err error
goGetArgs := [][]string{
{"github.com/google/wire/cmd/wire"},
}
npmInstallArgs := [][]string{
{"gulp@v4"},
}
prependArgs := []string{"get"}
if viper.GetBool("update") {
prependArgs = append(prependArgs, "-u")
}
if viper.GetBool("verbose") {
prependArgs = append(prependArgs, "-v")
}
for i := 0; i < len(goGetArgs); i++ {
goGetArgs[i] = append(prependArgs, goGetArgs[i]...)
}
if !viper.GetBool("go-only") {
prependArgs = []string{"install", "--global"}
if viper.GetBool("verbose") {
prependArgs = append(prependArgs, "--verbose")
}
for i := 0; i < len(npmInstallArgs); i++ {
npmInstallArgs[i] = append(prependArgs, npmInstallArgs[i]...)
}
fmt.Printf("Retrieving all Nodejs dependencies using \"npm install --global\":\n\n")
_, err = exec.LookPath("npm")
if err != nil {
fmt.Printf(`Error: npm could not be found in your $PATH. If you have not already installed nodejs
and npm you must do so before proceeding. Please follow the instructions at:
https://docs.npmjs.com/getting-started/installing-node
If you receive permission related errors, please apply the following fix:
https://docs.npmjs.com/getting-started/fixing-npm-permissions
`)
os.Exit(1)
}
for _, npmInstallArg := range npmInstallArgs {
fmt.Printf("%s ... ", npmInstallArg[len(npmInstallArg)-1])
exc := exec.Command("npm", npmInstallArg...)
out, err := exc.CombinedOutput()
if err != nil {
fmt.Printf("ERROR\n\n")
} else {
fmt.Printf("SUCCESS\n\n")
}
if len(out) > 0 {
fmt.Print(string(out))
}
if err != nil {
fmt.Printf("%s\n\n", err)
fmt.Printf(`Note: If you are receiving a permission related exit status or error, please apply the following fix:
https://docs.npmjs.com/getting-started/fixing-npm-permissions
`)
os.Exit(1)
}
}
}
if !viper.GetBool("assets-only") {
fmt.Printf("\nRetrieving all Go dependencies using \"go get\":\n\n")
for _, goGetArg := range goGetArgs {
fmt.Printf("%s ... ", goGetArg[len(goGetArg)-1])
exc := exec.Command("go", goGetArg...)
exc.Env = append(os.Environ(), "GO111MODULE=off")
out, err := exc.CombinedOutput()
if err != nil {
fmt.Printf("ERROR\n\n")
} else {
fmt.Printf("SUCCESS\n")
}
if len(out) > 0 {
fmt.Print(string(out))
}
if err != nil {
fmt.Printf("%s\n\n", err)
os.Exit(1)
}
}
}
fmt.Printf("\nAll dependencies successfully installed.\n\n")
return err
}