Skip to content

Commit fb83747

Browse files
committed
fix: allow to force setting a project even if it can't be listed
1 parent af4f7f6 commit fb83747

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

auth/set_project.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
type SetProjectCmd struct {
2020
format.Writer `hidden:""`
2121
Name string `arg:"" help:"Name of the default project to be used." completion-predictor:"project_name"`
22+
Force bool `flag:"force" help:"Force setting the project even if it is not found. Required for APIServiceAccounts in sub-projects and APIServiceAccounts in the organization project without organization access."`
2223
}
2324

2425
func (s *SetProjectCmd) Run(ctx context.Context, client *api.Client) error {
@@ -27,6 +28,10 @@ func (s *SetProjectCmd) Run(ctx context.Context, client *api.Client) error {
2728
return err
2829
}
2930

31+
if s.Force {
32+
return s.do(client)
33+
}
34+
3035
// Ensure the project exists. Try switching otherwise.
3136
if err := client.Get(
3237
ctx,
@@ -37,10 +42,7 @@ func (s *SetProjectCmd) Run(ctx context.Context, client *api.Client) error {
3742
return fmt.Errorf("failed to set project %s: %w", s.Name, err)
3843
}
3944

40-
s.Warningf("Project %q does not exist in organization %q, checking other organizations...\n",
41-
s.Name,
42-
org,
43-
)
45+
s.Warningf("Project %q does not exist in organization %q.\nChecking other organizations...\n", s.Name, org)
4446
if err := trySwitchOrg(ctx, client, s.Name); err != nil {
4547
return err
4648
}
@@ -51,18 +53,23 @@ func (s *SetProjectCmd) Run(ctx context.Context, client *api.Client) error {
5153
}
5254
}
5355

54-
if err := config.SetContextProject(
55-
client.KubeconfigPath,
56-
client.KubeconfigContext,
57-
s.Name,
58-
); err != nil {
56+
if err := s.do(client); err != nil {
5957
return err
6058
}
6159

6260
s.Successf("📝", "set active Project to %s in organization %s", s.Name, org)
6361
return nil
6462
}
6563

64+
// do sets the project.
65+
func (s *SetProjectCmd) do(client *api.Client) error {
66+
return config.SetContextProject(
67+
client.KubeconfigPath,
68+
client.KubeconfigContext,
69+
s.Name,
70+
)
71+
}
72+
6673
// trySwitchOrg attempts to find the organization containing the given project
6774
// and switches the current context to that organization.
6875
func trySwitchOrg(ctx context.Context, client *api.Client, project string) error {
@@ -136,5 +143,6 @@ func orgFromProject(ctx context.Context, client *api.Client, project string) (st
136143

137144
return "", cli.ErrorWithContext(fmt.Errorf("could not find project %q in any available organization", project)).
138145
WithExitCode(cli.ExitUsageError).
139-
WithSuggestions(format.Command().GetProjects())
146+
WithSuggestions("List all available projects:\n" + format.Command().GetProjects()).
147+
WithSuggestions("For APIServiceAccounts in sub-projects or without organizational access, you'll need to set the --force flag.")
140148
}

internal/format/print.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var spinnerCharset = yacspin.CharSets[24]
4646
// Progress is a formatted message for use with a spinner.Suffix. An
4747
// icon can be added which is displayed at the end of the message.
4848
func Progress(icon, message string) string {
49-
return fmt.Sprintf(" %s %s", message, icon)
49+
return Progressf(icon, "%s", message)
5050
}
5151

5252
// Progressf is a formatted message for use with a spinner.Suffix. An
@@ -80,8 +80,12 @@ func info(icon, message string) string {
8080
return fmt.Sprintf(" %s %s %s", InfoChar, message, icon)
8181
}
8282

83-
func warningf(msg string, a ...any) string {
84-
return fmt.Sprintf(color.YellowString("Warning: ")+msg, a...)
83+
func warningf(format string, a ...any) string {
84+
const prefix = "Warning: "
85+
msg := fmt.Sprintf(format, a...)
86+
padding := strings.Repeat(" ", len(prefix))
87+
msg = strings.ReplaceAll(msg, "\n", "\n"+padding)
88+
return color.YellowString(prefix) + msg
8589
}
8690

8791
// NewSpinner returns a new spinner with the default config

0 commit comments

Comments
 (0)