From 8445a7546ee81c7c6503401127bbc7b25cb14fee Mon Sep 17 00:00:00 2001 From: jmug Date: Sat, 10 May 2025 14:26:20 -0700 Subject: [PATCH] Allow registering device without requiring the creation of the config file. Signed-off-by: jmug --- cmd/cli/registerDevice.go | 13 ++++++++++--- internal/configfile/config.go | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/cmd/cli/registerDevice.go b/cmd/cli/registerDevice.go index 4d7a911..eb52387 100644 --- a/cmd/cli/registerDevice.go +++ b/cmd/cli/registerDevice.go @@ -12,6 +12,7 @@ import ( func init() { rootCmd.AddCommand(registerDeviceCommand) + registerDeviceCommand.Flags().StringVarP(&email, "email", "e", "", "email is your login identifier, ignored if your config file has has an email") } var registerDeviceCommand = &cobra.Command{ @@ -23,8 +24,11 @@ var registerDeviceCommand = &cobra.Command{ return err } - if cc.Email == "" { - return errors.New("you don't have an account configured for thist device") + if cc.Email != "" { + email = cc.Email + } + if email == "" { + return errors.New("provide an email through your config file or with --email") } if cc.DeviceId != "" { @@ -35,7 +39,7 @@ var registerDeviceCommand = &cobra.Command{ pbk := pvk.PublicKey() password := input.ReadPassword() - res, err := apiclient.RegisterDevice(cc.Email, password, pbk.Bytes()) + res, err := apiclient.RegisterDevice(email, password, pbk.Bytes()) if err != nil { return err } @@ -43,6 +47,9 @@ var registerDeviceCommand = &cobra.Command{ // Write the key files first, if those fail to write then we should not // save the device Id. cc.DeviceId = res.DeviceID + if cc.Email == "" { + cc.Email = email + } err = configfile.SavePrivateKey(pvk) if err != nil { return err diff --git a/internal/configfile/config.go b/internal/configfile/config.go index 4e927c5..53c3796 100644 --- a/internal/configfile/config.go +++ b/internal/configfile/config.go @@ -22,7 +22,7 @@ type ConfigFile struct { var Path string func EnsureAndGet() (ConfigFile, error) { - err := os.MkdirAll(Path, os.FileMode(int(0660))) + err := os.MkdirAll(Path, os.FileMode(int(0770))) if err != nil { return ConfigFile{}, fmt.Errorf("could not create config directory: %w", err) } @@ -38,7 +38,7 @@ func EnsureAndGet() (ConfigFile, error) { } func Write(c ConfigFile) error { - err := os.MkdirAll(Path, os.FileMode(int(0660))) + err := os.MkdirAll(Path, os.FileMode(int(0770))) if err != nil { return err }