1
1
package target
2
2
3
3
import (
4
+ "strings"
5
+
4
6
"github.com/pinecone-io/cli/internal/pkg/dashboard"
5
7
"github.com/pinecone-io/cli/internal/pkg/utils/configuration/state"
6
8
"github.com/pinecone-io/cli/internal/pkg/utils/exit"
7
9
"github.com/pinecone-io/cli/internal/pkg/utils/help"
8
10
"github.com/pinecone-io/cli/internal/pkg/utils/log"
11
+ "github.com/pinecone-io/cli/internal/pkg/utils/msg"
9
12
"github.com/pinecone-io/cli/internal/pkg/utils/pcio"
10
13
"github.com/pinecone-io/cli/internal/pkg/utils/presenters"
11
14
"github.com/pinecone-io/cli/internal/pkg/utils/style"
@@ -59,40 +62,41 @@ func NewTargetCmd() *cobra.Command {
59
62
text .PrettyPrintJSON (state .GetTargetContext ())
60
63
return
61
64
}
62
- log .Info ().Msg ("Outputting target context as table" )
65
+ log .Info ().
66
+ Msg ("Outputting target context as table" )
63
67
pcio .Printf ("To update the context, run %s. The current target context is:\n \n " , style .Code ("pinecone target --org <org> --project <project>" ))
64
68
presenters .PrintTargetContext (state .GetTargetContext ())
65
69
return
66
70
}
67
71
68
72
orgs , err := dashboard .GetOrganizations ()
69
73
if err != nil {
74
+ msg .FailMsg ("Failed to get organizations: %s" , err )
70
75
exit .Error (err )
71
76
}
72
77
73
78
var org dashboard.Organization
74
79
if options .Org != "" {
75
- org , err = getOrg (orgs , options .Org )
76
- if err != nil {
77
- exit .Error (err )
78
- }
80
+ org = getOrg (orgs , options .Org )
79
81
if ! options .json {
80
- pcio . Printf ( style . SuccessMsg ("Target org updated to %s\n " ) , style .Emphasis (org .Name ))
82
+ msg . SuccessMsg ("Target org updated to %s" , style .Emphasis (org .Name ))
81
83
}
82
- state .TargetOrgName .Set (org .Name )
83
- state .TargetProjectName .Set ("" )
84
- } else {
85
- org , err = getOrg (orgs , state .TargetOrgName .Get ())
86
- if err != nil {
87
- exit .Error (err )
84
+ var oldOrg = state .TargetOrgName .Get ()
85
+ var newOrg = org .Name
86
+
87
+ state .TargetOrgName .Set (newOrg )
88
+
89
+ // If the org has changed, reset the project
90
+ if oldOrg != org .Name {
91
+ state .TargetProjectName .Set ("" )
88
92
}
93
+ } else {
94
+ // Use the current org if no org is specified
95
+ org = getOrg (orgs , state .TargetOrgName .Get ())
89
96
}
90
97
91
98
if options .Project != "" {
92
- proj , err := getProject (org , options .Project )
93
- if err != nil {
94
- exit .Error (err )
95
- }
99
+ proj := getProject (org , options .Project )
96
100
if ! options .json {
97
101
pcio .Printf (style .SuccessMsg ("Target project updated to %s\n " ), style .Emphasis (proj .Name ))
98
102
}
@@ -117,20 +121,39 @@ func NewTargetCmd() *cobra.Command {
117
121
return cmd
118
122
}
119
123
120
- func getOrg (orgs * dashboard.OrganizationsResponse , orgName string ) ( dashboard.Organization , error ) {
124
+ func getOrg (orgs * dashboard.OrganizationsResponse , orgName string ) dashboard.Organization {
121
125
for _ , org := range orgs .Organizations {
122
126
if org .Name == orgName {
123
- return org , nil
127
+ return org
124
128
}
125
129
}
126
- return dashboard.Organization {}, pcio .Errorf ("organization %s not found" , style .Emphasis (orgName ))
130
+
131
+ // Join org names for error message
132
+ orgNames := make ([]string , len (orgs .Organizations ))
133
+ for i , org := range orgs .Organizations {
134
+ orgNames [i ] = org .Name
135
+ }
136
+
137
+ availableOrgs := strings .Join (orgNames , ", " )
138
+ log .Error ().Str ("orgName" , orgName ).Str ("avialableOrgs" , availableOrgs ).Msg ("Failed to find organization" )
139
+ msg .FailMsg ("Failed to find organization %s. Available organizations: %s.\n " , style .Emphasis (orgName ), availableOrgs )
140
+ exit .ErrorMsg (pcio .Sprintf ("organization %s not found" , style .Emphasis (orgName )))
141
+ return dashboard.Organization {}
127
142
}
128
143
129
- func getProject (org dashboard.Organization , projectName string ) ( dashboard.Project , error ) {
144
+ func getProject (org dashboard.Organization , projectName string ) dashboard.Project {
130
145
for _ , project := range org .Projects {
131
146
if project .Name == projectName {
132
- return project , nil
147
+ return project
133
148
}
134
149
}
135
- return dashboard.Project {}, pcio .Errorf ("project %s not found in org %s" , style .Emphasis (projectName ), style .Emphasis (org .Name ))
150
+
151
+ availableProjects := make ([]string , len (org .Projects ))
152
+ for i , project := range org .Projects {
153
+ availableProjects [i ] = project .Name
154
+ }
155
+
156
+ msg .FailMsg ("Failed to find project %s in org %s. Available projects: %s." , style .Emphasis (projectName ), style .Emphasis (org .Name ), strings .Join (availableProjects , ", " ))
157
+ exit .Error (pcio .Errorf ("project %s not found in organization %s" , projectName , org .Name ))
158
+ return dashboard.Project {}
136
159
}
0 commit comments