From 7c5d41151e4592664191227788863800f31e95c5 Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Fri, 5 Apr 2024 11:59:06 +0200 Subject: [PATCH] Minor cleanups * ctx as first arg * apply code formatter * refactor error/result at Run() --- cmd/root.go | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index d44eef4c..cd2f487e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -283,11 +283,12 @@ const ( ) type authenticateModel struct { + ctx context.Context + status statusMsg err error deviceCode *oauth2.DeviceAuthResponse config oauth2.Config - ctx context.Context token *oauth2.Token } @@ -323,16 +324,17 @@ func (m authenticateModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, openBrowser(m.deviceCode.VerificationURI) case WaitingForConfirmation: m.status = WaitingForConfirmation - return m, awaitToken(m.config, m.ctx, m.deviceCode) + return m, awaitToken(m.ctx, m.config, m.deviceCode) case Authenticated: - case ErrorAuthenticating: { - return m, nil + case ErrorAuthenticating: + { + return m, nil + } } - } case browserOpenErrorMsg: m.status = WaitingForConfirmation - return m, awaitToken(m.config, m.ctx, m.deviceCode) + return m, awaitToken(m.ctx, m.config, m.deviceCode) case failedToAuthenticateErrorMsg: m.err = msg.err @@ -355,7 +357,7 @@ Attempting to automatically open the SSO authorization page in your default brow If the browser does not open or you wish to use a different device to authorize this request, open the following URL: %v - + Then enter the code: %v @@ -391,7 +393,7 @@ func openBrowser(url string) tea.Cmd { } } -func awaitToken(config oauth2.Config, ctx context.Context, deviceCode *oauth2.DeviceAuthResponse) tea.Cmd { +func awaitToken(ctx context.Context, config oauth2.Config, deviceCode *oauth2.DeviceAuthResponse) tea.Cmd { return func() tea.Msg { token, err := config.DeviceAccessToken(ctx, deviceCode) if err != nil { @@ -460,19 +462,19 @@ func getOauthToken(ctx context.Context, oi OvermindInstance, requiredScopes []st return nil, fmt.Errorf("error getting device code: %w", err) } - m := authenticateModel{status: PromptUser, deviceCode: deviceCode, config: config, ctx: ctx} + m := authenticateModel{ctx: ctx, status: PromptUser, deviceCode: deviceCode, config: config} authenticateProgram := tea.NewProgram(m) - if result, err := authenticateProgram.Run(); err != nil { + result, err := authenticateProgram.Run() + if err != nil { fmt.Println("Error running program:", err) os.Exit(1) - } else { - updatedModel, ok := result.(authenticateModel) - if !ok { - fmt.Println("Error running program: result is not authenticateModel") - os.Exit(1) - } - m = updatedModel + } + + m, ok := result.(authenticateModel) + if !ok { + fmt.Println("Error running program: result is not authenticateModel") + os.Exit(1) } span := trace.SpanFromContext(ctx)