Skip to content

Commit 8db0ce3

Browse files
authored
fix(cli): add --workspace flags and safer multi-workspace defaults (#80)
AI agents and scripts need explicit workspace scoping. Add --workspace to project/server/environment/addons/token/gitops commands, wire a client-side workspace override, fall back project env fetch when scoped path 403s, and bump pipeops-go-sdk to v0.17.5.
1 parent 32dc1b7 commit 8db0ce3

18 files changed

Lines changed: 199 additions & 134 deletions

File tree

cmd/addons/deployments.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ var deploymentsCmd = &cobra.Command{
1010
1111
Examples:
1212
- List all addon deployments:
13-
pipeops addons deployments`,
13+
pipeops addons deployments
14+
pipeops addons deployments --workspace <workspace-uuid>`,
1415
Run: runAddonDeployments,
1516
Args: cobra.NoArgs,
1617
}
1718

1819
func init() {
20+
deploymentsCmd.Flags().String("workspace", "", "Workspace UUID (or set PIPEOPS_WORKSPACE_UUID / pipeops workspace select)")
1921
AddonsCmd.AddCommand(deploymentsCmd)
2022
}

cmd/addons/list.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ func runAddonDeployments(cmd *cobra.Command, args []string) {
113113
if !utils.RequireAuth(client, opts) {
114114
return
115115
}
116+
if ws, _ := cmd.Flags().GetString("workspace"); ws != "" {
117+
client.SetWorkspaceOverride(ws)
118+
}
116119

117120
utils.PrintInfo("Fetching deployed addons...", opts)
118121

@@ -162,5 +165,6 @@ func runAddonDeployments(cmd *cobra.Command, args []string) {
162165
}
163166

164167
func init() {
168+
listCmd.Flags().String("workspace", "", "Workspace UUID (or set PIPEOPS_WORKSPACE_UUID / pipeops workspace select)")
165169
AddonsCmd.AddCommand(listCmd, availableCmd)
166170
}

cmd/environment.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var environmentListCmd = &cobra.Command{
2323
Short: "List environments",
2424
RunE: func(cmd *cobra.Command, args []string) error {
2525
opts := utils.GetOutputOptions(cmd)
26-
client, err := rootClient(opts)
26+
client, err := rootClient(cmd, opts)
2727
if err != nil || client == nil {
2828
return err
2929
}
@@ -49,7 +49,7 @@ var environmentGetCmd = &cobra.Command{
4949
Short: "Get environment details",
5050
RunE: func(cmd *cobra.Command, args []string) error {
5151
opts := utils.GetOutputOptions(cmd)
52-
client, err := rootClient(opts)
52+
client, err := rootClient(cmd, opts)
5353
if err != nil || client == nil {
5454
return err
5555
}
@@ -67,7 +67,7 @@ var environmentCreateCmd = &cobra.Command{
6767
Short: "Create an environment",
6868
RunE: func(cmd *cobra.Command, args []string) error {
6969
opts := utils.GetOutputOptions(cmd)
70-
client, err := rootClient(opts)
70+
client, err := rootClient(cmd, opts)
7171
if err != nil || client == nil {
7272
return err
7373
}
@@ -101,7 +101,7 @@ var environmentUpdateCmd = &cobra.Command{
101101
Short: "Update an environment",
102102
RunE: func(cmd *cobra.Command, args []string) error {
103103
opts := utils.GetOutputOptions(cmd)
104-
client, err := rootClient(opts)
104+
client, err := rootClient(cmd, opts)
105105
if err != nil || client == nil {
106106
return err
107107
}
@@ -127,7 +127,7 @@ var environmentDeleteCmd = &cobra.Command{
127127
if !force {
128128
return fmt.Errorf("--force is required to delete an environment")
129129
}
130-
client, err := rootClient(opts)
130+
client, err := rootClient(cmd, opts)
131131
if err != nil || client == nil {
132132
return err
133133
}
@@ -153,7 +153,7 @@ var environmentVarsSetCmd = &cobra.Command{
153153
Short: "Set environment variables",
154154
RunE: func(cmd *cobra.Command, args []string) error {
155155
opts := utils.GetOutputOptions(cmd)
156-
client, err := rootClient(opts)
156+
client, err := rootClient(cmd, opts)
157157
if err != nil || client == nil {
158158
return err
159159
}
@@ -173,7 +173,7 @@ var environmentVarsSetCmd = &cobra.Command{
173173
Args: cobra.MinimumNArgs(2),
174174
}
175175

176-
func rootClient(opts utils.OutputOptions) (pipeops.ClientAPI, error) {
176+
func rootClient(cmd *cobra.Command, opts utils.OutputOptions) (pipeops.ClientAPI, error) {
177177
cfg, err := config.Load()
178178
if err != nil {
179179
return nil, fmt.Errorf("load configuration: %w", err)
@@ -182,6 +182,13 @@ func rootClient(opts utils.OutputOptions) (pipeops.ClientAPI, error) {
182182
if !utils.RequireAuth(client, opts) {
183183
return nil, nil
184184
}
185+
if cmd != nil {
186+
if flag := cmd.Flags().Lookup("workspace"); flag != nil {
187+
if ws := flag.Value.String(); ws != "" {
188+
client.SetWorkspaceOverride(ws)
189+
}
190+
}
191+
}
185192
return client, nil
186193
}
187194

@@ -227,6 +234,8 @@ func sdkTime(ts *sdk.Timestamp) string {
227234
}
228235

229236
func init() {
237+
environmentListCmd.Flags().String("workspace", "", "Workspace UUID (or set PIPEOPS_WORKSPACE_UUID / pipeops workspace select)")
238+
environmentGetCmd.Flags().String("workspace", "", "Workspace UUID (or set PIPEOPS_WORKSPACE_UUID / pipeops workspace select)")
230239
environmentCreateCmd.Flags().String("name", "", "Environment name")
231240
environmentCreateCmd.Flags().String("workspace", "", "Workspace UUID")
232241
environmentCreateCmd.Flags().String("cluster", "", "Cluster UUID")

cmd/gitops.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var gitopsListCmd = &cobra.Command{
3535
Short: "List GitOps applications",
3636
RunE: func(cmd *cobra.Command, args []string) error {
3737
opts := utils.GetOutputOptions(cmd)
38-
client, err := rootClient(opts)
38+
client, err := rootClient(cmd, opts)
3939
if err != nil || client == nil {
4040
return err
4141
}
@@ -46,6 +46,9 @@ var gitopsListCmd = &cobra.Command{
4646
if limit, _ := cmd.Flags().GetInt("limit"); limit > 0 {
4747
listOpts.Limit = limit
4848
}
49+
if workspace, _ := cmd.Flags().GetString("workspace"); workspace != "" {
50+
listOpts.WorkspaceUUID = workspace
51+
}
4952
resp, err := client.ListGitOps(context.Background(), listOpts)
5053
if err != nil {
5154
return fmt.Errorf("list gitops: %w", err)
@@ -83,7 +86,7 @@ var gitopsGetCmd = &cobra.Command{
8386
Short: "Get GitOps application details",
8487
RunE: func(cmd *cobra.Command, args []string) error {
8588
opts := utils.GetOutputOptions(cmd)
86-
client, err := rootClient(opts)
89+
client, err := rootClient(cmd, opts)
8790
if err != nil || client == nil {
8891
return err
8992
}
@@ -101,7 +104,7 @@ var gitopsCreateCmd = &cobra.Command{
101104
Short: "Create a GitOps application",
102105
RunE: func(cmd *cobra.Command, args []string) error {
103106
opts := utils.GetOutputOptions(cmd)
104-
client, err := rootClient(opts)
107+
client, err := rootClient(cmd, opts)
105108
if err != nil || client == nil {
106109
return err
107110
}
@@ -148,7 +151,7 @@ var gitopsUpdateCmd = &cobra.Command{
148151
Short: "Update a GitOps application",
149152
RunE: func(cmd *cobra.Command, args []string) error {
150153
opts := utils.GetOutputOptions(cmd)
151-
client, err := rootClient(opts)
154+
client, err := rootClient(cmd, opts)
152155
if err != nil || client == nil {
153156
return err
154157
}
@@ -186,7 +189,7 @@ var gitopsDeleteCmd = &cobra.Command{
186189
if !yes {
187190
return fmt.Errorf("--yes is required to delete a GitOps application")
188191
}
189-
client, err := rootClient(opts)
192+
client, err := rootClient(cmd, opts)
190193
if err != nil || client == nil {
191194
return err
192195
}
@@ -207,7 +210,7 @@ var gitopsSyncCmd = &cobra.Command{
207210
Short: "Trigger a GitOps sync",
208211
RunE: func(cmd *cobra.Command, args []string) error {
209212
opts := utils.GetOutputOptions(cmd)
210-
client, err := rootClient(opts)
213+
client, err := rootClient(cmd, opts)
211214
if err != nil || client == nil {
212215
return err
213216
}
@@ -244,7 +247,7 @@ var gitopsStatusCmd = &cobra.Command{
244247
Short: "Get GitOps sync status",
245248
RunE: func(cmd *cobra.Command, args []string) error {
246249
opts := utils.GetOutputOptions(cmd)
247-
client, err := rootClient(opts)
250+
client, err := rootClient(cmd, opts)
248251
if err != nil || client == nil {
249252
return err
250253
}
@@ -277,7 +280,7 @@ var gitopsDiffCmd = &cobra.Command{
277280
Short: "Show GitOps diff (git vs live)",
278281
RunE: func(cmd *cobra.Command, args []string) error {
279282
opts := utils.GetOutputOptions(cmd)
280-
client, err := rootClient(opts)
283+
client, err := rootClient(cmd, opts)
281284
if err != nil || client == nil {
282285
return err
283286
}
@@ -306,7 +309,7 @@ var gitopsHistoryCmd = &cobra.Command{
306309
Short: "Show GitOps sync history",
307310
RunE: func(cmd *cobra.Command, args []string) error {
308311
opts := utils.GetOutputOptions(cmd)
309-
client, err := rootClient(opts)
312+
client, err := rootClient(cmd, opts)
310313
if err != nil || client == nil {
311314
return err
312315
}
@@ -428,6 +431,7 @@ func optionalUintFlag(cmd *cobra.Command, name string) (*uint, error) {
428431
func init() {
429432
gitopsListCmd.Flags().Int("page", 0, "Page number")
430433
gitopsListCmd.Flags().Int("limit", 0, "Page size")
434+
gitopsListCmd.Flags().String("workspace", "", "Workspace UUID (or set PIPEOPS_WORKSPACE_UUID / pipeops workspace select)")
431435

432436
gitopsCreateCmd.Flags().String("name", "", "Application name")
433437
gitopsCreateCmd.Flags().String("repo-url", "", "Git repository URL")

cmd/groups.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var groupsListCmd = &cobra.Command{
6767
Short: "List project groups",
6868
RunE: func(cmd *cobra.Command, args []string) error {
6969
opts := utils.GetOutputOptions(cmd)
70-
client, err := rootClient(opts)
70+
client, err := rootClient(cmd, opts)
7171
if err != nil || client == nil {
7272
return err
7373
}
@@ -107,7 +107,7 @@ var groupsGetCmd = &cobra.Command{
107107
Short: "Get project group details",
108108
RunE: func(cmd *cobra.Command, args []string) error {
109109
opts := utils.GetOutputOptions(cmd)
110-
client, err := rootClient(opts)
110+
client, err := rootClient(cmd, opts)
111111
if err != nil || client == nil {
112112
return err
113113
}
@@ -125,7 +125,7 @@ var groupsCreateCmd = &cobra.Command{
125125
Short: "Create a project group",
126126
RunE: func(cmd *cobra.Command, args []string) error {
127127
opts := utils.GetOutputOptions(cmd)
128-
client, err := rootClient(opts)
128+
client, err := rootClient(cmd, opts)
129129
if err != nil || client == nil {
130130
return err
131131
}
@@ -154,7 +154,7 @@ var groupsUpdateCmd = &cobra.Command{
154154
Short: "Update a project group",
155155
RunE: func(cmd *cobra.Command, args []string) error {
156156
opts := utils.GetOutputOptions(cmd)
157-
client, err := rootClient(opts)
157+
client, err := rootClient(cmd, opts)
158158
if err != nil || client == nil {
159159
return err
160160
}
@@ -192,7 +192,7 @@ var groupsDeleteCmd = &cobra.Command{
192192
if !yes {
193193
return fmt.Errorf("--yes is required to delete a project group")
194194
}
195-
client, err := rootClient(opts)
195+
client, err := rootClient(cmd, opts)
196196
if err != nil || client == nil {
197197
return err
198198
}
@@ -213,7 +213,7 @@ var groupsTopologyCmd = &cobra.Command{
213213
Short: "Show project group topology",
214214
RunE: func(cmd *cobra.Command, args []string) error {
215215
opts := utils.GetOutputOptions(cmd)
216-
client, err := rootClient(opts)
216+
client, err := rootClient(cmd, opts)
217217
if err != nil || client == nil {
218218
return err
219219
}
@@ -268,7 +268,7 @@ var groupsMembersAttachCmd = &cobra.Command{
268268
Short: "Attach a project or addon to a group",
269269
RunE: func(cmd *cobra.Command, args []string) error {
270270
opts := utils.GetOutputOptions(cmd)
271-
client, err := rootClient(opts)
271+
client, err := rootClient(cmd, opts)
272272
if err != nil || client == nil {
273273
return err
274274
}
@@ -313,7 +313,7 @@ var groupsMembersDetachCmd = &cobra.Command{
313313
Short: "Detach a project or addon from a group",
314314
RunE: func(cmd *cobra.Command, args []string) error {
315315
opts := utils.GetOutputOptions(cmd)
316-
client, err := rootClient(opts)
316+
client, err := rootClient(cmd, opts)
317317
if err != nil || client == nil {
318318
return err
319319
}
@@ -357,7 +357,7 @@ var groupsEnvGetCmd = &cobra.Command{
357357
Short: "Get shared environment variables",
358358
RunE: func(cmd *cobra.Command, args []string) error {
359359
opts := utils.GetOutputOptions(cmd)
360-
client, err := rootClient(opts)
360+
client, err := rootClient(cmd, opts)
361361
if err != nil || client == nil {
362362
return err
363363
}
@@ -394,7 +394,7 @@ JSON file may be either:
394394
or a plain object map: {"KEY":"VAL",...}`,
395395
RunE: func(cmd *cobra.Command, args []string) error {
396396
opts := utils.GetOutputOptions(cmd)
397-
client, err := rootClient(opts)
397+
client, err := rootClient(cmd, opts)
398398
if err != nil || client == nil {
399399
return err
400400
}
@@ -423,7 +423,7 @@ var groupsEnvInjectCmd = &cobra.Command{
423423
Short: "Inject shared environment variables into members",
424424
RunE: func(cmd *cobra.Command, args []string) error {
425425
opts := utils.GetOutputOptions(cmd)
426-
client, err := rootClient(opts)
426+
client, err := rootClient(cmd, opts)
427427
if err != nil || client == nil {
428428
return err
429429
}
@@ -474,7 +474,7 @@ var groupsConnectCmd = &cobra.Command{
474474
Short: "Connect provider addon envs into a consumer project",
475475
RunE: func(cmd *cobra.Command, args []string) error {
476476
opts := utils.GetOutputOptions(cmd)
477-
client, err := rootClient(opts)
477+
client, err := rootClient(cmd, opts)
478478
if err != nil || client == nil {
479479
return err
480480
}
@@ -539,7 +539,7 @@ var groupsRedeployCmd = &cobra.Command{
539539
Short: "Redeploy application members in a project group",
540540
RunE: func(cmd *cobra.Command, args []string) error {
541541
opts := utils.GetOutputOptions(cmd)
542-
client, err := rootClient(opts)
542+
client, err := rootClient(cmd, opts)
543543
if err != nil || client == nil {
544544
return err
545545
}
@@ -568,7 +568,7 @@ var groupsResolveCmd = &cobra.Command{
568568
Short: "Resolve which group a member belongs to",
569569
RunE: func(cmd *cobra.Command, args []string) error {
570570
opts := utils.GetOutputOptions(cmd)
571-
client, err := rootClient(opts)
571+
client, err := rootClient(cmd, opts)
572572
if err != nil || client == nil {
573573
return err
574574
}
@@ -606,7 +606,7 @@ var groupsCandidatesCmd = &cobra.Command{
606606
Short: "List attachable projects and addons",
607607
RunE: func(cmd *cobra.Command, args []string) error {
608608
opts := utils.GetOutputOptions(cmd)
609-
client, err := rootClient(opts)
609+
client, err := rootClient(cmd, opts)
610610
if err != nil || client == nil {
611611
return err
612612
}

cmd/project/helpers.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/spf13/cobra"
1414
)
1515

16+
const workspaceFlagHelp = "Workspace UUID (or set PIPEOPS_WORKSPACE_UUID / pipeops workspace select)"
17+
1618
func authenticatedClient(cmd *cobra.Command, opts utils.OutputOptions) (pipeops.ClientAPI, error) {
1719
cfg, err := config.Load()
1820
if err != nil {
@@ -22,9 +24,21 @@ func authenticatedClient(cmd *cobra.Command, opts utils.OutputOptions) (pipeops.
2224
if !utils.RequireAuth(client, opts) {
2325
return nil, nil
2426
}
27+
applyWorkspaceFlag(cmd, client)
2528
return client, nil
2629
}
2730

31+
func applyWorkspaceFlag(cmd *cobra.Command, client pipeops.ClientAPI) {
32+
if cmd == nil || client == nil {
33+
return
34+
}
35+
if flag := cmd.Flags().Lookup("workspace"); flag != nil {
36+
if ws := strings.TrimSpace(flag.Value.String()); ws != "" {
37+
client.SetWorkspaceOverride(ws)
38+
}
39+
}
40+
}
41+
2842
func printProject(project *models.Project, opts utils.OutputOptions) {
2943
if opts.Format == utils.OutputFormatJSON {
3044
_ = utils.PrintJSON(project)

cmd/project/list.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Examples:
3636
if !utils.RequireAuth(client, opts) {
3737
return
3838
}
39+
applyWorkspaceFlag(cmd, client)
3940

4041
// Fetch projects from API
4142
projectsResp, err := client.GetProjects()
@@ -98,6 +99,7 @@ Examples:
9899

99100
// NewList initializes and returns the list command
100101
func (p *projectModel) listProjects() *cobra.Command {
102+
listCmd.Flags().String("workspace", "", workspaceFlagHelp)
101103
p.rootCmd.AddCommand(listCmd)
102104
return listCmd
103105
}

0 commit comments

Comments
 (0)