Skip to content

Commit 3373889

Browse files
committed
handle wrong org selected
1 parent aeb7f6c commit 3373889

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
@@ -641,9 +641,10 @@ func NewOrganizationContext() *OrganizationContext {
641641
return &OrganizationContext{}
642642
}
643643

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

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

0 commit comments

Comments
 (0)