Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7a4eaf1

Browse files
committed
handle wrong org selected
1 parent 52bf5aa commit 7a4eaf1

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

cli/create.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"strings"
78
"time"
89

910
"github.com/google/uuid"
@@ -154,16 +155,21 @@ func (r *RootCmd) create() *serpent.Command {
154155
}
155156

156157
if len(templates) > 1 {
158+
templateOrgs := []string{}
159+
for _, tpl := range templates {
160+
templateOrgs = append(templateOrgs, tpl.OrganizationName)
161+
}
162+
157163
selectedOrg, err := orgContext.Selected(inv, client)
158164
if err != nil {
159-
return xerrors.Errorf("multiple templates found with the name %q, use `--org=<organization_name>` to specify which template by that name to use", templateName)
165+
return xerrors.Errorf("multiple templates found with the name %q, use `--org=<organization_name>` to specify which template by that name to use. Organizations available: %s", templateName, strings.Join(templateOrgs, ", "))
160166
}
161167

162168
index := slices.IndexFunc(templates, func(i codersdk.Template) bool {
163169
return i.OrganizationID == selectedOrg.ID
164170
})
165171
if index == -1 {
166-
return xerrors.Errorf("no templates found with the name %q in the organization %q", templateName, selectedOrg.Name)
172+
return xerrors.Errorf("no templates found with the name %q in the organization %q. Templates by that name exist in organizations: %s. Use --org=<organization_name> to select one.", templateName, selectedOrg.Name, strings.Join(templateOrgs, ", "))
167173
}
168174

169175
// remake the list with the only template selected
@@ -174,6 +180,29 @@ func (r *RootCmd) create() *serpent.Command {
174180
templateVersionID = template.ActiveVersionID
175181
}
176182

183+
// If the user specified an organization via a flag or env var, the template **must**
184+
// be in that organization. Otherwise, we should throw an error.
185+
orgValue, orgValueSource := orgContext.ValueSource(inv)
186+
if orgValue != "" && !(orgValueSource == serpent.ValueSourceDefault || orgValueSource == serpent.ValueSourceNone) {
187+
selectedOrg, err := orgContext.Selected(inv, client)
188+
if err != nil {
189+
return err
190+
}
191+
192+
if template.OrganizationID != selectedOrg.ID {
193+
orgNameFormat := "'--org=%q'"
194+
if orgValueSource == serpent.ValueSourceEnv {
195+
orgNameFormat = "CODER_ORGANIZATION=%q"
196+
}
197+
198+
return xerrors.Errorf("template is in organization %q, but %s was specified. Use %s to use this template",
199+
template.OrganizationName,
200+
fmt.Sprintf(orgNameFormat, selectedOrg.Name),
201+
fmt.Sprintf(orgNameFormat, template.OrganizationName),
202+
)
203+
}
204+
}
205+
177206
var schedSpec *string
178207
if startAt != "" {
179208
sched, err := parseCLISchedule(startAt)

cli/root.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,9 +640,10 @@ func NewOrganizationContext() *OrganizationContext {
640640
return &OrganizationContext{}
641641
}
642642

643+
func (*OrganizationContext) optionName() string { return "Organization" }
643644
func (o *OrganizationContext) AttachOptions(cmd *serpent.Command) {
644645
cmd.Options = append(cmd.Options, serpent.Option{
645-
Name: "Organization",
646+
Name: o.optionName(),
646647
Description: "Select which organization (uuid or name) to use.",
647648
// Only required if the user is a part of more than 1 organization.
648649
// Otherwise, we can assume a default value.
@@ -654,6 +655,14 @@ func (o *OrganizationContext) AttachOptions(cmd *serpent.Command) {
654655
})
655656
}
656657

658+
func (o *OrganizationContext) ValueSource(inv *serpent.Invocation) (string, serpent.ValueSource) {
659+
opt := inv.Command.Options.ByName(o.optionName())
660+
if opt == nil {
661+
return o.FlagSelect, serpent.ValueSourceNone
662+
}
663+
return o.FlagSelect, opt.ValueSource
664+
}
665+
657666
func (o *OrganizationContext) Selected(inv *serpent.Invocation, client *codersdk.Client) (codersdk.Organization, error) {
658667
// Fetch the set of organizations the user is a member of.
659668
orgs, err := client.OrganizationsByUser(inv.Context(), codersdk.Me)

0 commit comments

Comments
 (0)