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
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Commit58b4d42

Browse files
author
Faris Huskovic
committed
Change coder create | edit envs image flag to take image name and use defaults from image
1 parent6f8b9b8 commit58b4d42

File tree

5 files changed

+324
-81
lines changed

5 files changed

+324
-81
lines changed

‎ci/integration/envs_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
// From Coder organization images
12-
constubuntuImgID="5f443b16-30652892427b955601330fa5"
12+
//const ubuntuImgID = "5f443b16-30652892427b955601330fa5"
1313

1414
funcTestEnvsCLI(t*testing.T) {
1515
t.Parallel()
@@ -37,6 +37,18 @@ func TestEnvsCLI(t *testing.T) {
3737
tcli.StderrEmpty(),
3838
)
3939

40+
// Image unset.
41+
c.Run(ctx,"coder envs create test-env").Assert(t,
42+
tcli.StderrMatches(regexp.QuoteMeta("fatal: required flag(s)\"image\" not set")),
43+
tcli.Error(),
44+
)
45+
46+
// Image not imported.
47+
c.Run(ctx,"coder envs create test-env --image doesntmatter").Assert(t,
48+
tcli.StderrMatches(regexp.QuoteMeta("fatal: image not found - did you forget to import this image?")),
49+
tcli.Error(),
50+
)
51+
4052
// TODO(Faris) : uncomment this when we can safely purge the environments
4153
// the integrations tests would create in the sidecar
4254
// Successfully create environment.
@@ -46,24 +58,12 @@ func TestEnvsCLI(t *testing.T) {
4658
// tcli.StderrMatches(regexp.QuoteMeta("Successfully created environment \"test-ubuntu\"")),
4759
// )
4860

49-
// Invalid environment name should fail.
50-
c.Run(ctx,"coder envs create --image "+ubuntuImgID+" this-IS-an-invalid-EnvironmentName").Assert(t,
51-
tcli.Error(),
52-
tcli.StderrMatches(regexp.QuoteMeta("environment name must conform to regex ^[a-z0-9]([a-z0-9-]+)?")),
53-
)
54-
5561
// TODO(Faris) : uncomment this when we can safely purge the environments
5662
// the integrations tests would create in the sidecar
5763
// Successfully provision environment with fractional resource amounts
5864
// c.Run(ctx, fmt.Sprintf(`coder envs create -i %s -c 1.2 -m 1.4 non-whole-resource-amounts`, ubuntuImgID)).Assert(t,
5965
// tcli.Success(),
6066
// tcli.StderrMatches(regexp.QuoteMeta("Successfully created environment \"non-whole-resource-amounts\"")),
6167
// )
62-
63-
// Image does not exist should fail.
64-
c.Run(ctx,"coder envs create --image does-not-exist env-will-not-be-created").Assert(t,
65-
tcli.Error(),
66-
tcli.StderrMatches(regexp.QuoteMeta("does not exist")),
67-
)
6868
})
6969
}

‎coder-sdk/image.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type Image struct {
1313
Descriptionstring`json:"description"`
1414
URLstring`json:"url"`// User-supplied URL for image.
1515
DefaultCPUCoresfloat32`json:"default_cpu_cores"`
16-
DefaultMemoryGBint`json:"default_memory_gb"`
16+
DefaultMemoryGBfloat32`json:"default_memory_gb"`
1717
DefaultDiskGBint`json:"default_disk_gb"`
1818
Deprecatedbool`json:"deprecated"`
1919
}
@@ -47,3 +47,12 @@ func (c Client) ImportImage(ctx context.Context, orgID string, req ImportImageRe
4747
}
4848
return&img,nil
4949
}
50+
51+
// GetOrganizationImages returns all of the images imported for orgID.
52+
func (cClient)GetOrganizationImages(ctx context.Context,orgIDstring) ([]Image,error) {
53+
varimgs []Image
54+
iferr:=c.requestBody(ctx,http.MethodGet,"/api/orgs/"+orgID+"/images",nil,&imgs);err!=nil {
55+
returnnil,err
56+
}
57+
returnimgs,nil
58+
}

‎internal/clog/error.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ func LogSuccess(header string, lines ...string) {
6868
}.String())
6969
}
7070

