diff --git a/cmd/whoami.go b/cmd/whoami.go index 4121fef..8b902f4 100644 --- a/cmd/whoami.go +++ b/cmd/whoami.go @@ -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{ @@ -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 diff --git a/pkg/client/autoscaling/autoscaling_gen.go b/pkg/client/autoscaling/autoscaling_gen.go index a0bf521..0f6e442 100644 --- a/pkg/client/autoscaling/autoscaling_gen.go +++ b/pkg/client/autoscaling/autoscaling_gen.go @@ -3,7 +3,7 @@ // Package client provides primitives to interact with the openapi HTTP API. // -// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.1.0 DO NOT EDIT. +// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT. package client // AutoscalingConfig defines model for autoscalingConfig. diff --git a/pkg/client/blueprints/blueprints_gen.go b/pkg/client/blueprints/blueprints_gen.go index 6f7cb10..68c93df 100644 --- a/pkg/client/blueprints/blueprints_gen.go +++ b/pkg/client/blueprints/blueprints_gen.go @@ -3,7 +3,7 @@ // Package client provides primitives to interact with the openapi HTTP API. // -// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.1.0 DO NOT EDIT. +// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT. package client import ( diff --git a/pkg/client/client_gen.go b/pkg/client/client_gen.go index dafb02a..32eb001 100644 --- a/pkg/client/client_gen.go +++ b/pkg/client/client_gen.go @@ -3,7 +3,7 @@ // Package client provides primitives to interact with the openapi HTTP API. // -// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.1.0 DO NOT EDIT. +// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT. package client import ( @@ -630,6 +630,9 @@ type ClientInterface interface { // SuspendService request SuspendService(ctx context.Context, serviceId ServiceIdParam, reqEditors ...RequestEditorFn) (*http.Response, error) + + // GetUser request + GetUser(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) } func (c *Client) ListBlueprints(ctx context.Context, params *ListBlueprintsParams, reqEditors ...RequestEditorFn) (*http.Response, error) { @@ -2924,6 +2927,18 @@ func (c *Client) SuspendService(ctx context.Context, serviceId ServiceIdParam, r return c.Client.Do(req) } +func (c *Client) GetUser(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewGetUserRequest(c.Server) + if err != nil { + return nil, err + } + req = req.WithContext(ctx) + if err := c.applyEditors(ctx, req, reqEditors); err != nil { + return nil, err + } + return c.Client.Do(req) +} + // NewListBlueprintsRequest generates requests for ListBlueprints func NewListBlueprintsRequest(server string, params *ListBlueprintsParams) (*http.Request, error) { var err error @@ -13283,6 +13298,33 @@ func NewSuspendServiceRequest(server string, serviceId ServiceIdParam) (*http.Re return req, nil } +// NewGetUserRequest generates requests for GetUser +func NewGetUserRequest(server string) (*http.Request, error) { + var err error + + serverURL, err := url.Parse(server) + if err != nil { + return nil, err + } + + operationPath := fmt.Sprintf("/users") + if operationPath[0] == '/' { + operationPath = "." + operationPath + } + + queryURL, err := serverURL.Parse(operationPath) + if err != nil { + return nil, err + } + + req, err := http.NewRequest("GET", queryURL.String(), nil) + if err != nil { + return nil, err + } + + return req, nil +} + func (c *Client) applyEditors(ctx context.Context, req *http.Request, additionalEditors []RequestEditorFn) error { for _, r := range c.RequestEditors { if err := r(ctx, req); err != nil { @@ -13855,6 +13897,9 @@ type ClientWithResponsesInterface interface { // SuspendServiceWithResponse request SuspendServiceWithResponse(ctx context.Context, serviceId ServiceIdParam, reqEditors ...RequestEditorFn) (*SuspendServiceResponse, error) + + // GetUserWithResponse request + GetUserWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*GetUserResponse, error) } type ListBlueprintsResponse struct { @@ -18079,6 +18124,33 @@ func (r SuspendServiceResponse) StatusCode() int { return 0 } +type GetUserResponse struct { + Body []byte + HTTPResponse *http.Response + JSON200 *User + JSON401 *N401Unauthorized + JSON406 *N406NotAcceptable + JSON429 *N429RateLimit + JSON500 *N500InternalServerError + JSON503 *N503ServiceUnavailable +} + +// Status returns HTTPResponse.Status +func (r GetUserResponse) Status() string { + if r.HTTPResponse != nil { + return r.HTTPResponse.Status + } + return http.StatusText(0) +} + +// StatusCode returns HTTPResponse.StatusCode +func (r GetUserResponse) StatusCode() int { + if r.HTTPResponse != nil { + return r.HTTPResponse.StatusCode + } + return 0 +} + // ListBlueprintsWithResponse request returning *ListBlueprintsResponse func (c *ClientWithResponses) ListBlueprintsWithResponse(ctx context.Context, params *ListBlueprintsParams, reqEditors ...RequestEditorFn) (*ListBlueprintsResponse, error) { rsp, err := c.ListBlueprints(ctx, params, reqEditors...) @@ -19755,6 +19827,15 @@ func (c *ClientWithResponses) SuspendServiceWithResponse(ctx context.Context, se return ParseSuspendServiceResponse(rsp) } +// GetUserWithResponse request returning *GetUserResponse +func (c *ClientWithResponses) GetUserWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*GetUserResponse, error) { + rsp, err := c.GetUser(ctx, reqEditors...) + if err != nil { + return nil, err + } + return ParseGetUserResponse(rsp) +} + // ParseListBlueprintsResponse parses an HTTP response from a ListBlueprintsWithResponse call func ParseListBlueprintsResponse(rsp *http.Response) (*ListBlueprintsResponse, error) { bodyBytes, err := io.ReadAll(rsp.Body) @@ -30340,3 +30421,64 @@ func ParseSuspendServiceResponse(rsp *http.Response) (*SuspendServiceResponse, e return response, nil } + +// ParseGetUserResponse parses an HTTP response from a GetUserWithResponse call +func ParseGetUserResponse(rsp *http.Response) (*GetUserResponse, error) { + bodyBytes, err := io.ReadAll(rsp.Body) + defer func() { _ = rsp.Body.Close() }() + if err != nil { + return nil, err + } + + response := &GetUserResponse{ + Body: bodyBytes, + HTTPResponse: rsp, + } + + switch { + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: + var dest User + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON200 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 401: + var dest N401Unauthorized + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON401 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 406: + var dest N406NotAcceptable + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON406 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 429: + var dest N429RateLimit + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON429 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 500: + var dest N500InternalServerError + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON500 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 503: + var dest N503ServiceUnavailable + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON503 = &dest + + } + + return response, nil +} diff --git a/pkg/client/disks/disks_gen.go b/pkg/client/disks/disks_gen.go index 5ae9053..06eb229 100644 --- a/pkg/client/disks/disks_gen.go +++ b/pkg/client/disks/disks_gen.go @@ -3,7 +3,7 @@ // Package client provides primitives to interact with the openapi HTTP API. // -// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.1.0 DO NOT EDIT. +// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT. package client import ( diff --git a/pkg/client/events/events_gen.go b/pkg/client/events/events_gen.go index bba1364..73e5590 100644 --- a/pkg/client/events/events_gen.go +++ b/pkg/client/events/events_gen.go @@ -3,7 +3,7 @@ // Package client provides primitives to interact with the openapi HTTP API. // -// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.1.0 DO NOT EDIT. +// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT. package client import ( diff --git a/pkg/client/jobs/jobs_gen.go b/pkg/client/jobs/jobs_gen.go index 2303ae6..35c320a 100644 --- a/pkg/client/jobs/jobs_gen.go +++ b/pkg/client/jobs/jobs_gen.go @@ -3,7 +3,7 @@ // Package client provides primitives to interact with the openapi HTTP API. // -// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.1.0 DO NOT EDIT. +// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT. package client import ( diff --git a/pkg/client/logs/logs_gen.go b/pkg/client/logs/logs_gen.go index 668bb27..3b01056 100644 --- a/pkg/client/logs/logs_gen.go +++ b/pkg/client/logs/logs_gen.go @@ -3,7 +3,7 @@ // Package client provides primitives to interact with the openapi HTTP API. // -// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.1.0 DO NOT EDIT. +// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT. package client import ( diff --git a/pkg/client/maintenance/maintenance_gen.go b/pkg/client/maintenance/maintenance_gen.go index 07f3d47..c9c4f92 100644 --- a/pkg/client/maintenance/maintenance_gen.go +++ b/pkg/client/maintenance/maintenance_gen.go @@ -3,7 +3,7 @@ // Package client provides primitives to interact with the openapi HTTP API. // -// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.1.0 DO NOT EDIT. +// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT. package client import ( diff --git a/pkg/client/metrics/metrics_gen.go b/pkg/client/metrics/metrics_gen.go index f7539de..6a6b9da 100644 --- a/pkg/client/metrics/metrics_gen.go +++ b/pkg/client/metrics/metrics_gen.go @@ -3,7 +3,7 @@ // Package client provides primitives to interact with the openapi HTTP API. // -// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.1.0 DO NOT EDIT. +// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT. package client import ( diff --git a/pkg/client/notifications/notifications_gen.go b/pkg/client/notifications/notifications_gen.go index b74419f..327afb3 100644 --- a/pkg/client/notifications/notifications_gen.go +++ b/pkg/client/notifications/notifications_gen.go @@ -3,7 +3,7 @@ // Package client provides primitives to interact with the openapi HTTP API. // -// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.1.0 DO NOT EDIT. +// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT. package client // Defines values for NotifyOverride. diff --git a/pkg/client/postgres/postgres_gen.go b/pkg/client/postgres/postgres_gen.go index ee5a4ce..7092158 100644 --- a/pkg/client/postgres/postgres_gen.go +++ b/pkg/client/postgres/postgres_gen.go @@ -3,7 +3,7 @@ // Package client provides primitives to interact with the openapi HTTP API. // -// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.1.0 DO NOT EDIT. +// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT. package client import ( diff --git a/pkg/client/types_gen.go b/pkg/client/types_gen.go index 7850dbf..a77abb2 100644 --- a/pkg/client/types_gen.go +++ b/pkg/client/types_gen.go @@ -3,7 +3,7 @@ // Package client provides primitives to interact with the openapi HTTP API. // -// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.1.0 DO NOT EDIT. +// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT. package client import ( @@ -1655,6 +1655,12 @@ type SyncWithCursor struct { Sync externalRef1.Sync `json:"sync"` } +// User defines model for user. +type User struct { + Email string `json:"email"` + Name string `json:"name"` +} + // WebServiceDetails defines model for webServiceDetails. type WebServiceDetails struct { Autoscaling *externalRef0.AutoscalingConfig `json:"autoscaling,omitempty"` diff --git a/pkg/user/repo.go b/pkg/user/repo.go new file mode 100644 index 0000000..65cf268 --- /dev/null +++ b/pkg/user/repo.go @@ -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 +}