|
| 1 | +package account |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands" |
| 9 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/config" |
| 10 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/output" |
| 11 | + "github.com/spf13/pflag" |
| 12 | +) |
| 13 | + |
| 14 | +// LoginCommand creates the "account login" command |
| 15 | +func LoginCommand() commands.Command { |
| 16 | + return &loginCommand{ |
| 17 | + BaseCommand: commands.New( |
| 18 | + "login", |
| 19 | + "Configure an authentication token to the system keyring.", |
| 20 | + "upctl account login --with-token", |
| 21 | + ), |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +type loginCommand struct { |
| 26 | + *commands.BaseCommand |
| 27 | + |
| 28 | + withToken config.OptionalBoolean |
| 29 | +} |
| 30 | + |
| 31 | +// InitCommand implements Command.InitCommand |
| 32 | +func (s *loginCommand) InitCommand() { |
| 33 | + fs := &pflag.FlagSet{} |
| 34 | + config.AddToggleFlag(fs, &s.withToken, "with-token", false, "Read token from standard input.") |
| 35 | + s.AddFlags(fs) |
| 36 | + |
| 37 | + // Require the with-token flag until we support using browser to authenticate. |
| 38 | + commands.Must(s.Cobra().MarkFlagRequired("with-token")) |
| 39 | +} |
| 40 | + |
| 41 | +// DoesNotUseServices implements commands.OfflineCommand as this command does not use services |
| 42 | +func (s *loginCommand) DoesNotUseServices() {} |
| 43 | + |
| 44 | +// ExecuteWithoutArguments implements commands.NoArgumentCommand |
| 45 | +func (s *loginCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) { |
| 46 | + if s.withToken.Value() { |
| 47 | + return s.executeWithToken(exec) |
| 48 | + } |
| 49 | + |
| 50 | + return output.None{}, nil |
| 51 | +} |
| 52 | + |
| 53 | +func (s *loginCommand) executeWithToken(exec commands.Executor) (output.Output, error) { |
| 54 | + in := bufio.NewReader(s.Cobra().InOrStdin()) |
| 55 | + token, err := in.ReadString('\n') |
| 56 | + if err != nil { |
| 57 | + return nil, fmt.Errorf("failed to read token from standard input: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + msg := "Saving provided token to the system keyring." |
| 61 | + exec.PushProgressStarted(msg) |
| 62 | + err = config.SaveToken(strings.TrimSpace(token)) |
| 63 | + if err != nil { |
| 64 | + return commands.HandleError(exec, msg, err) |
| 65 | + } |
| 66 | + |
| 67 | + exec.PushProgressSuccess(msg) |
| 68 | + |
| 69 | + return output.None{}, nil |
| 70 | +} |
0 commit comments