71+
// LogWarn prints the given warn message to stderr.
72+
funcLogWarn(headerstring,lines...string) {
73+
fmt.Fprint(os.Stderr,CLIMessage{
74+
Level:"warning",
75+
Color:color.FgYellow,
76+
Header:header,
77+
Lines:lines,
78+
}.String())
79+
}
80+
7181
// Warn creates an error with the level "warning".
7282
funcWarn(headerstring,lines...string)CLIError {
7383
returnCLIError{

‎internal/cmd/ceapi.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"context"
55
"fmt"
6+
"strings"
67

78
"cdr.dev/coder-cli/coder-sdk"
89
"cdr.dev/coder-cli/internal/clog"
@@ -81,3 +82,111 @@ func findEnv(ctx context.Context, client *coder.Client, envName, userEmail strin
8182
clog.Tipf("run\"coder envs ls\" to view your environments"),
8283
)
8384
}
85+
86+
funcfindImg(ctx context.Context,client*coder.Client,email,imgName,orgNamestring) (*coder.Image,error) {
87+
switch {
88+
caseemail=="":
89+
returnnil,xerrors.New("user email unset")
90+
caseimgName=="":
91+
returnnil,xerrors.New("image name unset")
92+
}
93+
94+
imgs,err:=getImgs(ctx,
95+
getImgsConf{
96+
client:client,
97+
email:email,
98+
orgName:orgName,
99+
},
100+
)
101+
iferr!=nil {
102+
returnnil,err
103+
}
104+
105+
varpossibleMatches []coder.Image
106+
107+
// The user may provide an image thats not an exact match
108+
// to one of their imported images but they may be close.
109+
// We can assist the user by collecting images that contain
110+
// the user provided image flag value as a substring.
111+
for_,img:=rangeimgs {
112+
// If it's an exact match we can just return and exit.
113+
ifimg.Repository==imgName {
114+
return&img,nil
115+
}
116+
ifstrings.Contains(img.Repository,imgName) {
117+
possibleMatches=append(possibleMatches,img)
118+
}
119+
}
120+
121+
iflen(possibleMatches)==0 {
122+
returnnil,xerrors.New("image not found - did you forget to import this image?")
123+
}
124+
125+
lines:= []string{clog.Tipf("Did you mean?")}
126+
127+
for_,img:=rangepossibleMatches {
128+
lines=append(lines,img.Repository)
129+
}
130+
returnnil,clog.Fatal(
131+
fmt.Sprintf("Found %d possible matches for %q.",len(possibleMatches),imgName),
132+
lines...,
133+
)
134+
}
135+
136+
typegetImgsConfstruct {
137+
client*coder.Client
138+
emailstring
139+
orgNamestring
140+
}
141+
142+
funcgetImgs(ctx context.Context,confgetImgsConf) ([]coder.Image,error) {
143+
u,err:=conf.client.UserByEmail(ctx,conf.email)
144+
iferr!=nil {
145+
returnnil,err
146+
}
147+
148+
orgs,err:=conf.client.Organizations(ctx)
149+
iferr!=nil {
150+
returnnil,err
151+
}
152+
153+
orgs=lookupUserOrgs(u,orgs)
154+
155+
for_,org:=rangeorgs {
156+
imgs,err:=conf.client.GetOrganizationImages(ctx,org.ID)
157+
iferr!=nil {
158+
returnnil,err
159+
}
160+
// If orgName is set we know the user is a multi-org member
161+
// so we should only return the imported images that beong to the org they specified.
162+
ifconf.orgName!=""&&conf.orgName==org.Name {
163+
returnimgs,nil
164+
}
165+
166+
ifconf.orgName=="" {
167+
// if orgName is unset we know the user is only part of one org.
168+
returnimgs,nil
169+
}
170+
}
171+
returnnil,xerrors.Errorf("org name %q not found",conf.orgName)
172+
}
173+
174+
funcisMultiOrgMember(ctx context.Context,emailstring) (*bool,error) {
175+
client,err:=newClient()
176+
iferr!=nil {
177+
returnnil,err
178+
}
179+
180+
u,err:=client.UserByEmail(ctx,email)
181+
iferr!=nil {
182+
returnnil,xerrors.New("email not found")
183+
}
184+
185+
orgs,err:=client.Organizations(ctx)
186+
iferr!=nil {
187+
returnnil,err
188+
}
189+
190+
isMulti:=len(lookupUserOrgs(u,orgs))>1
191+
return&isMulti,nil
192+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp