Move certain sub-command optional fields to options on flag parsing method call

This commit is contained in:
Brian Picciano 2024-12-10 22:07:25 +01:00
parent 81cfdd5030
commit 37d436a428
10 changed files with 79 additions and 56 deletions

View File

@ -11,9 +11,8 @@ import (
)
var subCmdDaemon = subCmd{
name: "daemon",
descr: "Runs the isle daemon (Default if no sub-command given)",
noNetwork: true,
name: "daemon",
descr: "Runs the isle daemon (Default if no sub-command given)",
do: func(ctx subCmdCtx) error {
daemonConfigPath := ctx.flags.StringP(
"config-path", "c", "",
@ -25,7 +24,9 @@ var subCmdDaemon = subCmd{
"Write the default configuration file to stdout and exit.",
)
ctx, err := ctx.withParsedFlags()
ctx, err := ctx.withParsedFlags(&withParsedFlagsOpts{
noNetwork: true,
})
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}

View File

@ -32,9 +32,8 @@ func initMCConfigDir(envVars daecommon.EnvVars) (string, error) {
}
var subCmdGarageMC = subCmd{
name: "mc",
descr: "Runs the mc (minio-client) binary. The isle garage can be accessed under the `garage` alias",
passthroughArgs: true,
name: "mc",
descr: "Runs the mc (minio-client) binary. The isle garage can be accessed under the `garage` alias",
do: func(ctx subCmdCtx) error {
keyID := ctx.flags.StringP(
"key-id", "i", "",
@ -46,7 +45,9 @@ var subCmdGarageMC = subCmd{
"Optional key secret to use, defaults to that of the shared global key",
)
ctx, err := ctx.withParsedFlags()
ctx, err := ctx.withParsedFlags(&withParsedFlagsOpts{
passthroughArgs: true,
})
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}
@ -113,11 +114,12 @@ var subCmdGarageMC = subCmd{
}
var subCmdGarageCLI = subCmd{
name: "cli",
descr: "Runs the garage binary, automatically configured to point to the garage sub-process of a running isle daemon",
passthroughArgs: true,
name: "cli",
descr: "Runs the garage binary, automatically configured to point to the garage sub-process of a running isle daemon",
do: func(ctx subCmdCtx) error {
ctx, err := ctx.withParsedFlags()
ctx, err := ctx.withParsedFlags(&withParsedFlagsOpts{
passthroughArgs: true,
})
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}

View File

@ -31,7 +31,7 @@ var subCmdHostCreate = subCmd{
"The new host should have the ability to create hosts too",
)
ctx, err := ctx.withParsedFlags()
ctx, err := ctx.withParsedFlags(nil)
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}
@ -58,7 +58,7 @@ var subCmdHostList = subCmd{
name: "list",
descr: "Lists all hosts in the network, and their IPs",
do: doWithOutput(func(ctx subCmdCtx) (any, error) {
ctx, err := ctx.withParsedFlags()
ctx, err := ctx.withParsedFlags(nil)
if err != nil {
return nil, fmt.Errorf("parsing flags: %w", err)
}
@ -122,7 +122,7 @@ var subCmdHostRemove = subCmd{
"Name of the host to remove",
)
ctx, err := ctx.withParsedFlags()
ctx, err := ctx.withParsedFlags(nil)
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}

View File

@ -48,9 +48,8 @@ func binPath(name string) string {
}
var rootCmd = subCmd{
name: "isle",
descr: "All Isle sub-commands",
noNetwork: true,
name: "isle",
descr: "All Isle sub-commands",
do: func(ctx subCmdCtx) error {
return ctx.doSubCmd(
subCmdDaemon,

View File

@ -13,9 +13,8 @@ import (
)
var subCmdNetworkCreate = subCmd{
name: "create",
descr: "Create's a new network, with this host being the first host in that network.",
noNetwork: true,
name: "create",
descr: "Create's a new network, with this host being the first host in that network.",
do: func(ctx subCmdCtx) error {
var (
ipNet ipNetFlag
@ -45,7 +44,9 @@ var subCmdNetworkCreate = subCmd{
"Name of this host, which will be the first host in the network",
)
ctx, err := ctx.withParsedFlags()
ctx, err := ctx.withParsedFlags(&withParsedFlagsOpts{
noNetwork: true,
})
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}
@ -69,15 +70,16 @@ var subCmdNetworkCreate = subCmd{
}
var subCmdNetworkJoin = subCmd{
name: "join",
descr: "Joins this host to an existing network",
noNetwork: true,
name: "join",
descr: "Joins this host to an existing network",
do: func(ctx subCmdCtx) error {
bootstrapPath := ctx.flags.StringP(
"bootstrap-path", "b", "", "Path to a bootstrap.json file.",
)
ctx, err := ctx.withParsedFlags()
ctx, err := ctx.withParsedFlags(&withParsedFlagsOpts{
noNetwork: true,
})
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}
@ -98,11 +100,12 @@ var subCmdNetworkJoin = subCmd{
}
var subCmdNetworkList = subCmd{
name: "list",
descr: "Lists all networks which have been joined",
noNetwork: true,
name: "list",
descr: "Lists all networks which have been joined",
do: doWithOutput(func(ctx subCmdCtx) (any, error) {
ctx, err := ctx.withParsedFlags()
ctx, err := ctx.withParsedFlags(&withParsedFlagsOpts{
noNetwork: true,
})
if err != nil {
return nil, fmt.Errorf("parsing flags: %w", err)
}
@ -183,7 +186,7 @@ var subCmdNetworkGetConfig = subCmd{
name: "get-config",
descr: "Displays the currently active configuration for a joined network",
do: doWithOutput(func(ctx subCmdCtx) (any, error) {
ctx, err := ctx.withParsedFlags()
ctx, err := ctx.withParsedFlags(nil)
if err != nil {
return nil, fmt.Errorf("parsing flags: %w", err)
}

View File

@ -87,7 +87,7 @@ var subCmdStorageAllocationAdd = subCmd{
" on. Will be automatically assigned if not given.",
)
ctx, err := ctx.withParsedFlags()
ctx, err := ctx.withParsedFlags(nil)
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}
@ -118,7 +118,7 @@ var subCmdStorageAllocationList = subCmd{
plural: "s",
descr: "Lists all storage which is currently allocated on this host",
do: doWithOutput(func(ctx subCmdCtx) (any, error) {
ctx, err := ctx.withParsedFlags()
ctx, err := ctx.withParsedFlags(nil)
if err != nil {
return nil, fmt.Errorf("parsing flags: %w", err)
}
@ -144,7 +144,7 @@ var subCmdStorageAllocationRemove = subCmd{
"specified more than once",
)
ctx, err := ctx.withParsedFlags()
ctx, err := ctx.withParsedFlags(nil)
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}

View File

@ -15,7 +15,6 @@ import (
"gopkg.in/yaml.v3"
)
// TODO noNetwork and passthroughArgs should be arguments to withParsedFlags.
type subCmd struct {
name string
descr string
@ -23,15 +22,6 @@ type subCmd struct {
// If set then the name will be allowed to be suffixed with this string.
plural string
// noNetwork, if true, means the call doesn't require a network to be
// specified on the command-line if there are more than one networks
// configured.
noNetwork bool
// Extra arguments on the command-line will be passed through to some
// underlying command.
passthroughArgs bool
}
func (c subCmd) fullName() string {
@ -108,7 +98,31 @@ func usagePrefix(subCmdNames []string) string {
return fmt.Sprintf("USAGE:\n %s %s", os.Args[0], subCmdNamesStr)
}
func (ctx subCmdCtx) withParsedFlags() (subCmdCtx, error) {
type withParsedFlagsOpts struct {
// noNetwork, if true, means the call doesn't require a network to be
// specified on the command-line if there are more than one networks
// configured.
noNetwork bool
// Extra arguments on the command-line will be passed through to some
// underlying command.
passthroughArgs bool
}
func (o *withParsedFlagsOpts) withDefaults() *withParsedFlagsOpts {
if o == nil {
o = new(withParsedFlagsOpts)
}
return o
}
func (ctx subCmdCtx) withParsedFlags(
opts *withParsedFlagsOpts,
) (
subCmdCtx, error,
) {
opts = opts.withDefaults()
logLevel := logLevelFlag{mlog.LevelInfo}
ctx.flags.VarP(
&logLevel,
@ -117,7 +131,7 @@ func (ctx subCmdCtx) withParsedFlags() (subCmdCtx, error) {
)
var network string
if !ctx.subCmd.noNetwork {
if !opts.noNetwork {
ctx.flags.StringVar(
&network,
"network", "",
@ -142,7 +156,7 @@ func (ctx subCmdCtx) withParsedFlags() (subCmdCtx, error) {
}
var passthroughStr string
if ctx.subCmd.passthroughArgs {
if opts.passthroughArgs {
passthroughStr = " [--] [args...]"
}

View File

@ -7,15 +7,19 @@ import (
)
var subCmdVersion = subCmd{
name: "version",
descr: "Dumps version and build info to stdout",
noNetwork: true,
name: "version",
descr: "Dumps version and build info to stdout",
do: func(ctx subCmdCtx) error {
ctx, err := ctx.withParsedFlags(&withParsedFlagsOpts{
noNetwork: true,
})
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}
versionPath := filepath.Join(envAppDirPath, "share/version")
version, err := os.ReadFile(versionPath)
if err != nil {
return fmt.Errorf("reading version info from %q: %w", versionPath, err)
}

View File

@ -24,7 +24,7 @@ var subCmdVPNCreateCert = subCmd{
`Path to PEM file containing public key which will be embedded in the cert.`,
)
ctx, err := ctx.withParsedFlags()
ctx, err := ctx.withParsedFlags(nil)
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}

View File

@ -90,7 +90,7 @@ var subCmdVPNFirewallAdd = subCmd{
"One or more comma-separated group names to allow",
)
ctx, err := ctx.withParsedFlags()
ctx, err := ctx.withParsedFlags(nil)
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}
@ -136,7 +136,7 @@ var subCmdVPNFirewallCommit = subCmd{
name: "commit",
descr: "Commit all changes which were staged using 'add' or 'remove'",
do: func(ctx subCmdCtx) error {
ctx, err := ctx.withParsedFlags()
ctx, err := ctx.withParsedFlags(nil)
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}
@ -178,7 +178,7 @@ var subCmdVPNFirewallRemove = subCmd{
"Comma-separated indexes of rules to remove, as returned by 'show --staged'",
)
ctx, err := ctx.withParsedFlags()
ctx, err := ctx.withParsedFlags(nil)
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}
@ -290,7 +290,7 @@ var subCmdVPNFirewallShow = subCmd{
"Return the firewall configuration with staged changes included",
)
ctx, err := ctx.withParsedFlags()
ctx, err := ctx.withParsedFlags(nil)
if err != nil {
return nil, fmt.Errorf("parsing flags: %w", err)
}