forked from splunk/qbec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
169 lines (146 loc) · 5.04 KB
/
setup.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
/*
Copyright 2019 Splunk Inc.
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 main
import (
"fmt"
"os"
"path/filepath"
"github.com/mattn/go-isatty"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/splunk/qbec/internal/commands"
"github.com/splunk/qbec/internal/model"
"github.com/splunk/qbec/internal/remote"
"github.com/splunk/qbec/internal/sio"
"github.com/splunk/qbec/internal/vm"
)
func envOrDefault(name, def string) string {
v := os.Getenv(name)
if v != "" {
return v
}
return def
}
func defaultRoot() string {
return envOrDefault("QBEC_ROOT", "")
}
func usageTemplate(rootCmd string) string {
return fmt.Sprintf(`Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
Aliases:
{{.NameAndAliases}}{{end}}{{if .HasExample}}
Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
Use "%s options" for a list of global options available to all commands.
`, rootCmd)
}
var expectedFiles = []string{"qbec.yaml"}
// setWorkDir sets the working dir of the current process as the top-level
// directory of the source tree. The current working directory of the process
// must be somewhere at or under this tree. If the specified argument is non-empty
// then it is returned provided it is a valid root.
func setWorkDir(specified string) error {
isRootDir := func(dir string) bool {
for _, f := range expectedFiles {
if _, err := os.Stat(filepath.Join(dir, f)); err != nil {
return false
}
}
return true
}
cwd, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "os.Getwd")
}
orig := cwd
doChdir := func(dir string) error {
if orig != dir {
sio.Debugln(fmt.Sprintf("cd %s", dir))
if err := os.Chdir(dir); err != nil {
return err
}
}
return nil
}
if specified != "" {
abs, err := filepath.Abs(specified)
if err != nil {
return err
}
if !isRootDir(abs) {
return fmt.Errorf("specified root %q not valid, does not contain expected files", abs)
}
return doChdir(abs)
}
for {
if isRootDir(cwd) {
return doChdir(cwd)
}
old := cwd
cwd = filepath.Dir(cwd)
if cwd == "" || old == cwd {
return fmt.Errorf("unable to find source root at or above %s", orig)
}
}
}
// setup sets up all sub-commands for the supplied root command and adds facilities for commands
// to access common options.
func setup(root *cobra.Command) {
var cp commands.ConfigFactory
var rootDir string
var appTag string
vmConfigFn := vm.ConfigFromCommandParams(root, "vm:", true)
cfg := remote.NewConfig(root, "k8s:")
root.SetUsageTemplate(usageTemplate(root.CommandPath()))
root.PersistentFlags().StringVar(&rootDir, "root", defaultRoot(), "root directory of repo (from QBEC_ROOT or auto-detect)")
root.PersistentFlags().IntVarP(&cp.Verbosity, "verbose", "v", 0, "verbosity level")
root.PersistentFlags().BoolVar(&cp.Colors, "colors", false, "colorize output (set automatically if not specified)")
root.PersistentFlags().BoolVar(&cp.SkipConfirm, "yes", false, "do not prompt for confirmation")
root.PersistentFlags().BoolVar(&cp.StrictVars, "strict-vars", false, "require declared variables to be specified, do not allow undeclared variables")
root.PersistentFlags().IntVar(&cp.EvalConcurrency, "eval-concurrency", 5, "concurrency with which to evaluate components")
root.PersistentFlags().StringVar(&appTag, "app-tag", "", "build tag to create suffixed objects, indicates GC scope")
root.AddCommand(newOptionsCommand(root))
root.AddCommand(newVersionCommand())
var cmdCfg *commands.Config
root.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if cmd.Name() == "version" || cmd.Name() == "init" { // don't make these commands dependent on work dir
return nil
}
if !cmd.Flags().Changed("colors") {
cp.Colors = isatty.IsTerminal(os.Stdout.Fd())
}
sio.EnableColors = cp.Colors
if err := setWorkDir(rootDir); err != nil {
return err
}
app, err := model.NewApp("qbec.yaml", appTag)
if err != nil {
return err
}
conf, err := vmConfigFn()
if err != nil {
return commands.NewRuntimeError(err)
}
cmdCfg, err = cp.Config(app, conf, cfg)
return err
}
commands.Setup(root, func() *commands.Config {
return cmdCfg
})
}