Skip to content

Commit

Permalink
Minor cleanups
Browse files Browse the repository at this point in the history
* ctx as first arg
* apply code formatter
* refactor error/result at Run()
  • Loading branch information
DavidS-ovm committed Apr 5, 2024
1 parent 9cc4828 commit 7c5d411
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7c5d411

Please sign in to comment.