Embed context directly into subCmdCtx

This commit is contained in:
Brian Picciano 2024-09-04 22:35:29 +02:00
parent 6c185f6263
commit 06a345ecd1
9 changed files with 63 additions and 76 deletions

View File

@ -6,7 +6,7 @@ import (
) )
func (ctx subCmdCtx) getHosts() (daemon.GetHostsResult, error) { func (ctx subCmdCtx) getHosts() (daemon.GetHostsResult, error) {
res, err := ctx.daemonRPC.GetHosts(ctx.ctx) res, err := ctx.daemonRPC.GetHosts(ctx)
if err != nil { if err != nil {
return daemon.GetHostsResult{}, fmt.Errorf("calling GetHosts: %w", err) return daemon.GetHostsResult{}, fmt.Errorf("calling GetHosts: %w", err)
} }

View File

@ -17,9 +17,9 @@ import (
var subCmdDaemon = subCmd{ var subCmdDaemon = subCmd{
name: "daemon", name: "daemon",
descr: "Runs the isle daemon (Default if no sub-command given)", descr: "Runs the isle daemon (Default if no sub-command given)",
do: func(subCmdCtx subCmdCtx) error { do: func(ctx subCmdCtx) error {
flags := subCmdCtx.flagSet(false) flags := ctx.flagSet(false)
daemonConfigPath := flags.StringP( daemonConfigPath := flags.StringP(
"config-path", "c", "", "config-path", "c", "",
@ -36,12 +36,10 @@ var subCmdDaemon = subCmd{
`Maximum log level which should be output. Values can be "debug", "info", "warn", "error", "fatal". Does not apply to sub-processes`, `Maximum log level which should be output. Values can be "debug", "info", "warn", "error", "fatal". Does not apply to sub-processes`,
) )
if err := flags.Parse(subCmdCtx.args); err != nil { if err := flags.Parse(ctx.args); err != nil {
return fmt.Errorf("parsing flags: %w", err) return fmt.Errorf("parsing flags: %w", err)
} }
ctx := subCmdCtx.ctx
if *dumpConfig { if *dumpConfig {
return daemon.CopyDefaultConfig(os.Stdout, envAppDirPath) return daemon.CopyDefaultConfig(os.Stdout, envAppDirPath)
} }
@ -51,7 +49,7 @@ var subCmdDaemon = subCmd{
return fmt.Errorf("couldn't parse log level %q", *logLevelStr) return fmt.Errorf("couldn't parse log level %q", *logLevelStr)
} }
logger := subCmdCtx.logger.WithMaxLevel(logLevel.Int()) logger := ctx.logger.WithMaxLevel(logLevel.Int())
// TODO check that daemon is either running as root, or that the // TODO check that daemon is either running as root, or that the
// required linux capabilities are set. // required linux capabilities are set.

View File

@ -33,9 +33,9 @@ func initMCConfigDir() (string, error) {
var subCmdGarageMC = subCmd{ var subCmdGarageMC = subCmd{
name: "mc", name: "mc",
descr: "Runs the mc (minio-client) binary. The isle garage can be accessed under the `garage` alias", descr: "Runs the mc (minio-client) binary. The isle garage can be accessed under the `garage` alias",
do: func(subCmdCtx subCmdCtx) error { do: func(ctx subCmdCtx) error {
flags := subCmdCtx.flagSet(true) flags := ctx.flagSet(true)
keyID := flags.StringP( keyID := flags.StringP(
"key-id", "i", "", "key-id", "i", "",
@ -47,13 +47,11 @@ var subCmdGarageMC = subCmd{
"Optional key secret to use, defaults to that of the shared global key", "Optional key secret to use, defaults to that of the shared global key",
) )
if err := flags.Parse(subCmdCtx.args); err != nil { if err := flags.Parse(ctx.args); err != nil {
return fmt.Errorf("parsing flags: %w", err) return fmt.Errorf("parsing flags: %w", err)
} }
clientParams, err := subCmdCtx.daemonRPC.GetGarageClientParams( clientParams, err := ctx.daemonRPC.GetGarageClientParams(ctx)
subCmdCtx.ctx,
)
if err != nil { if err != nil {
return fmt.Errorf("calling GetGarageClientParams: %w", err) return fmt.Errorf("calling GetGarageClientParams: %w", err)
} }
@ -115,11 +113,9 @@ var subCmdGarageMC = subCmd{
var subCmdGarageCLI = subCmd{ var subCmdGarageCLI = subCmd{
name: "cli", name: "cli",
descr: "Runs the garage binary, automatically configured to point to the garage sub-process of a running isle daemon", descr: "Runs the garage binary, automatically configured to point to the garage sub-process of a running isle daemon",
do: func(subCmdCtx subCmdCtx) error { do: func(ctx subCmdCtx) error {
clientParams, err := subCmdCtx.daemonRPC.GetGarageClientParams( clientParams, err := ctx.daemonRPC.GetGarageClientParams(ctx)
subCmdCtx.ctx,
)
if err != nil { if err != nil {
return fmt.Errorf("calling GetGarageClientParams: %w", err) return fmt.Errorf("calling GetGarageClientParams: %w", err)
} }
@ -130,7 +126,7 @@ var subCmdGarageCLI = subCmd{
var ( var (
binPath = binPath("garage") binPath = binPath("garage")
args = append([]string{"garage"}, subCmdCtx.args...) args = append([]string{"garage"}, ctx.args...)
cliEnv = append( cliEnv = append(
os.Environ(), os.Environ(),
"GARAGE_RPC_HOST="+clientParams.Peer.RPCPeerAddr(), "GARAGE_RPC_HOST="+clientParams.Peer.RPCPeerAddr(),
@ -152,8 +148,8 @@ var subCmdGarageCLI = subCmd{
var subCmdGarage = subCmd{ var subCmdGarage = subCmd{
name: "garage", name: "garage",
descr: "Runs the garage binary, automatically configured to point to the garage sub-process of a running isle daemon", descr: "Runs the garage binary, automatically configured to point to the garage sub-process of a running isle daemon",
do: func(subCmdCtx subCmdCtx) error { do: func(ctx subCmdCtx) error {
return subCmdCtx.doSubCmd( return ctx.doSubCmd(
subCmdGarageCLI, subCmdGarageCLI,
subCmdGarageMC, subCmdGarageMC,
) )

View File

@ -14,9 +14,9 @@ import (
var subCmdHostCreate = subCmd{ var subCmdHostCreate = subCmd{
name: "create", name: "create",
descr: "Creates a new host in the network, writing its new bootstrap.json to stdout", descr: "Creates a new host in the network, writing its new bootstrap.json to stdout",
do: func(subCmdCtx subCmdCtx) error { do: func(ctx subCmdCtx) error {
var ( var (
flags = subCmdCtx.flagSet(false) flags = ctx.flagSet(false)
hostName hostNameFlag hostName hostNameFlag
ip ipFlag ip ipFlag
) )
@ -35,7 +35,7 @@ var subCmdHostCreate = subCmd{
"The new host should have the ability to create hosts too", "The new host should have the ability to create hosts too",
) )
if err := flags.Parse(subCmdCtx.args); err != nil { if err := flags.Parse(ctx.args); err != nil {
return fmt.Errorf("parsing flags: %w", err) return fmt.Errorf("parsing flags: %w", err)
} }
@ -43,15 +43,13 @@ var subCmdHostCreate = subCmd{
return errors.New("--hostname is required") return errors.New("--hostname is required")
} }
res, err := subCmdCtx.daemonRPC.CreateHost( res, err := ctx.daemonRPC.CreateHost(ctx, daemon.CreateHostRequest{
subCmdCtx.ctx, daemon.CreateHostRequest{
HostName: hostName.V, HostName: hostName.V,
Opts: daemon.CreateHostOpts{ Opts: daemon.CreateHostOpts{
IP: ip.V, IP: ip.V,
CanCreateHosts: *canCreateHosts, CanCreateHosts: *canCreateHosts,
}, },
}, })
)
if err != nil { if err != nil {
return fmt.Errorf("calling CreateHost: %w", err) return fmt.Errorf("calling CreateHost: %w", err)
} }
@ -63,8 +61,8 @@ var subCmdHostCreate = subCmd{
var subCmdHostList = subCmd{ var subCmdHostList = subCmd{
name: "list", name: "list",
descr: "Lists all hosts in the network, and their IPs", descr: "Lists all hosts in the network, and their IPs",
do: func(subCmdCtx subCmdCtx) error { do: func(ctx subCmdCtx) error {
hostsRes, err := subCmdCtx.getHosts() hostsRes, err := ctx.getHosts()
if err != nil { if err != nil {
return fmt.Errorf("calling GetHosts: %w", err) return fmt.Errorf("calling GetHosts: %w", err)
} }
@ -99,9 +97,9 @@ var subCmdHostList = subCmd{
var subCmdHostRemove = subCmd{ var subCmdHostRemove = subCmd{
name: "remove", name: "remove",
descr: "Removes a host from the network", descr: "Removes a host from the network",
do: func(subCmdCtx subCmdCtx) error { do: func(ctx subCmdCtx) error {
var ( var (
flags = subCmdCtx.flagSet(false) flags = ctx.flagSet(false)
hostName hostNameFlag hostName hostNameFlag
) )
@ -111,7 +109,7 @@ var subCmdHostRemove = subCmd{
"Name of the host to remove", "Name of the host to remove",
) )
if err := flags.Parse(subCmdCtx.args); err != nil { if err := flags.Parse(ctx.args); err != nil {
return fmt.Errorf("parsing flags: %w", err) return fmt.Errorf("parsing flags: %w", err)
} }
@ -119,11 +117,9 @@ var subCmdHostRemove = subCmd{
return errors.New("--hostname is required") return errors.New("--hostname is required")
} }
_, err := subCmdCtx.daemonRPC.RemoveHost( _, err := ctx.daemonRPC.RemoveHost(ctx, daemon.RemoveHostRequest{
subCmdCtx.ctx, daemon.RemoveHostRequest{
HostName: hostName.V, HostName: hostName.V,
}, })
)
if err != nil { if err != nil {
return fmt.Errorf("calling RemoveHost: %w", err) return fmt.Errorf("calling RemoveHost: %w", err)
} }
@ -136,8 +132,8 @@ var subCmdHost = subCmd{
name: "host", name: "host",
plural: "s", plural: "s",
descr: "Sub-commands having to do with configuration of hosts in the network", descr: "Sub-commands having to do with configuration of hosts in the network",
do: func(subCmdCtx subCmdCtx) error { do: func(ctx subCmdCtx) error {
return subCmdCtx.doSubCmd( return ctx.doSubCmd(
subCmdHostCreate, subCmdHostCreate,
subCmdHostRemove, subCmdHostRemove,
subCmdHostList, subCmdHostList,

View File

@ -57,8 +57,8 @@ func main() {
}() }()
err := subCmdCtx{ err := subCmdCtx{
Context: ctx,
args: os.Args[1:], args: os.Args[1:],
ctx: ctx,
logger: logger, logger: logger,
}.doSubCmd( }.doSubCmd(
subCmdDaemon, subCmdDaemon,

View File

@ -12,9 +12,9 @@ import (
var subCmdNebulaCreateCert = subCmd{ var subCmdNebulaCreateCert = subCmd{
name: "create-cert", name: "create-cert",
descr: "Creates a signed nebula certificate file for an existing host and writes it to stdout", descr: "Creates a signed nebula certificate file for an existing host and writes it to stdout",
do: func(subCmdCtx subCmdCtx) error { do: func(ctx subCmdCtx) error {
var ( var (
flags = subCmdCtx.flagSet(false) flags = ctx.flagSet(false)
hostName hostNameFlag hostName hostNameFlag
) )
@ -29,7 +29,7 @@ var subCmdNebulaCreateCert = subCmd{
`Path to PEM file containing public key which will be embedded in the cert.`, `Path to PEM file containing public key which will be embedded in the cert.`,
) )
if err := flags.Parse(subCmdCtx.args); err != nil { if err := flags.Parse(ctx.args); err != nil {
return fmt.Errorf("parsing flags: %w", err) return fmt.Errorf("parsing flags: %w", err)
} }
@ -47,8 +47,8 @@ var subCmdNebulaCreateCert = subCmd{
return fmt.Errorf("unmarshaling public key as PEM: %w", err) return fmt.Errorf("unmarshaling public key as PEM: %w", err)
} }
res, err := subCmdCtx.daemonRPC.CreateNebulaCertificate( res, err := ctx.daemonRPC.CreateNebulaCertificate(
subCmdCtx.ctx, daemon.CreateNebulaCertificateRequest{ ctx, daemon.CreateNebulaCertificateRequest{
HostName: hostName.V, HostName: hostName.V,
HostEncryptingPublicKey: hostPub, HostEncryptingPublicKey: hostPub,
}, },
@ -73,21 +73,19 @@ var subCmdNebulaCreateCert = subCmd{
var subCmdNebulaShow = subCmd{ var subCmdNebulaShow = subCmd{
name: "show", name: "show",
descr: "Writes nebula network information to stdout in JSON format", descr: "Writes nebula network information to stdout in JSON format",
do: func(subCmdCtx subCmdCtx) error { do: func(ctx subCmdCtx) error {
flags := subCmdCtx.flagSet(false) flags := ctx.flagSet(false)
if err := flags.Parse(subCmdCtx.args); err != nil { if err := flags.Parse(ctx.args); err != nil {
return fmt.Errorf("parsing flags: %w", err) return fmt.Errorf("parsing flags: %w", err)
} }
hosts, err := subCmdCtx.getHosts() hosts, err := ctx.getHosts()
if err != nil { if err != nil {
return fmt.Errorf("getting hosts: %w", err) return fmt.Errorf("getting hosts: %w", err)
} }
caPublicCreds, err := subCmdCtx.daemonRPC.GetNebulaCAPublicCredentials( caPublicCreds, err := ctx.daemonRPC.GetNebulaCAPublicCredentials(ctx)
subCmdCtx.ctx,
)
if err != nil { if err != nil {
return fmt.Errorf("calling GetNebulaCAPublicCredentials: %w", err) return fmt.Errorf("calling GetNebulaCAPublicCredentials: %w", err)
} }
@ -140,8 +138,8 @@ var subCmdNebulaShow = subCmd{
var subCmdNebula = subCmd{ var subCmdNebula = subCmd{
name: "nebula", name: "nebula",
descr: "Sub-commands related to the nebula VPN", descr: "Sub-commands related to the nebula VPN",
do: func(subCmdCtx subCmdCtx) error { do: func(ctx subCmdCtx) error {
return subCmdCtx.doSubCmd( return ctx.doSubCmd(
subCmdNebulaCreateCert, subCmdNebulaCreateCert,
subCmdNebulaShow, subCmdNebulaShow,
) )

View File

@ -10,9 +10,9 @@ import (
var subCmdNetworkCreate = subCmd{ var subCmdNetworkCreate = subCmd{
name: "create", name: "create",
descr: "Create's a new network, with this host being the first host in that network.", descr: "Create's a new network, with this host being the first host in that network.",
do: func(subCmdCtx subCmdCtx) error { do: func(ctx subCmdCtx) error {
var ( var (
flags = subCmdCtx.flagSet(false) flags = ctx.flagSet(false)
ipNet ipNetFlag ipNet ipNetFlag
hostName hostNameFlag hostName hostNameFlag
) )
@ -40,7 +40,7 @@ var subCmdNetworkCreate = subCmd{
"Name of this host, which will be the first host in the network", "Name of this host, which will be the first host in the network",
) )
if err := flags.Parse(subCmdCtx.args); err != nil { if err := flags.Parse(ctx.args); err != nil {
return fmt.Errorf("parsing flags: %w", err) return fmt.Errorf("parsing flags: %w", err)
} }
@ -51,8 +51,8 @@ var subCmdNetworkCreate = subCmd{
return errors.New("--name, --domain, --ip-net, and --hostname are required") return errors.New("--name, --domain, --ip-net, and --hostname are required")
} }
_, err := subCmdCtx.daemonRPC.CreateNetwork( _, err := ctx.daemonRPC.CreateNetwork(
subCmdCtx.ctx, *name, *domain, ipNet.V, hostName.V, ctx, *name, *domain, ipNet.V, hostName.V,
) )
if err != nil { if err != nil {
return fmt.Errorf("creating network: %w", err) return fmt.Errorf("creating network: %w", err)
@ -65,15 +65,15 @@ var subCmdNetworkCreate = subCmd{
var subCmdNetworkJoin = subCmd{ var subCmdNetworkJoin = subCmd{
name: "join", name: "join",
descr: "Joins this host to an existing network", descr: "Joins this host to an existing network",
do: func(subCmdCtx subCmdCtx) error { do: func(ctx subCmdCtx) error {
var ( var (
flags = subCmdCtx.flagSet(false) flags = ctx.flagSet(false)
bootstrapPath = flags.StringP( bootstrapPath = flags.StringP(
"bootstrap-path", "b", "", "Path to a bootstrap.json file.", "bootstrap-path", "b", "", "Path to a bootstrap.json file.",
) )
) )
if err := flags.Parse(subCmdCtx.args); err != nil { if err := flags.Parse(ctx.args); err != nil {
return fmt.Errorf("parsing flags: %w", err) return fmt.Errorf("parsing flags: %w", err)
} }
@ -88,9 +88,7 @@ var subCmdNetworkJoin = subCmd{
) )
} }
_, err := subCmdCtx.daemonRPC.JoinNetwork( _, err := ctx.daemonRPC.JoinNetwork(ctx, newBootstrap)
subCmdCtx.ctx, newBootstrap,
)
return err return err
}, },
} }
@ -98,8 +96,8 @@ var subCmdNetworkJoin = subCmd{
var subCmdNetwork = subCmd{ var subCmdNetwork = subCmd{
name: "network", name: "network",
descr: "Sub-commands related to network membership", descr: "Sub-commands related to network membership",
do: func(subCmdCtx subCmdCtx) error { do: func(ctx subCmdCtx) error {
return subCmdCtx.doSubCmd( return ctx.doSubCmd(
subCmdNetworkCreate, subCmdNetworkCreate,
subCmdNetworkJoin, subCmdNetworkJoin,
) )

View File

@ -30,11 +30,12 @@ func (fs flagSet) Parse(args []string) error {
// subCmdCtx contains all information available to a subCmd's do method. // subCmdCtx contains all information available to a subCmd's do method.
type subCmdCtx struct { type subCmdCtx struct {
context.Context
subCmd subCmd // the subCmd itself subCmd subCmd // the subCmd itself
args []string // command-line arguments, excluding the subCmd itself. args []string // command-line arguments, excluding the subCmd itself.
subCmdNames []string // names of subCmds so far, including this one subCmdNames []string // names of subCmds so far, including this one
ctx context.Context
logger *mlog.Logger logger *mlog.Logger
daemonRPC daemon.RPC daemonRPC daemon.RPC
} }
@ -133,10 +134,10 @@ func (ctx subCmdCtx) doSubCmd(subCmds ...subCmd) error {
) )
err := subCmd.do(subCmdCtx{ err := subCmd.do(subCmdCtx{
Context: ctx.Context,
subCmd: subCmd, subCmd: subCmd,
args: args, args: args,
subCmdNames: append(ctx.subCmdNames, subCmdName), subCmdNames: append(ctx.subCmdNames, subCmdName),
ctx: ctx.ctx,
logger: ctx.logger, logger: ctx.logger,
daemonRPC: daemonRPC, daemonRPC: daemonRPC,
}) })

View File

@ -9,7 +9,7 @@ import (
var subCmdVersion = subCmd{ var subCmdVersion = subCmd{
name: "version", name: "version",
descr: "Dumps version and build info to stdout", descr: "Dumps version and build info to stdout",
do: func(subCmdCtx subCmdCtx) error { do: func(ctx subCmdCtx) error {
versionPath := filepath.Join(envAppDirPath, "share/version") versionPath := filepath.Join(envAppDirPath, "share/version")