From 06a345ecd1e521faa87c43caa3d690e6b49fcc4e Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Wed, 4 Sep 2024 22:35:29 +0200 Subject: [PATCH] Embed context directly into subCmdCtx --- go/cmd/entrypoint/client.go | 2 +- go/cmd/entrypoint/daemon.go | 10 ++++----- go/cmd/entrypoint/garage.go | 22 ++++++++----------- go/cmd/entrypoint/host.go | 42 ++++++++++++++++-------------------- go/cmd/entrypoint/main.go | 6 +++--- go/cmd/entrypoint/nebula.go | 26 +++++++++++----------- go/cmd/entrypoint/network.go | 24 ++++++++++----------- go/cmd/entrypoint/sub_cmd.go | 5 +++-- go/cmd/entrypoint/version.go | 2 +- 9 files changed, 63 insertions(+), 76 deletions(-) diff --git a/go/cmd/entrypoint/client.go b/go/cmd/entrypoint/client.go index ca16bc3..cba6ba2 100644 --- a/go/cmd/entrypoint/client.go +++ b/go/cmd/entrypoint/client.go @@ -6,7 +6,7 @@ import ( ) func (ctx subCmdCtx) getHosts() (daemon.GetHostsResult, error) { - res, err := ctx.daemonRPC.GetHosts(ctx.ctx) + res, err := ctx.daemonRPC.GetHosts(ctx) if err != nil { return daemon.GetHostsResult{}, fmt.Errorf("calling GetHosts: %w", err) } diff --git a/go/cmd/entrypoint/daemon.go b/go/cmd/entrypoint/daemon.go index bcf53c4..a21525b 100644 --- a/go/cmd/entrypoint/daemon.go +++ b/go/cmd/entrypoint/daemon.go @@ -17,9 +17,9 @@ import ( var subCmdDaemon = subCmd{ name: "daemon", 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( "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`, ) - if err := flags.Parse(subCmdCtx.args); err != nil { + if err := flags.Parse(ctx.args); err != nil { return fmt.Errorf("parsing flags: %w", err) } - ctx := subCmdCtx.ctx - if *dumpConfig { return daemon.CopyDefaultConfig(os.Stdout, envAppDirPath) } @@ -51,7 +49,7 @@ var subCmdDaemon = subCmd{ 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 // required linux capabilities are set. diff --git a/go/cmd/entrypoint/garage.go b/go/cmd/entrypoint/garage.go index ac2a956..6e78170 100644 --- a/go/cmd/entrypoint/garage.go +++ b/go/cmd/entrypoint/garage.go @@ -33,9 +33,9 @@ func initMCConfigDir() (string, error) { var subCmdGarageMC = subCmd{ name: "mc", 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( "key-id", "i", "", @@ -47,13 +47,11 @@ var subCmdGarageMC = subCmd{ "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) } - clientParams, err := subCmdCtx.daemonRPC.GetGarageClientParams( - subCmdCtx.ctx, - ) + clientParams, err := ctx.daemonRPC.GetGarageClientParams(ctx) if err != nil { return fmt.Errorf("calling GetGarageClientParams: %w", err) } @@ -115,11 +113,9 @@ 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", - do: func(subCmdCtx subCmdCtx) error { + do: func(ctx subCmdCtx) error { - clientParams, err := subCmdCtx.daemonRPC.GetGarageClientParams( - subCmdCtx.ctx, - ) + clientParams, err := ctx.daemonRPC.GetGarageClientParams(ctx) if err != nil { return fmt.Errorf("calling GetGarageClientParams: %w", err) } @@ -130,7 +126,7 @@ var subCmdGarageCLI = subCmd{ var ( binPath = binPath("garage") - args = append([]string{"garage"}, subCmdCtx.args...) + args = append([]string{"garage"}, ctx.args...) cliEnv = append( os.Environ(), "GARAGE_RPC_HOST="+clientParams.Peer.RPCPeerAddr(), @@ -152,8 +148,8 @@ var subCmdGarageCLI = subCmd{ var subCmdGarage = subCmd{ name: "garage", descr: "Runs the garage binary, automatically configured to point to the garage sub-process of a running isle daemon", - do: func(subCmdCtx subCmdCtx) error { - return subCmdCtx.doSubCmd( + do: func(ctx subCmdCtx) error { + return ctx.doSubCmd( subCmdGarageCLI, subCmdGarageMC, ) diff --git a/go/cmd/entrypoint/host.go b/go/cmd/entrypoint/host.go index ff5c235..fe7a214 100644 --- a/go/cmd/entrypoint/host.go +++ b/go/cmd/entrypoint/host.go @@ -14,9 +14,9 @@ import ( var subCmdHostCreate = subCmd{ name: "create", 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 ( - flags = subCmdCtx.flagSet(false) + flags = ctx.flagSet(false) hostName hostNameFlag ip ipFlag ) @@ -35,7 +35,7 @@ var subCmdHostCreate = subCmd{ "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) } @@ -43,15 +43,13 @@ var subCmdHostCreate = subCmd{ return errors.New("--hostname is required") } - res, err := subCmdCtx.daemonRPC.CreateHost( - subCmdCtx.ctx, daemon.CreateHostRequest{ - HostName: hostName.V, - Opts: daemon.CreateHostOpts{ - IP: ip.V, - CanCreateHosts: *canCreateHosts, - }, + res, err := ctx.daemonRPC.CreateHost(ctx, daemon.CreateHostRequest{ + HostName: hostName.V, + Opts: daemon.CreateHostOpts{ + IP: ip.V, + CanCreateHosts: *canCreateHosts, }, - ) + }) if err != nil { return fmt.Errorf("calling CreateHost: %w", err) } @@ -63,8 +61,8 @@ var subCmdHostCreate = subCmd{ var subCmdHostList = subCmd{ name: "list", descr: "Lists all hosts in the network, and their IPs", - do: func(subCmdCtx subCmdCtx) error { - hostsRes, err := subCmdCtx.getHosts() + do: func(ctx subCmdCtx) error { + hostsRes, err := ctx.getHosts() if err != nil { return fmt.Errorf("calling GetHosts: %w", err) } @@ -99,9 +97,9 @@ var subCmdHostList = subCmd{ var subCmdHostRemove = subCmd{ name: "remove", descr: "Removes a host from the network", - do: func(subCmdCtx subCmdCtx) error { + do: func(ctx subCmdCtx) error { var ( - flags = subCmdCtx.flagSet(false) + flags = ctx.flagSet(false) hostName hostNameFlag ) @@ -111,7 +109,7 @@ var subCmdHostRemove = subCmd{ "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) } @@ -119,11 +117,9 @@ var subCmdHostRemove = subCmd{ return errors.New("--hostname is required") } - _, err := subCmdCtx.daemonRPC.RemoveHost( - subCmdCtx.ctx, daemon.RemoveHostRequest{ - HostName: hostName.V, - }, - ) + _, err := ctx.daemonRPC.RemoveHost(ctx, daemon.RemoveHostRequest{ + HostName: hostName.V, + }) if err != nil { return fmt.Errorf("calling RemoveHost: %w", err) } @@ -136,8 +132,8 @@ var subCmdHost = subCmd{ name: "host", plural: "s", descr: "Sub-commands having to do with configuration of hosts in the network", - do: func(subCmdCtx subCmdCtx) error { - return subCmdCtx.doSubCmd( + do: func(ctx subCmdCtx) error { + return ctx.doSubCmd( subCmdHostCreate, subCmdHostRemove, subCmdHostList, diff --git a/go/cmd/entrypoint/main.go b/go/cmd/entrypoint/main.go index 09d5ee6..21f89de 100644 --- a/go/cmd/entrypoint/main.go +++ b/go/cmd/entrypoint/main.go @@ -57,9 +57,9 @@ func main() { }() err := subCmdCtx{ - args: os.Args[1:], - ctx: ctx, - logger: logger, + Context: ctx, + args: os.Args[1:], + logger: logger, }.doSubCmd( subCmdDaemon, subCmdGarage, diff --git a/go/cmd/entrypoint/nebula.go b/go/cmd/entrypoint/nebula.go index 333d454..1f8358f 100644 --- a/go/cmd/entrypoint/nebula.go +++ b/go/cmd/entrypoint/nebula.go @@ -12,9 +12,9 @@ import ( var subCmdNebulaCreateCert = subCmd{ name: "create-cert", 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 ( - flags = subCmdCtx.flagSet(false) + flags = ctx.flagSet(false) hostName hostNameFlag ) @@ -29,7 +29,7 @@ var subCmdNebulaCreateCert = subCmd{ `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) } @@ -47,8 +47,8 @@ var subCmdNebulaCreateCert = subCmd{ return fmt.Errorf("unmarshaling public key as PEM: %w", err) } - res, err := subCmdCtx.daemonRPC.CreateNebulaCertificate( - subCmdCtx.ctx, daemon.CreateNebulaCertificateRequest{ + res, err := ctx.daemonRPC.CreateNebulaCertificate( + ctx, daemon.CreateNebulaCertificateRequest{ HostName: hostName.V, HostEncryptingPublicKey: hostPub, }, @@ -73,21 +73,19 @@ var subCmdNebulaCreateCert = subCmd{ var subCmdNebulaShow = subCmd{ name: "show", descr: "Writes nebula network information to stdout in JSON format", - do: func(subCmdCtx subCmdCtx) error { + do: func(ctx subCmdCtx) error { - flags := subCmdCtx.flagSet(false) - if err := flags.Parse(subCmdCtx.args); err != nil { + flags := ctx.flagSet(false) + if err := flags.Parse(ctx.args); err != nil { return fmt.Errorf("parsing flags: %w", err) } - hosts, err := subCmdCtx.getHosts() + hosts, err := ctx.getHosts() if err != nil { return fmt.Errorf("getting hosts: %w", err) } - caPublicCreds, err := subCmdCtx.daemonRPC.GetNebulaCAPublicCredentials( - subCmdCtx.ctx, - ) + caPublicCreds, err := ctx.daemonRPC.GetNebulaCAPublicCredentials(ctx) if err != nil { return fmt.Errorf("calling GetNebulaCAPublicCredentials: %w", err) } @@ -140,8 +138,8 @@ var subCmdNebulaShow = subCmd{ var subCmdNebula = subCmd{ name: "nebula", descr: "Sub-commands related to the nebula VPN", - do: func(subCmdCtx subCmdCtx) error { - return subCmdCtx.doSubCmd( + do: func(ctx subCmdCtx) error { + return ctx.doSubCmd( subCmdNebulaCreateCert, subCmdNebulaShow, ) diff --git a/go/cmd/entrypoint/network.go b/go/cmd/entrypoint/network.go index e1f9998..7531305 100644 --- a/go/cmd/entrypoint/network.go +++ b/go/cmd/entrypoint/network.go @@ -10,9 +10,9 @@ import ( var subCmdNetworkCreate = subCmd{ name: "create", 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 ( - flags = subCmdCtx.flagSet(false) + flags = ctx.flagSet(false) ipNet ipNetFlag hostName hostNameFlag ) @@ -40,7 +40,7 @@ var subCmdNetworkCreate = subCmd{ "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) } @@ -51,8 +51,8 @@ var subCmdNetworkCreate = subCmd{ return errors.New("--name, --domain, --ip-net, and --hostname are required") } - _, err := subCmdCtx.daemonRPC.CreateNetwork( - subCmdCtx.ctx, *name, *domain, ipNet.V, hostName.V, + _, err := ctx.daemonRPC.CreateNetwork( + ctx, *name, *domain, ipNet.V, hostName.V, ) if err != nil { return fmt.Errorf("creating network: %w", err) @@ -65,15 +65,15 @@ var subCmdNetworkCreate = subCmd{ var subCmdNetworkJoin = subCmd{ name: "join", descr: "Joins this host to an existing network", - do: func(subCmdCtx subCmdCtx) error { + do: func(ctx subCmdCtx) error { var ( - flags = subCmdCtx.flagSet(false) + flags = ctx.flagSet(false) bootstrapPath = flags.StringP( "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) } @@ -88,9 +88,7 @@ var subCmdNetworkJoin = subCmd{ ) } - _, err := subCmdCtx.daemonRPC.JoinNetwork( - subCmdCtx.ctx, newBootstrap, - ) + _, err := ctx.daemonRPC.JoinNetwork(ctx, newBootstrap) return err }, } @@ -98,8 +96,8 @@ var subCmdNetworkJoin = subCmd{ var subCmdNetwork = subCmd{ name: "network", descr: "Sub-commands related to network membership", - do: func(subCmdCtx subCmdCtx) error { - return subCmdCtx.doSubCmd( + do: func(ctx subCmdCtx) error { + return ctx.doSubCmd( subCmdNetworkCreate, subCmdNetworkJoin, ) diff --git a/go/cmd/entrypoint/sub_cmd.go b/go/cmd/entrypoint/sub_cmd.go index fb0686e..5a260e0 100644 --- a/go/cmd/entrypoint/sub_cmd.go +++ b/go/cmd/entrypoint/sub_cmd.go @@ -30,11 +30,12 @@ func (fs flagSet) Parse(args []string) error { // subCmdCtx contains all information available to a subCmd's do method. type subCmdCtx struct { + context.Context + subCmd subCmd // the subCmd itself args []string // command-line arguments, excluding the subCmd itself. subCmdNames []string // names of subCmds so far, including this one - ctx context.Context logger *mlog.Logger daemonRPC daemon.RPC } @@ -133,10 +134,10 @@ func (ctx subCmdCtx) doSubCmd(subCmds ...subCmd) error { ) err := subCmd.do(subCmdCtx{ + Context: ctx.Context, subCmd: subCmd, args: args, subCmdNames: append(ctx.subCmdNames, subCmdName), - ctx: ctx.ctx, logger: ctx.logger, daemonRPC: daemonRPC, }) diff --git a/go/cmd/entrypoint/version.go b/go/cmd/entrypoint/version.go index 039d02d..0bc4a09 100644 --- a/go/cmd/entrypoint/version.go +++ b/go/cmd/entrypoint/version.go @@ -9,7 +9,7 @@ import ( var subCmdVersion = subCmd{ name: "version", descr: "Dumps version and build info to stdout", - do: func(subCmdCtx subCmdCtx) error { + do: func(ctx subCmdCtx) error { versionPath := filepath.Join(envAppDirPath, "share/version")