Skip to content

Commit b07874a

Browse files
zepatrikaeneasr
authored andcommitted
fix: correctly determine project and workspace IDs
1 parent 81ecdfd commit b07874a

File tree

9 files changed

+417
-68
lines changed

9 files changed

+417
-68
lines changed

cmd/cloudx/client/api_key.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"errors"
99
"fmt"
10-
"os"
1110

1211
cloud "github.com/ory/client-go"
1312
"github.com/ory/x/cmdx"
@@ -66,8 +65,8 @@ func (h *CommandHelper) DeleteWorkspaceAPIKey(ctx context.Context, workspaceID,
6665
}
6766

6867
func (h *CommandHelper) TemporaryAPIKey(ctx context.Context, name string) (apiKey string, cleanup func() error, err error) {
69-
if ak := os.Getenv(ProjectAPIKey); len(ak) > 0 {
70-
return ak, noop, nil
68+
if h.projectAPIKey != nil {
69+
return *h.projectAPIKey, noop, nil
7170
}
7271

7372
// For all other projects, except the playground, we need to authenticate.

cmd/cloudx/client/command_helper.go

Lines changed: 117 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,16 @@ var ErrProjectNotSet = fmt.Errorf("no project was specified")
3939

4040
type (
4141
CommandHelper struct {
42-
config *Config
43-
projectOverride *string
44-
workspaceOverride *string
45-
configLocation string
46-
noConfirm bool
47-
isQuiet bool
48-
VerboseErrWriter io.Writer
49-
Stdin *bufio.Reader
50-
openBrowserHook func(string) error
51-
projectAPIKey *string
52-
workspaceAPIKey *string
42+
config *Config
43+
projectOverride, workspaceOverride,
44+
projectAPIKey, workspaceAPIKey,
45+
cloudConsoleAPIURL *string
46+
projectID, workspaceID uuid.UUID
47+
configLocation string
48+
noConfirm, isQuiet bool
49+
VerboseErrWriter io.Writer
50+
Stdin *bufio.Reader
51+
openBrowserHook func(string) error
5352
}
5453
helperOptionsContextKey struct{}
5554
CommandHelperOption func(*CommandHelper)
@@ -180,70 +179,130 @@ func NewCommandHelper(ctx context.Context, opts ...CommandHelperOption) (*Comman
180179
return nil, err
181180
}
182181

183-
getAPIKey := func(envKey string, override *string) *string {
184-
if override != nil {
185-
return override
182+
if h.workspaceAPIKey == nil {
183+
if key, ok := os.LookupEnv(WorkspaceAPIKey); ok {
184+
h.workspaceAPIKey = &key
186185
}
187-
if key, ok := os.LookupEnv(envKey); ok {
188-
return &key
186+
}
187+
if h.projectAPIKey == nil {
188+
if key, ok := os.LookupEnv(ProjectAPIKey); ok {
189+
h.projectAPIKey = &key
189190
}
190-
return nil
191191
}
192-
h.workspaceAPIKey = getAPIKey(WorkspaceAPIKey, h.workspaceAPIKey)
193-
h.projectAPIKey = getAPIKey(ProjectAPIKey, h.projectAPIKey)
194192

195-
{
196-
// determine current workspace from all possible sources
197-
workspace := ""
193+
if err := h.determineWorkspaceID(ctx, config); err != nil {
194+
return nil, err
195+
}
196+
if err := h.determineProjectID(ctx, config); err != nil {
197+
return nil, err
198+
}
199+
200+
return h, nil
201+
}
202+
203+
func (h *CommandHelper) determineWorkspaceID(ctx context.Context, config *Config) error {
204+
if h.workspaceAPIKey != nil {
198205
if h.workspaceOverride != nil {
199-
workspace = *h.workspaceOverride
200-
} else if ws, ok := os.LookupEnv(WorkspaceKey); ok {
201-
workspace = ws
202-
} else {
203-
if config.SelectedWorkspace != uuid.Nil {
204-
workspace = config.SelectedWorkspace.String()
205-
}
206+
return errors.New("workspace API key is set but workspace flag is also set, please remove one")
206207
}
207-
workspace = strings.TrimSpace(workspace)
208-
209-
if id, err := uuid.FromString(workspace); err == nil {
210-
h.workspaceOverride = pointerx.Ptr(id.String())
211-
} else if workspace != "" {
212-
ws, err := h.findWorkspace(ctx, workspace)
208+
ws, err := h.ListWorkspaces(ctx)
209+
if err != nil {
210+
return err
211+
}
212+
if len(ws) > 0 {
213+
h.workspaceID, err = uuid.FromString(ws[0].Id)
213214
if err != nil {
214-
return nil, err
215-
}
216-
if ws != nil {
217-
h.workspaceOverride = pointerx.Ptr(ws.Id)
215+
return fmt.Errorf("unable to parse workspace ID from response: %w", err)
218216
}
217+
return nil
219218
}
219+
return errors.New("workspace API key is set but no workspaces were found")
220+
}
221+
222+
workspace := ""
223+
if h.workspaceOverride != nil {
224+
workspace = *h.workspaceOverride
225+
} else if ws, ok := os.LookupEnv(WorkspaceKey); ok {
226+
workspace = ws
227+
} else if config.SelectedWorkspace != uuid.Nil {
228+
h.workspaceID = config.SelectedWorkspace
229+
return nil
230+
}
231+
workspace = strings.TrimSpace(workspace)
232+
if workspace == "" {
233+
return nil
234+
}
235+
236+
// At this point, we have a (partial) workspace ID or name.
237+
if id, err := uuid.FromString(workspace); err == nil {
238+
h.workspaceID = id
239+
return nil
240+
}
241+
242+
// We need to resolve the non-UUID identifier to the workspace ID.
243+
ws, err := h.findWorkspace(ctx, workspace)
244+
if err != nil {
245+
return err
246+
}
247+
h.workspaceID, err = uuid.FromString(ws.Id)
248+
if err != nil {
249+
return fmt.Errorf("unable to parse workspace ID from response: %w", err)
220250
}
221-
{
222-
// determine current project from all possible sources
223-
project := ""
251+
return nil
252+
}
253+
254+
func (h *CommandHelper) determineProjectID(ctx context.Context, config *Config) error {
255+
if h.projectAPIKey != nil {
224256
if h.projectOverride != nil {
225-
project = *h.projectOverride
226-
} else if pj, ok := os.LookupEnv(ProjectKey); ok {
227-
project = pj
228-
} else if config.SelectedProject != uuid.Nil {
229-
project = config.SelectedProject.String()
257+
return errors.New("project API key is set but project flag is also set, please remove one")
230258
}
231-
project = strings.TrimSpace(project)
232-
233-
if id, err := uuid.FromString(project); err == nil {
234-
h.projectOverride = pointerx.Ptr(id.String())
235-
} else if project != "" {
236-
pj, err := h.findProject(ctx, project, h.workspaceOverride)
259+
if h.workspaceID != uuid.Nil {
260+
return errors.New("project API key is set but workspace is also set, please remove one")
261+
}
262+
pjs, err := h.ListProjects(ctx, nil)
263+
if err != nil {
264+
return err
265+
}
266+
if len(pjs) > 0 {
267+
h.projectID, err = uuid.FromString(pjs[0].Id)
237268
if err != nil {
238-
return nil, err
239-
}
240-
if pj != nil {
241-
h.projectOverride = pointerx.Ptr(pj.Id)
269+
return fmt.Errorf("unable to parse project ID from response: %w", err)
242270
}
271+
return nil
243272
}
273+
return errors.New("project API key is set but no projects were found")
244274
}
245275

246-
return h, nil
276+
project := ""
277+
if h.projectOverride != nil {
278+
project = *h.projectOverride
279+
} else if pj, ok := os.LookupEnv(ProjectKey); ok {
280+
project = pj
281+
} else if config.SelectedProject != uuid.Nil {
282+
h.projectID = config.SelectedProject
283+
return nil
284+
}
285+
project = strings.TrimSpace(project)
286+
if project == "" {
287+
return nil
288+
}
289+
290+
// At this point, we have a (partial) project ID or slug.
291+
if id, err := uuid.FromString(project); err == nil {
292+
h.projectID = id
293+
return nil
294+
}
295+
296+
// We need to resolve the non-UUID identifier to the project ID.
297+
pj, err := h.findProject(ctx, project, h.workspaceOverride)
298+
if err != nil {
299+
return err
300+
}
301+
h.projectID, err = uuid.FromString(pj.Id)
302+
if err != nil {
303+
return fmt.Errorf("unable to parse project ID from response: %w", err)
304+
}
305+
return nil
247306
}
248307

249308
func (h *CommandHelper) ProjectID() (string, error) {

0 commit comments

Comments
 (0)