-
Notifications
You must be signed in to change notification settings - Fork 37
/
root.go
241 lines (210 loc) · 7.1 KB
/
root.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright © 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"fmt"
"os"
"os/exec"
"runtime/pprof"
"sort"
"strconv"
"strings"
"github.com/clearlinux/mixer-tools/builder"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var config string
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "mixer",
Long: `Mixer is a tool used to compose OS update content and images.`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if rootCmdFlags.cpuProfile != "" {
f, err := os.Create(rootCmdFlags.cpuProfile)
if err != nil {
failf("couldn't create file for CPU profile: %s", err)
}
err = pprof.StartCPUProfile(f)
if err != nil {
failf("couldn't start profiling: %s", err)
}
}
// Both --version and --check should work regardless of the regular
// check for external programs.
if cmd.Parent() == nil { // This is RootCmd.
if rootCmdFlags.version {
fmt.Printf("Mixer %s\n", builder.Version)
os.Exit(0)
}
if rootCmdFlags.check {
ok := checkAllDeps()
if !ok {
os.Exit(1)
}
os.Exit(0)
}
}
return checkCmdDeps(cmd)
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if rootCmdFlags.cpuProfile != "" {
pprof.StopCPUProfile()
}
},
Run: func(cmd *cobra.Command, args []string) {
// Use cmd here to print exactly like other prints of Usage (that might be
// configurable).
cmd.Print(cmd.UsageString())
},
}
var rootCmdFlags = struct {
version bool
check bool
cpuProfile string
}{}
type initCmdFlags struct {
allLocal bool
allUpstream bool
clearVer string
mixver int
localRPMs bool
upstreamURL string
git bool
}
var initFlags initCmdFlags
var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize the mixer and workspace",
Long: `Initialize the mixer and workspace`,
RunE: func(cmd *cobra.Command, args []string) error {
if _, err := strconv.Atoi(initFlags.clearVer); err != nil {
if initFlags.clearVer != "latest" {
// Note: output matches Cobra's default pflag error syntax, as
// if initFlags.clearVer were an int all along.
return errors.Errorf("invalid argument \"%s\" for \"--clear-version\" flag: %s", initFlags.clearVer, err)
}
}
b := builder.New()
if config == "" {
// Create default config if necessary
if err := b.Config.CreateDefaultConfig(initFlags.localRPMs); err != nil {
fail(err)
}
}
if err := b.LoadBuilderConf(config); err != nil {
fail(err)
}
err := b.InitMix(initFlags.clearVer, strconv.Itoa(initFlags.mixver), initFlags.allLocal, initFlags.allUpstream, initFlags.upstreamURL, initFlags.git)
if err != nil {
fail(err)
}
return nil
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func init() {
RootCmd.PersistentFlags().StringVar(&rootCmdFlags.cpuProfile, "cpu-profile", "", "write CPU profile to a file")
_ = RootCmd.PersistentFlags().MarkHidden("cpu-profile")
// TODO: Remove this once we migrate to new implementation.
RootCmd.PersistentFlags().BoolVar(&builder.UseNewSwupdServer, "new-swupd", false, "EXPERIMENTAL: Use new implementation of swupd-server when possible")
// TODO: Remove this once we drop the old config format
RootCmd.PersistentFlags().BoolVar(&builder.UseNewConfig, "new-config", false, "EXPERIMENTAL: use the new TOML config format")
RootCmd.PersistentFlags().BoolVar(&builder.Offline, "offline", false, "Skip caching upstream-bundles; work entirely with local-bundles")
RootCmd.AddCommand(initCmd)
RootCmd.Flags().BoolVar(&rootCmdFlags.version, "version", false, "Print version information and quit")
RootCmd.Flags().BoolVar(&rootCmdFlags.check, "check", false, "Check all dependencies needed by mixer and quit")
initCmd.Flags().BoolVar(&initFlags.allLocal, "all-local", false, "Initialize mix with all local bundles automatically included")
initCmd.Flags().BoolVar(&initFlags.allUpstream, "all-upstream", false, "Initialize mix with all upstream bundles automatically included")
initCmd.Flags().StringVar(&initFlags.clearVer, "clear-version", "latest", "Supply the Clear version to compose the mix from")
initCmd.Flags().StringVar(&initFlags.clearVer, "upstream-version", "latest", "Alias to --clear-version")
initCmd.Flags().IntVar(&initFlags.mixver, "mix-version", 10, "Supply the Mix version to build")
initCmd.Flags().BoolVar(&initFlags.localRPMs, "local-rpms", false, "Create and configure local RPMs directories")
initCmd.Flags().StringVar(&config, "config", "", "Supply a specific builder.conf to use for mixing")
initCmd.Flags().StringVar(&initFlags.upstreamURL, "upstream-url", "https://download.clearlinux.org", "Supply an upstream URL to use for mixing")
initCmd.Flags().BoolVar(&initFlags.git, "git", false, "Track mixer's internal work dir with git")
externalDeps[initCmd] = []string{
"git",
}
}
// externalDeps let commands keep track of their external program dependencies. Those will be
// verified when the command is executed, just make sure it is filled at initialization.
var externalDeps = make(map[*cobra.Command][]string)
func checkCmdDeps(cmd *cobra.Command) error {
var deps []string
for ; cmd != nil; cmd = cmd.Parent() {
deps = append(deps, externalDeps[cmd]...)
}
sort.Strings(deps)
var missing []string
for i, dep := range deps {
if i > 0 && deps[i] == deps[i-1] {
// Skip duplicate.
continue
}
_, err := exec.LookPath(dep)
if err != nil {
missing = append(missing, dep)
}
}
if len(missing) > 0 {
return errors.Errorf("missing following external programs: %s", strings.Join(missing, ", "))
}
return nil
}
func checkAllDeps() bool {
var allDeps []string
for _, deps := range externalDeps {
allDeps = append(allDeps, deps...)
}
sort.Strings(allDeps)
var max int
for _, dep := range allDeps {
if len(dep) > max {
max = len(dep)
}
}
fmt.Println("Programs used by Mixer commands:")
ok := true
for i, dep := range allDeps {
if i > 0 && allDeps[i] == allDeps[i-1] {
// Skip duplicate.
continue
}
_, err := exec.LookPath(dep)
if err != nil {
fmt.Printf(" %-*s not found\n", max, dep)
ok = false
} else {
fmt.Printf(" %-*s ok\n", max, dep)
}
}
return ok
}
func fail(err error) {
if rootCmdFlags.cpuProfile != "" {
pprof.StopCPUProfile()
}
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
func failf(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, fmt.Sprintf("ERROR: %s\n", format), a...)
os.Exit(1)
}