Skip to content

Commit

Permalink
Fix whoami command (#131)
Browse files Browse the repository at this point in the history
* Regenerate types to add user object

* Use user endpoint for whoami
  • Loading branch information
jakemalachowski authored Nov 16, 2024
1 parent faddb84 commit cb243bc
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 31 deletions.
23 changes: 4 additions & 19 deletions cmd/whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"fmt"

"github.com/renderinc/cli/pkg/user"
"github.com/spf13/cobra"

"github.com/renderinc/cli/pkg/client"
"github.com/renderinc/cli/pkg/owner"
)

var whoamiCmd = &cobra.Command{
Expand All @@ -25,28 +25,13 @@ func runWhoami(ctx context.Context) error {
return fmt.Errorf("failed to create client: %w", err)
}

ownerRepo := owner.NewRepo(c)

owners, err := ownerRepo.ListOwners(ctx, owner.ListInput{})
userRepo := user.NewRepo(c)
currentUser, err := userRepo.CurrentUser(ctx)
if err != nil {
return fmt.Errorf("failed to list owners: %w", err)
}

var currentUser *client.Owner
// an owner list response will always have exactly one owner of type user
for _, o := range owners {
if o.Type == client.OwnerTypeUser {
currentUser = o
break
}
}

if currentUser == nil {
return fmt.Errorf("no user found in the list of owners")
return fmt.Errorf("failed to get current user: %w", err)
}

fmt.Printf("Name: %s\n", currentUser.Name)
fmt.Printf("ID: %s\n", currentUser.Id)
fmt.Printf("Email: %s\n", currentUser.Email)

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/autoscaling/autoscaling_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/client/blueprints/blueprints_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

144 changes: 143 additions & 1 deletion pkg/client/client_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/client/disks/disks_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/client/events/events_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/client/jobs/jobs_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/client/logs/logs_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/client/maintenance/maintenance_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/client/metrics/metrics_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/client/notifications/notifications_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/client/postgres/postgres_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion pkg/client/types_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions pkg/user/repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package user

import (
"context"

"github.com/renderinc/cli/pkg/client"
)

type Repo struct {
client *client.ClientWithResponses
}

func NewRepo(client *client.ClientWithResponses) *Repo {
return &Repo{client: client}
}

func (r *Repo) CurrentUser(ctx context.Context) (*client.User, error) {
resp, err := r.client.GetUserWithResponse(ctx)
if err != nil {
return nil, err
}

if err := client.ErrorFromResponse(resp); err != nil {
return nil, err
}

return resp.JSON200, nil
}

0 comments on commit cb243bc

Please sign in to comment.