-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathroot.go
More file actions
123 lines (102 loc) · 5.05 KB
/
root.go
File metadata and controls
123 lines (102 loc) · 5.05 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
package root
import (
"github.com/OctopusDeploy/cli/pkg/apiclient"
accountCmd "github.com/OctopusDeploy/cli/pkg/cmd/account"
configCmd "github.com/OctopusDeploy/cli/pkg/cmd/config"
environmentCmd "github.com/OctopusDeploy/cli/pkg/cmd/environment"
packageCmd "github.com/OctopusDeploy/cli/pkg/cmd/package"
projectCmd "github.com/OctopusDeploy/cli/pkg/cmd/project"
projectGroupCmd "github.com/OctopusDeploy/cli/pkg/cmd/projectgroup"
releaseCmd "github.com/OctopusDeploy/cli/pkg/cmd/release"
runbookCmd "github.com/OctopusDeploy/cli/pkg/cmd/runbook"
spaceCmd "github.com/OctopusDeploy/cli/pkg/cmd/space"
deploymentTargetCmd "github.com/OctopusDeploy/cli/pkg/cmd/target"
taskCmd "github.com/OctopusDeploy/cli/pkg/cmd/task"
tenantCmd "github.com/OctopusDeploy/cli/pkg/cmd/tenant"
userCmd "github.com/OctopusDeploy/cli/pkg/cmd/user"
"github.com/OctopusDeploy/cli/pkg/cmd/version"
workerCmd "github.com/OctopusDeploy/cli/pkg/cmd/worker"
workerPoolCmd "github.com/OctopusDeploy/cli/pkg/cmd/workerpool"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/question"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// NewCmdRoot returns the base command when called without any subcommands
// note we explicitly pass in clientFactory and askProvider here because we configure them,
// which the Factory wrapper deliberately doesn't allow us to do
func NewCmdRoot(f factory.Factory, clientFactory apiclient.ClientFactory, askProvider question.AskProvider) *cobra.Command {
cmd := &cobra.Command{
Use: "octopus <command>",
Short: "Octopus Deploy CLI",
Long: `Work seamlessly with Octopus Deploy from the command line.`,
}
// ----- Child Commands -----
cmd.AddCommand(version.NewCmdVersion(f))
// infrastructure
cmd.AddCommand(accountCmd.NewCmdAccount(f))
cmd.AddCommand(environmentCmd.NewCmdEnvironment(f))
cmd.AddCommand(packageCmd.NewCmdPackage(f))
cmd.AddCommand(deploymentTargetCmd.NewCmdDeploymentTarget(f))
cmd.AddCommand(workerCmd.NewCmdWorker(f))
cmd.AddCommand(workerPoolCmd.NewCmdWorkerPool(f))
// core
cmd.AddCommand(projectGroupCmd.NewCmdProjectGroup(f))
cmd.AddCommand(projectCmd.NewCmdProject(f))
cmd.AddCommand(tenantCmd.NewCmdTenant(f))
cmd.AddCommand(taskCmd.NewCmdTask(f))
// configuration
cmd.AddCommand(configCmd.NewCmdConfig(f))
cmd.AddCommand(spaceCmd.NewCmdSpace(f))
cmd.AddCommand(userCmd.NewCmdUser(f))
cmd.AddCommand(releaseCmd.NewCmdRelease(f))
cmd.AddCommand(runbookCmd.NewCmdRunbook(f))
// ----- Configuration -----
// commands are expected to print their own errors to avoid double-ups
cmd.SilenceUsage = true
cmd.SilenceErrors = true
cmdPFlags := cmd.PersistentFlags()
cmdPFlags.BoolP(constants.FlagHelp, "h", false, "Show help for a command")
cmd.SetHelpFunc(rootHelpFunc)
cmdPFlags.StringP(constants.FlagSpace, "s", "", "Specify the space for operations")
// remember if you read FlagOutputFormat you also need to check FlagOutputFormatLegacy
cmdPFlags.StringP(constants.FlagOutputFormat, "f", constants.OutputFormatTable, `Specify the output format for a command ("json", "table", or "basic")`)
cmdPFlags.BoolP(constants.FlagNoPrompt, "", false, "Disable prompting in interactive mode")
// Legacy flags brought across from the .NET CLI.
// Consumers of these flags will have to explicitly check for them as well as the new
// flags. The pflag documentation says you can use SetNormalizeFunc to translate/alias flag
// names, however this doesn't actually work; It normalizes both the old and new flag
// names to the same thing at configuration time, then panics due to duplicate flag declarations.
cmdPFlags.String(constants.FlagOutputFormatLegacy, "", "")
_ = cmdPFlags.MarkHidden(constants.FlagOutputFormatLegacy)
flagAliases := map[string][]string{constants.FlagOutputFormat: {constants.FlagOutputFormatLegacy}}
_ = viper.BindPFlag(constants.ConfigNoPrompt, cmdPFlags.Lookup(constants.FlagNoPrompt))
_ = viper.BindPFlag(constants.ConfigSpace, cmdPFlags.Lookup(constants.FlagSpace))
// if we attempt to check the flags before Execute is called, cobra hasn't parsed anything yet,
// so we'll get bad values. PersistentPreRun is a convenient callback for setting up our
// environment after parsing but before execution.
cmd.PersistentPreRun = func(_ *cobra.Command, _ []string) {
// map flag alias values
for k, v := range flagAliases {
for _, aliasName := range v {
f := cmdPFlags.Lookup(aliasName)
r := f.Value.String() // boolean flags get stringified here but it's fast enough and a one-shot so meh
if r != f.DefValue {
_ = cmdPFlags.Lookup(k).Value.Set(r)
}
}
}
if noPrompt := viper.GetBool(constants.ConfigNoPrompt); noPrompt {
askProvider.DisableInteractive()
if v, _ := cmdPFlags.GetString(constants.FlagOutputFormat); v == "" {
cmdPFlags.Set(constants.FlagOutputFormat, constants.OutputFormatBasic)
}
}
if spaceNameOrId := viper.GetString(constants.ConfigSpace); spaceNameOrId != "" {
clientFactory.SetSpaceNameOrId(spaceNameOrId)
}
}
cmd.Flags().Bool(constants.FlagVersion, false, "Show version")
return cmd
}