From 65d74d396b223a53a6ac4765e1aa7c872f26f683 Mon Sep 17 00:00:00 2001 From: Krzysztof Nazarewski Date: Thu, 2 Feb 2023 13:48:56 +0100 Subject: [PATCH] support flags on both up and service commands families --- client/cmd/root.go | 45 ++++++++++++++++++-------------- client/cmd/service_controller.go | 8 ++++++ 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/client/cmd/root.go b/client/cmd/root.go index 76d2c886bc6..496452b673a 100644 --- a/client/cmd/root.go +++ b/client/cmd/root.go @@ -15,6 +15,7 @@ import ( "time" "github.com/cenkalti/backoff/v4" + "github.com/netbirdio/netbird/iface" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -105,25 +106,31 @@ func init() { rootCmd.AddCommand(sshCmd) serviceCmd.AddCommand(runCmd, startCmd, stopCmd, restartCmd) // service control commands are subcommands of service serviceCmd.AddCommand(installCmd, uninstallCmd) // service installer commands are subcommands of service - upCmd.PersistentFlags().StringSliceVar(&natExternalIPs, externalIPMapFlag, nil, - `Sets external IPs maps between local addresses and interfaces.`+ - `You can specify a comma-separated list with a single IP and IP/IP or IP/Interface Name. `+ - `An empty string "" clears the previous configuration. `+ - `E.g. --external-ip-map 12.34.56.78/10.0.0.1 or --external-ip-map 12.34.56.200,12.34.56.78/10.0.0.1,12.34.56.80/eth1 `+ - `or --external-ip-map ""`, - ) - upCmd.PersistentFlags().StringVar(&customDNSAddress, dnsResolverAddress, "", - `Sets a custom address for NetBird's local DNS resolver. `+ - `If set, the agent won't attempt to discover the best ip and port to listen on. `+ - `An empty string "" clears the previous configuration. `+ - `E.g. --dns-resolver-address 127.0.0.1:5053 or --dns-resolver-address ""`, - ) - // do not set default, because it will override whatever custom value was set - upCmd.PersistentFlags().StringVar(&wgIface, "wg-iface", "", - `WireGuard interface name to use for Netbird client instance. `+ - `MacOS restricts the tun interface name to utun[0-9]+ pattern. e.g., utun100`) - // do not set default, because it will override whatever custom value was set - upCmd.PersistentFlags().IntVar(&wgPort, "wg-port", 0, "WireGuard port number to use for Netbird client instance") + + for _, cmd := range []*cobra.Command{upCmd, serviceCmd} { + cmd.PersistentFlags().StringSliceVar(&natExternalIPs, externalIPMapFlag, nil, + `Sets external IPs maps between local addresses and interfaces.`+ + `You can specify a comma-separated list with a single IP and IP/IP or IP/Interface Name. `+ + `An empty string "" clears the previous configuration. `+ + `E.g. --external-ip-map 12.34.56.78/10.0.0.1 or --external-ip-map 12.34.56.200,12.34.56.78/10.0.0.1,12.34.56.80/eth1 `+ + `or --external-ip-map ""`, + ) + cmd.PersistentFlags().StringVar(&customDNSAddress, dnsResolverAddress, "", + `Sets a custom address for NetBird's local DNS resolver. `+ + `If set, the agent won't attempt to discover the best ip and port to listen on. `+ + `An empty string "" clears the previous configuration. `+ + `E.g. --dns-resolver-address 127.0.0.1:5053 or --dns-resolver-address ""`, + ) + // do not set default, because it will override whatever custom value was set + cmd.PersistentFlags().StringVar(&wgIface, "wg-iface", "", + `WireGuard interface name to use for Netbird client instance. `+ + fmt.Sprintf("Defaults to %s", iface.WgInterfaceDefault)+ + `MacOS restricts the tun interface name to utun[0-9]+ pattern. e.g., utun100`) + // do not set default, because it will override whatever custom value was set + cmd.PersistentFlags().IntVar(&wgPort, "wg-port", 0, + "WireGuard port number to use for Netbird client instance. "+ + fmt.Sprintf("Defaults to %s", iface.WgInterfaceDefault)) + } } diff --git a/client/cmd/service_controller.go b/client/cmd/service_controller.go index b92e1cc050c..3659512dbf6 100644 --- a/client/cmd/service_controller.go +++ b/client/cmd/service_controller.go @@ -85,6 +85,8 @@ var runCmd = &cobra.Command{ Short: "runs Netbird as service", RunE: func(cmd *cobra.Command, args []string) error { SetFlagsFromEnvVars(rootCmd) + SetFlagsFromEnvVars(serviceCmd) + SetFlagsFromEnvVars(cmd) cmd.SetOut(cmd.OutOrStdout()) @@ -119,6 +121,8 @@ var startCmd = &cobra.Command{ Short: "starts Netbird service", RunE: func(cmd *cobra.Command, args []string) error { SetFlagsFromEnvVars(rootCmd) + SetFlagsFromEnvVars(serviceCmd) + SetFlagsFromEnvVars(cmd) cmd.SetOut(cmd.OutOrStdout()) @@ -154,6 +158,8 @@ var stopCmd = &cobra.Command{ Short: "stops Netbird service", RunE: func(cmd *cobra.Command, args []string) error { SetFlagsFromEnvVars(rootCmd) + SetFlagsFromEnvVars(serviceCmd) + SetFlagsFromEnvVars(cmd) cmd.SetOut(cmd.OutOrStdout()) @@ -187,6 +193,8 @@ var restartCmd = &cobra.Command{ Short: "restarts Netbird service", RunE: func(cmd *cobra.Command, args []string) error { SetFlagsFromEnvVars(rootCmd) + SetFlagsFromEnvVars(serviceCmd) + SetFlagsFromEnvVars(cmd) cmd.SetOut(cmd.OutOrStdout())