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

Commit 89f848f

Browse files
author
Faris Huskovic
committed
context adjustments
1 parent bc7bcb8 commit 89f848f

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

internal/clog/error.go

+10
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+
func LogWarn(header string, 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
func Warn(header string, lines ...string) CLIError {
7383
return CLIError{

internal/cmd/ceapi.go

+13-9
Original file line numberDiff line numberDiff line change
@@ -141,21 +141,25 @@ func getImgs(ctx context.Context, client *coder.Client, email, orgName string) (
141141
orgs = lookupUserOrgs(u, orgs)
142142

143143
for _, org := range orgs {
144-
if orgName == org.Name {
145-
imgs, err := client.GetOrganizationImages(ctx, org.ID)
146-
if err != nil {
147-
return nil, err
148-
}
144+
imgs, err := client.GetOrganizationImages(ctx, org.ID)
145+
if err != nil {
146+
return nil, err
147+
}
148+
// If orgName is set we know the user is a multi-org member
149+
// so we should only return the imported images that beong to the org they specified.
150+
if orgName != "" && orgName == org.Name {
151+
return imgs, nil
152+
}
153+
154+
if orgName == "" {
155+
// if orgName is unset we know the user is only part of one org.
149156
return imgs, nil
150157
}
151158
}
152159
return nil, xerrors.Errorf("org name %q not found", orgName)
153160
}
154161

155-
func isMultiOrgMember(email string) (*bool, error) {
156-
ctx, cancel := context.WithCancel(context.Background())
157-
defer cancel()
158-
162+
func isMultiOrgMember(ctx context.Context, email string) (*bool, error) {
159163
client, err := newClient()
160164
if err != nil {
161165
return nil, err

internal/cmd/envs.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ import (
1717
"golang.org/x/xerrors"
1818
)
1919

20-
const (
21-
defaultOrg = "default"
22-
defaultImgTag = "latest"
23-
)
20+
const defaultImgTag = "latest"
2421

2522
func envsCommand() *cobra.Command {
2623
var user string
@@ -170,7 +167,7 @@ coder envs create --cpu 4 --disk 100 --memory 8 --image 5f443b16-30652892427b955
170167
return xerrors.New("image unset")
171168
}
172169

173-
multiOrgMember, err := isMultiOrgMember(*user)
170+
multiOrgMember, err := isMultiOrgMember(cmd.Context(), *user)
174171
if err != nil {
175172
return err
176173
}
@@ -214,7 +211,7 @@ coder envs create --cpu 4 --disk 100 --memory 8 --image 5f443b16-30652892427b955
214211
createReq.DiskGB = importedImg.DefaultDiskGB
215212
}
216213

217-
env, err := client.CreateEnvironment(cmd.Context(), org, *createReq)
214+
env, err := client.CreateEnvironment(cmd.Context(), importedImg.OrganizationID, *createReq)
218215
if err != nil {
219216
return xerrors.Errorf("create environment: %w", err)
220217
}
@@ -234,7 +231,7 @@ coder envs create --cpu 4 --disk 100 --memory 8 --image 5f443b16-30652892427b955
234231
return nil
235232
},
236233
}
237-
cmd.Flags().StringVarP(&org, "org", "o", defaultOrg, "ID of the organization the environment should be created under.")
234+
cmd.Flags().StringVarP(&org, "org", "o", "", "ID of the organization the environment should be created under.")
238235
cmd.Flags().StringVarP(&tag, "tag", "t", defaultImgTag, "tag of the image the environment will be based off of.")
239236
cmd.Flags().Float32P("cpu", "c", 0, "number of cpu cores the environment should be provisioned with.")
240237
cmd.Flags().Float32P("memory", "m", 0, "GB of RAM an environment should be provisioned with.")
@@ -280,7 +277,7 @@ coder envs edit back-end-env --disk 20`,
280277
return err
281278
}
282279

283-
multiOrgMember, err := isMultiOrgMember(*user)
280+
multiOrgMember, err := isMultiOrgMember(cmd.Context(), *user)
284281
if err != nil {
285282
return err
286283
}
@@ -332,7 +329,7 @@ coder envs edit back-end-env --disk 20`,
332329
return nil
333330
},
334331
}
335-
cmd.Flags().StringVarP(&org, "org", "o", defaultOrg, "name of the organization the environment should be created under.")
332+
cmd.Flags().StringVarP(&org, "org", "o", "", "name of the organization the environment should be created under.")
336333
cmd.Flags().StringVarP(&img, "image", "i", "", "name of the image you wan't the environment to be based off of.")
337334
cmd.Flags().StringVarP(&tag, "tag", "t", "latest", "image tag of the image you wan't to base the environment off of.")
338335
cmd.Flags().Float32P("cpu", "c", cpuCores, "The number of cpu cores the environment should be provisioned with.")
@@ -484,6 +481,9 @@ func buildUpdateReq(ctx context.Context, conf updateConf) (*coder.UpdateEnvironm
484481
// if the user accidentally requests it or if the default diskGB value for a
485482
// newly requested image is smaller than the current amount the environment is using.
486483
if *updateReq.DiskGB < conf.environment.DiskGB {
484+
clog.LogWarn("disk can not be shrunk",
485+
fmt.Sprintf("keeping environment disk at %d GB", conf.environment.DiskGB),
486+
)
487487
updateReq.DiskGB = &conf.environment.DiskGB
488488
}
489489

0 commit comments

Comments
 (0)