-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
212 lines (193 loc) · 5.25 KB
/
main.go
File metadata and controls
212 lines (193 loc) · 5.25 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"fmt"
"os"
"regexp"
"time"
"github.com/macadmins/carafe/brew"
"github.com/macadmins/carafe/exec"
"github.com/spf13/cobra"
)
var formulaRe = regexp.MustCompile(`^[A-Za-z0-9+@._-]{1,128}$`) //nolint:gochecknoglobals
var version = "dev" //nolint:gochecknoglobals
func validateFormulaArg(arg string) error {
if !formulaRe.MatchString(arg) {
return fmt.Errorf("invalid formula name")
}
return nil
}
func completionCommand() *cobra.Command {
return &cobra.Command{
Use: "completion",
Short: "Generate the autocompletion script for the specified shell",
}
}
func buildRootCmd(c exec.CarafeConfig, version string) *cobra.Command {
rootCmd := &cobra.Command{
Use: "carafe",
Short: "A CLI tool for managing homebrew packages",
}
var cleanupCmd = &cobra.Command{
Use: "cleanup [package]",
Short: "Cleanup the desired package",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := validateFormulaArg(args[0]); err != nil {
return err
}
return brew.Cleanup(c, args[0])
},
}
var installCmd = &cobra.Command{
Use: "install [package]",
Short: "Install the desired package",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := validateFormulaArg(args[0]); err != nil {
return err
}
return brew.Install(c, args[0])
},
}
var uninstallCmd = &cobra.Command{
Use: "uninstall [package]",
Short: "Uninstall the desired package",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := validateFormulaArg(args[0]); err != nil {
return err
}
return brew.Uninstall(c, args[0])
},
}
var tapCmd = &cobra.Command{
Use: "tap [tapname]",
Short: "Add the desired tap",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return brew.Tap(c, args[0])
},
}
var untapCmd = &cobra.Command{
Use: "untap [tapname]",
Short: "Remove the desired tap",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return brew.Untap(c, args[0])
},
}
var infoCmd = &cobra.Command{
Use: "info [package]",
Short: "List information about installed packages or a specific package",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return brew.AllInfo(c)
} else {
if err := validateFormulaArg(args[0]); err != nil {
return err
}
return brew.Info(c, args[0])
}
},
}
var minVersion string
var upgradeCmd = &cobra.Command{
Use: "upgrade [package]",
Short: "Upgrade the package if its version is less than the specified minimum version",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := validateFormulaArg(args[0]); err != nil {
return err
}
if minVersion != "" {
return brew.EnsureMinimumVersion(c, args[0], minVersion)
}
return brew.Upgrade(c, args[0])
},
}
upgradeCmd.Flags().StringVar(&minVersion, "min-version", "", "Minimum version to update the package to")
// check command
var munkiInstallCheck bool
var skipNotInstalled bool
var noCache bool
var cacheTTL time.Duration
var checkCmd = &cobra.Command{
Use: "check [package]",
Short: "Check if the package is installed, and optionally at or above a specific version. Use --min-version to specify a minimum version. Use --munki-installcheck to reverse the exit codes.", //nolint:lll
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := validateFormulaArg(args[0]); err != nil {
return err
}
if noCache {
cacheTTL = 0
}
exitCode, err := brew.Check(c, args[0], minVersion, munkiInstallCheck, skipNotInstalled, cacheTTL)
if err != nil {
return err
}
os.Exit(exitCode)
return nil
},
}
checkCmd.Flags().StringVar(&minVersion, "min-version", "", "Minimum version to check the package against")
checkCmd.Flags().BoolVar(
&munkiInstallCheck,
"munki-installcheck",
false,
"Flag for munki installcheck which reverses the exit codes",
)
checkCmd.Flags().BoolVar(
&skipNotInstalled,
"skip-not-installed",
false,
"Exits with success if the package is not installed. Must be used with --min-version flag. For use when checking to upgrade for security reasons", //nolint:lll
)
checkCmd.Flags().BoolVar(
&noCache,
"no-cache",
false,
"Disable the brew info cache and call brew directly for each check",
)
checkCmd.Flags().DurationVar(
&cacheTTL,
"cache-ttl",
60*time.Second,
"How long the brew info cache is considered valid (e.g. 30s, 2m)",
)
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version of the carafe CLI tool",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version)
os.Exit(0)
},
}
completion := completionCommand()
completion.Hidden = true
rootCmd.AddCommand(completion)
rootCmd.AddCommand(
installCmd,
uninstallCmd,
cleanupCmd,
infoCmd,
tapCmd,
untapCmd,
upgradeCmd,
checkCmd,
versionCmd,
)
return rootCmd
}
func main() {
c, err := exec.NewConfig()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := buildRootCmd(c, version).Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}