|
5 | 5 | "fmt" |
6 | 6 | "strings" |
7 | 7 |
|
| 8 | + "github.com/UpCloudLtd/progress/messages" |
8 | 9 | "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands" |
| 10 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/account/tokenreceiver" |
9 | 11 | "github.com/UpCloudLtd/upcloud-cli/v3/internal/config" |
10 | 12 | "github.com/UpCloudLtd/upcloud-cli/v3/internal/output" |
11 | 13 | "github.com/spf13/pflag" |
@@ -47,6 +49,46 @@ func (s *loginCommand) ExecuteWithoutArguments(exec commands.Executor) (output.O |
47 | 49 | return s.executeWithToken(exec) |
48 | 50 | } |
49 | 51 |
|
| 52 | + return s.execute(exec) |
| 53 | +} |
| 54 | + |
| 55 | +func (s *loginCommand) execute(exec commands.Executor) (output.Output, error) { |
| 56 | + msg := "Waiting to receive token from browser." |
| 57 | + exec.PushProgressStarted(msg) |
| 58 | + |
| 59 | + receiver := tokenreceiver.New() |
| 60 | + err := receiver.Start() |
| 61 | + if err != nil { |
| 62 | + return commands.HandleError(exec, msg, err) |
| 63 | + } |
| 64 | + |
| 65 | + err = receiver.OpenBrowser() |
| 66 | + if err != nil { |
| 67 | + url := receiver.GetLoginURL() |
| 68 | + exec.PushProgressUpdate(messages.Update{ |
| 69 | + Message: "Failed to open browser.", |
| 70 | + Status: messages.MessageStatusError, |
| 71 | + Details: fmt.Sprintf("Please open a browser and navigate to %s to continue with the login.", url), |
| 72 | + }) |
| 73 | + } |
| 74 | + |
| 75 | + token, err := receiver.Wait(exec.Context()) |
| 76 | + if err != nil { |
| 77 | + return commands.HandleError(exec, msg, err) |
| 78 | + } |
| 79 | + |
| 80 | + exec.PushProgressUpdate(messages.Update{ |
| 81 | + Key: msg, |
| 82 | + Message: "Saving created token to the system keyring.", |
| 83 | + }) |
| 84 | + |
| 85 | + err = config.SaveTokenToKeyring(token) |
| 86 | + if err != nil { |
| 87 | + return commands.HandleError(exec, msg, fmt.Errorf("failed to save token to keyring: %w", err)) |
| 88 | + } |
| 89 | + |
| 90 | + exec.PushProgressSuccess(msg) |
| 91 | + |
50 | 92 | return output.None{}, nil |
51 | 93 | } |
52 | 94 |
|
|
0 commit comments