-
-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allow using only project API keys #384
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8a258ff
test: drop analytics and other scripts
zepatrik b1318e8
feat: allow using only project API keys
zepatrik 36784b7
fix: correctly determine project and workspace IDs
zepatrik cfab7b3
fix: use correct field internally
zepatrik 8145618
test: update command usage
zepatrik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,17 +39,16 @@ var ErrProjectNotSet = fmt.Errorf("no project was specified") | |
|
||
type ( | ||
CommandHelper struct { | ||
config *Config | ||
projectOverride *string | ||
workspaceOverride *string | ||
configLocation string | ||
noConfirm bool | ||
isQuiet bool | ||
VerboseErrWriter io.Writer | ||
Stdin *bufio.Reader | ||
openBrowserHook func(string) error | ||
projectAPIKey *string | ||
workspaceAPIKey *string | ||
config *Config | ||
projectOverride, workspaceOverride, | ||
projectAPIKey, workspaceAPIKey, | ||
cloudConsoleAPIURL *string | ||
projectID, workspaceID uuid.UUID | ||
configLocation string | ||
noConfirm, isQuiet bool | ||
VerboseErrWriter io.Writer | ||
Stdin *bufio.Reader | ||
openBrowserHook func(string) error | ||
} | ||
helperOptionsContextKey struct{} | ||
CommandHelperOption func(*CommandHelper) | ||
|
@@ -147,7 +146,11 @@ func NewCobraCommandHelper(cmd *cobra.Command, opts ...CommandHelperOption) (*Co | |
if config := flagx.MustGetString(cmd, FlagConfig); config != "" { | ||
defaultOpts = append(defaultOpts, WithConfigLocation(config)) | ||
} | ||
return NewCommandHelper(cmd.Context(), append(defaultOpts, opts...)...) | ||
h, err := NewCommandHelper(cmd.Context(), append(defaultOpts, opts...)...) | ||
if err != nil { | ||
return nil, cmdx.PrintOpenAPIError(cmd, err) | ||
} | ||
return h, nil | ||
} | ||
|
||
func NewCommandHelper(ctx context.Context, opts ...CommandHelperOption) (*CommandHelper, error) { | ||
|
@@ -180,81 +183,144 @@ func NewCommandHelper(ctx context.Context, opts ...CommandHelperOption) (*Comman | |
return nil, err | ||
} | ||
|
||
getAPIKey := func(envKey string, override *string) *string { | ||
if override != nil { | ||
return override | ||
if h.workspaceAPIKey == nil { | ||
if key, ok := os.LookupEnv(WorkspaceAPIKey); ok { | ||
h.workspaceAPIKey = &key | ||
} | ||
if key, ok := os.LookupEnv(envKey); ok { | ||
return &key | ||
} | ||
if h.projectAPIKey == nil { | ||
if key, ok := os.LookupEnv(ProjectAPIKey); ok { | ||
h.projectAPIKey = &key | ||
} | ||
return nil | ||
} | ||
h.workspaceAPIKey = getAPIKey(WorkspaceAPIKey, h.workspaceAPIKey) | ||
h.projectAPIKey = getAPIKey(ProjectAPIKey, h.projectAPIKey) | ||
|
||
{ | ||
// determine current workspace from all possible sources | ||
workspace := "" | ||
if err := h.determineWorkspaceID(ctx, config); err != nil { | ||
return nil, err | ||
} | ||
if err := h.determineProjectID(ctx, config); err != nil { | ||
return nil, err | ||
} | ||
|
||
return h, nil | ||
} | ||
|
||
func (h *CommandHelper) determineWorkspaceID(ctx context.Context, config *Config) error { | ||
if h.workspaceAPIKey != nil { | ||
if h.workspaceOverride != nil { | ||
workspace = *h.workspaceOverride | ||
} else if ws, ok := os.LookupEnv(WorkspaceKey); ok { | ||
workspace = ws | ||
} else { | ||
if config.SelectedWorkspace != uuid.Nil { | ||
workspace = config.SelectedWorkspace.String() | ||
} | ||
return errors.New("workspace API key is set but workspace flag is also set, please remove one") | ||
} | ||
workspace = strings.TrimSpace(workspace) | ||
|
||
if id, err := uuid.FromString(workspace); err == nil { | ||
h.workspaceOverride = pointerx.Ptr(id.String()) | ||
} else if workspace != "" { | ||
ws, err := h.findWorkspace(ctx, workspace) | ||
ws, err := h.ListWorkspaces(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
if len(ws) > 0 { | ||
h.workspaceID, err = uuid.FromString(ws[0].Id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if ws != nil { | ||
h.workspaceOverride = pointerx.Ptr(ws.Id) | ||
return fmt.Errorf("unable to parse workspace ID from response: %w", err) | ||
} | ||
return nil | ||
} | ||
return errors.New("workspace API key is set but no workspaces were found") | ||
} | ||
Comment on lines
+208
to
+224
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the new stuff for workspace API keys. |
||
|
||
workspace := "" | ||
if h.workspaceOverride != nil { | ||
workspace = *h.workspaceOverride | ||
} else if ws, ok := os.LookupEnv(WorkspaceKey); ok { | ||
workspace = ws | ||
} else if config.SelectedWorkspace != uuid.Nil { | ||
h.workspaceID = config.SelectedWorkspace | ||
return nil | ||
} | ||
workspace = strings.TrimSpace(workspace) | ||
if workspace == "" { | ||
return nil | ||
} | ||
{ | ||
// determine current project from all possible sources | ||
project := "" | ||
|
||
// At this point, we have a (partial) workspace ID or name. | ||
if id, err := uuid.FromString(workspace); err == nil { | ||
h.workspaceID = id | ||
return nil | ||
} | ||
|
||
// We need to resolve the non-UUID identifier to the workspace ID. | ||
ws, err := h.findWorkspace(ctx, workspace) | ||
if err != nil { | ||
return err | ||
} | ||
h.workspaceID, err = uuid.FromString(ws.Id) | ||
if err != nil { | ||
return fmt.Errorf("unable to parse workspace ID from response: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (h *CommandHelper) determineProjectID(ctx context.Context, config *Config) error { | ||
if h.projectAPIKey != nil { | ||
if h.projectOverride != nil { | ||
project = *h.projectOverride | ||
} else if pj, ok := os.LookupEnv(ProjectKey); ok { | ||
project = pj | ||
} else if config.SelectedProject != uuid.Nil { | ||
project = config.SelectedProject.String() | ||
return errors.New("project API key is set but project flag is also set, please remove one") | ||
} | ||
project = strings.TrimSpace(project) | ||
|
||
if id, err := uuid.FromString(project); err == nil { | ||
h.projectOverride = pointerx.Ptr(id.String()) | ||
} else if project != "" { | ||
pj, err := h.findProject(ctx, project, h.workspaceOverride) | ||
if h.workspaceID != uuid.Nil { | ||
return errors.New("project API key is set but workspace is also set, please remove one") | ||
} | ||
pjs, err := h.ListProjects(ctx, nil) | ||
if err != nil { | ||
return err | ||
} | ||
if len(pjs) > 0 { | ||
h.projectID, err = uuid.FromString(pjs[0].Id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if pj != nil { | ||
h.projectOverride = pointerx.Ptr(pj.Id) | ||
return fmt.Errorf("unable to parse project ID from response: %w", err) | ||
} | ||
return nil | ||
} | ||
return errors.New("project API key is set but no projects were found") | ||
} | ||
Comment on lines
+259
to
278
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the new stuff for project API keys. |
||
|
||
return h, nil | ||
project := "" | ||
if h.projectOverride != nil { | ||
project = *h.projectOverride | ||
} else if pj, ok := os.LookupEnv(ProjectKey); ok { | ||
project = pj | ||
} else if config.SelectedProject != uuid.Nil { | ||
h.projectID = config.SelectedProject | ||
return nil | ||
} | ||
project = strings.TrimSpace(project) | ||
if project == "" { | ||
return nil | ||
} | ||
|
||
// At this point, we have a (partial) project ID or slug. | ||
if id, err := uuid.FromString(project); err == nil { | ||
h.projectID = id | ||
return nil | ||
} | ||
|
||
// We need to resolve the non-UUID identifier to the project ID. | ||
pj, err := h.findProject(ctx, project, h.workspaceOverride) | ||
if err != nil { | ||
return err | ||
} | ||
h.projectID, err = uuid.FromString(pj.Id) | ||
if err != nil { | ||
return fmt.Errorf("unable to parse project ID from response: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (h *CommandHelper) ProjectID() (string, error) { | ||
if h.projectOverride == nil { | ||
if h.projectID == uuid.Nil { | ||
return "", ErrProjectNotSet | ||
} | ||
return *h.projectOverride, nil | ||
return h.projectID.String(), nil | ||
} | ||
|
||
func (h *CommandHelper) WorkspaceID() *string { | ||
return h.workspaceOverride | ||
if h.workspaceID == uuid.Nil { | ||
return nil | ||
} | ||
return pointerx.Ptr(h.workspaceID.String()) | ||
} | ||
|
||
func (h *CommandHelper) UserName(ctx context.Context) string { | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, but this was just running the same test as
test-e2e/test-proxy
?