Skip to content

Commit

Permalink
client/guest: nudge user to login for all priveliged actions
Browse files Browse the repository at this point in the history
  • Loading branch information
joshi4 committed Aug 23, 2024
1 parent c2d1cb6 commit 4e6d099
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions client/guest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
Expand Down Expand Up @@ -60,14 +59,20 @@ func (g *guest) WhoAmI(ctx context.Context) (string, error) {
return "Savvy Guest", nil
}

var ErrCannotUseGuest = errors.New("cannot use guest client for this operation")

func (g *guest) GenerateRunbookV2(ctx context.Context, commands []RecordedCommand) (*GeneratedRunbook, error) {
return nil, fmt.Errorf("%w: %v", ErrCannotUseGuest, "generate runbook")
cl, err := getLoggedInClient()
if err != nil {
return nil, err
}
return cl.GenerateRunbookV2(ctx, commands)
}

func (g *guest) GenerateRunbook(ctx context.Context, commands []string) (*GeneratedRunbook, error) {
return nil, fmt.Errorf("%w: %v", ErrCannotUseGuest, "generate runbook")
cl, err := getLoggedInClient()
if err != nil {
return nil, err
}
return cl.GenerateRunbook(ctx, commands)
}

func (g *guest) RunbookByID(ctx context.Context, id string) (*Runbook, error) {
Expand All @@ -87,6 +92,15 @@ func (g *guest) RunbookByID(ctx context.Context, id string) (*Runbook, error) {
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusUnauthorized {
cl, err := getLoggedInClient()
if err != nil {
return nil, fmt.Errorf("not authorized to view this runbook: %w", err)
}

return cl.RunbookByID(ctx, id)
}

if resp.StatusCode >= 400 {
var errResp ErrorResponse
if err := json.NewDecoder(resp.Body).Decode(&errResp); err != nil {
Expand All @@ -103,8 +117,7 @@ func (g *guest) RunbookByID(ctx context.Context, id string) (*Runbook, error) {
}

func (g *guest) Runbooks(ctx context.Context) ([]RunbookInfo, error) {
login.Run(VerifyLogin)
cl, err := New()
cl, err := getLoggedInClient()
if err != nil {
return nil, err
}
Expand All @@ -120,19 +133,31 @@ func (g *guest) Explain(ctx context.Context, code CodeInfo) (<-chan string, erro
}

func (g *guest) StepContentByStepID(ctx context.Context, stepID string) (*StepContent, error) {
login.Run(VerifyLogin)
cl, err := New()
cl, err := getLoggedInClient()
if err != nil {
return nil, err
}
return cl.StepContentByStepID(ctx, stepID)
}

func (g *guest) SaveRunbook(ctx context.Context, runbook *Runbook) (*GeneratedRunbook, error) {
login.Run(VerifyLogin)
cl, err := New()
cl, err := getLoggedInClient()
if err != nil {
return nil, err
}
return cl.SaveRunbook(ctx, runbook)
}

func getLoggedInClient() (Client, error) {
cl, err := New()
if err == nil {
return cl, nil
}

login.Run(VerifyLogin)
cl, err = New()
if err != nil {
return nil, err
}
return cl, nil
}

0 comments on commit 4e6d099

Please sign in to comment.