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

Commit 9dbb3cb

Browse files
author
Faris Huskovic
committed
combine lookupUserImgs with findImg so we only return 1 image and an error
1 parent 8258ed2 commit 9dbb3cb

File tree

1 file changed

+32
-49
lines changed

1 file changed

+32
-49
lines changed

internal/cmd/ceapi.go

+32-49
Original file line numberDiff line numberDiff line change
@@ -84,33 +84,50 @@ func findEnv(ctx context.Context, client *coder.Client, envName, userEmail strin
8484
}
8585

8686
func findImg(ctx context.Context, client *coder.Client, email, imgName, orgName string) (*coder.Image, error) {
87-
userImgs, err := lookupUserImgs(ctx, client, email, imgName, orgName)
87+
switch {
88+
case email == "":
89+
return nil, xerrors.New("user email unset")
90+
case imgName == "":
91+
return nil, xerrors.New("image name unset")
92+
}
93+
94+
imgs, err := getImgs(ctx, client, email, orgName)
8895
if err != nil {
8996
return nil, err
9097
}
9198

92-
numImgs := len(userImgs)
99+
var possibleMatches []coder.Image
93100

94-
if numImgs == 0 {
95-
return nil, xerrors.New("image not found - did you forget to import this image?")
101+
// The user may provide an image thats not an exact match
102+
// to one of their imported images but they may be close.
103+
// We can assist the user by collecting images that contain
104+
// the user provided image flag value as a substring.
105+
for _, img := range imgs {
106+
// If it's an exact match we can just return and exit.
107+
if img.Repository == imgName {
108+
return &img, nil
109+
}
110+
if strings.Contains(img.Repository, imgName) {
111+
possibleMatches = append(possibleMatches, img)
112+
}
96113
}
97114

98-
if numImgs > 1 {
99-
lines := []string{clog.Tipf("Did you mean?")}
100-
101-
for _, img := range userImgs {
102-
lines = append(lines, img.Repository)
103-
}
115+
if len(possibleMatches) == 0 {
116+
return nil, xerrors.New("image not found")
117+
}
104118

119+
lines := []string{clog.Tipf("Did you mean?")}
105120

106-
clog.Fatal(
107-
fmt.Sprintf("Found %d possible matches for %q.", numImgs, imgName),
108-
lines...,
109-
)
121+
for _, img := range possibleMatches {
122+
lines = append(lines, img.Repository)
110123
}
111-
return &userImgs[0], nil
124+
return nil, clog.Fatal(
125+
fmt.Sprintf("Found %d possible matches for %q.", len(possibleMatches), imgName),
126+
lines...,
127+
)
112128
}
113129

130+
114131
func getImgs(ctx context.Context, client *coder.Client, email, orgName string) ([]coder.Image, error) {
115132
u, err := client.UserByEmail(ctx, email)
116133
if err != nil {
@@ -136,40 +153,6 @@ func getImgs(ctx context.Context, client *coder.Client, email, orgName string) (
136153
return nil, xerrors.Errorf("org name %q not found", orgName)
137154
}
138155

139-
// returns all images that contain imgName as a substring and have been imported by the user.
140-
func lookupUserImgs(ctx context.Context, client *coder.Client, email, imgName, orgName string) ([]coder.Image, error) {
141-
switch {
142-
case email == "":
143-
return nil, xerrors.New("user email unset")
144-
case imgName == "":
145-
return nil, xerrors.New("image name unset")
146-
}
147-
148-
imgs, err := getImgs(ctx, client, email, orgName)
149-
if err != nil {
150-
return nil, err
151-
}
152-
153-
var userImgs []coder.Image
154-
155-
// The user may provide an image thats not an exact match
156-
// to one of their imported images but they may be close.
157-
// We can assist the user by collecting images that contain
158-
// the user provided image flag value as a substring.
159-
for _, img := range imgs {
160-
if strings.Contains(img.Repository, imgName) {
161-
userImgs = append(userImgs, img)
162-
}
163-
// If it's an exact match we can overwrite the slice and exit the loop
164-
// since we won't need the fuzzy matched images anymore.
165-
if img.Repository == imgName {
166-
userImgs = []coder.Image{img}
167-
break
168-
}
169-
}
170-
return userImgs, nil
171-
}
172-
173156
func isMultiOrgMember(email string) (*bool, error) {
174157
ctx, cancel := context.WithCancel(context.Background())
175158
defer cancel()

0 commit comments

Comments
 (0)