Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7a4eaf1

Browse files
committed
handle wrong org selected
1 parent52bf5aa commit7a4eaf1

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
iflen(templates)>1 {
158+
templateOrgs:= []string{}
159+
for_,tpl:=rangetemplates {
160+
templateOrgs=append(templateOrgs,tpl.OrganizationName)
161+
}
162+
157163
selectedOrg,err:=orgContext.Selected(inv,client)
158164
iferr!=nil {
159-
returnxerrors.Errorf("multiple templates found with the name %q, use `--org=<organization_name>` to specify which template by that name to use",templateName)
165+
returnxerrors.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
returni.OrganizationID==selectedOrg.ID
164170
})
165171
ifindex==-1 {
166-
returnxerrors.Errorf("no templates found with the name %q in the organization %q",templateName,selectedOrg.Name)
172+
returnxerrors.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+
iforgValue!=""&&!(orgValueSource==serpent.ValueSourceDefault||orgValueSource==serpent.ValueSourceNone) {
187+
selectedOrg,err:=orgContext.Selected(inv,client)
188+
iferr!=nil {
189+
returnerr
190+
}
191+
192+
iftemplate.OrganizationID!=selectedOrg.ID {
193+
orgNameFormat:="'--org=%q'"
194+
iforgValueSource==serpent.ValueSourceEnv {
195+
orgNameFormat="CODER_ORGANIZATION=%q"
196+
}
197+
198+
returnxerrors.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
varschedSpec*string
178207
ifstartAt!="" {
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+
ifopt==nil {
661+
returno.FlagSelect,serpent.ValueSourceNone
662+
}
663+
returno.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)

[8]ページ先頭

©2009-2025 Movatter.jp