From 400d17eb5878f2554df10c182fa1b0c2772f2c8f Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Fri, 9 Aug 2024 18:23:52 +0200 Subject: [PATCH] Parse data from setup key refacto functions and variable assigment --- client/cmd/login.go | 12 +++++------- client/cmd/root.go | 12 +++++++++++- client/cmd/up.go | 21 ++++++++++++--------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/client/cmd/login.go b/client/cmd/login.go index 512fbb08133..c7dd0fda1f3 100644 --- a/client/cmd/login.go +++ b/client/cmd/login.go @@ -39,11 +39,9 @@ var loginCmd = &cobra.Command{ ctx = context.WithValue(ctx, system.DeviceNameCtxKey, hostName) } - if setupKeyPath != "" && setupKey == "" { - setupKey, err = getSetupKeyFromFile(setupKeyPath) - if err != nil { - return err - } + providedSetupKey, err := getSetupKey() + if err != nil { + return err } // workaround to run without service @@ -69,7 +67,7 @@ var loginCmd = &cobra.Command{ config, _ = internal.UpdateOldManagementURL(ctx, config, configPath) - err = foregroundLogin(ctx, cmd, config, setupKey) + err = foregroundLogin(ctx, cmd, config, providedSetupKey) if err != nil { return fmt.Errorf("foreground login failed: %v", err) } @@ -88,7 +86,7 @@ var loginCmd = &cobra.Command{ client := proto.NewDaemonServiceClient(conn) loginRequest := proto.LoginRequest{ - SetupKey: setupKey, + SetupKey: providedSetupKey, ManagementUrl: managementURL, IsLinuxDesktopClient: isLinuxRunningDesktop(), Hostname: hostName, diff --git a/client/cmd/root.go b/client/cmd/root.go index b6d6694ee68..8dae6e27374 100644 --- a/client/cmd/root.go +++ b/client/cmd/root.go @@ -256,9 +256,19 @@ var CLIBackOffSettings = &backoff.ExponentialBackOff{ Clock: backoff.SystemClock, } +func getSetupKey() (string, error) { + if setupKeyPath != "" && setupKey == "" { + return getSetupKeyFromFile(setupKeyPath) + } + return setupKey, nil +} + func getSetupKeyFromFile(setupKeyPath string) (string, error) { data, err := os.ReadFile(setupKeyPath) - return string(data), err + if err != nil { + return "", fmt.Errorf("failed to read setup key file: %v", err) + } + return strings.TrimSpace(string(data)), nil } func handleRebrand(cmd *cobra.Command) error { diff --git a/client/cmd/up.go b/client/cmd/up.go index 0eaf7bc0d61..2ed6e41d216 100644 --- a/client/cmd/up.go +++ b/client/cmd/up.go @@ -73,13 +73,6 @@ func upFunc(cmd *cobra.Command, args []string) error { ctx = context.WithValue(ctx, system.DeviceNameCtxKey, hostName) } - if setupKeyPath != "" && setupKey == "" { - setupKey, err = getSetupKeyFromFile(setupKeyPath) - if err != nil { - return err - } - } - if foregroundMode { return runInForegroundMode(ctx, cmd) } @@ -154,6 +147,11 @@ func runInForegroundMode(ctx context.Context, cmd *cobra.Command) error { ic.DNSRouteInterval = &dnsRouteInterval } + providedSetupKey, err := getSetupKey() + if err != nil { + return err + } + config, err := internal.UpdateOrCreateConfig(ic) if err != nil { return fmt.Errorf("get config file: %v", err) @@ -161,7 +159,7 @@ func runInForegroundMode(ctx context.Context, cmd *cobra.Command) error { config, _ = internal.UpdateOldManagementURL(ctx, config, configPath) - err = foregroundLogin(ctx, cmd, config, setupKey) + err = foregroundLogin(ctx, cmd, config, providedSetupKey) if err != nil { return fmt.Errorf("foreground login failed: %v", err) } @@ -206,8 +204,13 @@ func runInDaemonMode(ctx context.Context, cmd *cobra.Command) error { return nil } + providedSetupKey, err := getSetupKey() + if err != nil { + return err + } + loginRequest := proto.LoginRequest{ - SetupKey: setupKey, + SetupKey: providedSetupKey, ManagementUrl: managementURL, AdminURL: adminURL, NatExternalIPs: natExternalIPs,