Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit e6daf5c

Browse files
author
Faris Huskovic
committed
collect find img params in struct
1 parent 58b4d42 commit e6daf5c

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

internal/cmd/ceapi.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,26 @@ func findEnv(ctx context.Context, client *coder.Client, envName, userEmail strin
8383
)
8484
}
8585

86-
func findImg(ctx context.Context, client *coder.Client, email, imgName, orgName string) (*coder.Image, error) {
86+
type findImgConf struct {
87+
client *coder.Client
88+
email string
89+
imgName string
90+
orgName string
91+
}
92+
93+
func findImg(ctx context.Context, conf findImgConf) (*coder.Image, error) {
8794
switch {
88-
case email == "":
95+
case conf.email == "":
8996
return nil, xerrors.New("user email unset")
90-
case imgName == "":
97+
case conf.imgName == "":
9198
return nil, xerrors.New("image name unset")
9299
}
93100

94101
imgs, err := getImgs(ctx,
95102
getImgsConf{
96-
client: client,
97-
email: email,
98-
orgName: orgName,
103+
client: conf.client,
104+
email: conf.email,
105+
orgName: conf.orgName,
99106
},
100107
)
101108
if err != nil {
@@ -110,10 +117,10 @@ func findImg(ctx context.Context, client *coder.Client, email, imgName, orgName
110117
// the user provided image flag value as a substring.
111118
for _, img := range imgs {
112119
// If it's an exact match we can just return and exit.
113-
if img.Repository == imgName {
120+
if img.Repository == conf.imgName {
114121
return &img, nil
115122
}
116-
if strings.Contains(img.Repository, imgName) {
123+
if strings.Contains(img.Repository, conf.imgName) {
117124
possibleMatches = append(possibleMatches, img)
118125
}
119126
}
@@ -128,7 +135,7 @@ func findImg(ctx context.Context, client *coder.Client, email, imgName, orgName
128135
lines = append(lines, img.Repository)
129136
}
130137
return nil, clog.Fatal(
131-
fmt.Sprintf("Found %d possible matches for %q.", len(possibleMatches), imgName),
138+
fmt.Sprintf("Found %d possible matches for %q.", len(possibleMatches), conf.imgName),
132139
lines...,
133140
)
134141
}

internal/cmd/envs.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,14 @@ coder envs create --cpu 4 --disk 100 --memory 8 --image 5f443b16-30652892427b955
181181
return err
182182
}
183183

184-
importedImg, err := findImg(cmd.Context(), client, *user, img, org)
184+
importedImg, err := findImg(cmd.Context(),
185+
findImgConf{
186+
client: client,
187+
email: *user,
188+
imgName: img,
189+
orgName: org,
190+
},
191+
)
185192
if err != nil {
186193
return err
187194
}
@@ -422,10 +429,12 @@ func buildUpdateReq(ctx context.Context, conf updateConf) (*coder.UpdateEnvironm
422429
// If this is not empty it means the user is requesting to change the environment image.
423430
if conf.image != "" {
424431
importedImg, err := findImg(ctx,
425-
conf.client,
426-
*conf.user,
427-
conf.image,
428-
conf.orgName,
432+
findImgConf{
433+
client: conf.client,
434+
email: *conf.user,
435+
imgName: conf.image,
436+
orgName: conf.orgName,
437+
},
429438
)
430439
if err != nil {
431440
return nil, err

0 commit comments

Comments
 (0)