Skip to content

Commit

Permalink
refractored logic to shorten code and added nil handling logic in tes…
Browse files Browse the repository at this point in the history
…t and collectUnsetEnvVars function
  • Loading branch information
Chrisyhjiang committed Jul 4, 2024
1 parent 7d8d191 commit 2419aef
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
35 changes: 32 additions & 3 deletions src/cmd/cli/command/collectUnsetEnvVars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func TestCollectUnsetEnvVars(t *testing.T) {
project *types.Project
expected []string
}{
{
name: "Nil project",
project: nil,
expected: []string{},
},
{
name: "No unset environment variables (map structure)",
project: &types.Project{
Expand Down Expand Up @@ -65,7 +70,7 @@ func TestCollectUnsetEnvVars(t *testing.T) {
},
},
},
expected: []string{"ENV2", "ENV3"},
expected: []string{"ENV2"},
},
{
name: "All unset environment variables (map structure)",
Expand All @@ -75,7 +80,7 @@ func TestCollectUnsetEnvVars(t *testing.T) {
Name: "service1",
Environment: types.MappingWithEquals{
"ENV1": nil,
"ENV2": stringPtr(""),
"ENV2": nil,
},
},
},
Expand Down Expand Up @@ -118,7 +123,31 @@ func TestCollectUnsetEnvVars(t *testing.T) {
},
},
},
expected: []string{"ENV2", "ENV3", "ENV5"},
expected: []string{"ENV2", "ENV5"},
},
{
name: "Services with map and array structure",
project: &types.Project{
Services: map[string]types.ServiceConfig{
"x": {
Name: "x",
Environment: types.MappingWithEquals{
"regular": stringPtr("value"),
"empty": stringPtr(""),
"config": nil,
},
},
"y": {
Name: "y",
Environment: types.MappingWithEquals{
"REDIS_HOST": stringPtr("x"),
"EMPTY": stringPtr(""),
"CONFIG": nil,
},
},
},
},
expected: []string{"config", "CONFIG"},
},
}

Expand Down
38 changes: 20 additions & 18 deletions src/cmd/cli/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,28 +562,28 @@ var generateCmd = &cobra.Command{
}

term.Info("Code generated successfully in folder", prompt.Folder)

// TODO: should we use EDITOR env var instead?
cmdd := exec.Command("code", ".")
err = cmdd.Start()
if err != nil {
term.Debug("unable to launch VS Code:", err)
}

cd := ""
if prompt.Folder != "." {
cd = "`cd " + prompt.Folder + "` "
}

// Load the project and check for empty environment variables
var envInstructions = ""
loader := compose.Loader{ComposeFilePath: filepath.Join(prompt.Folder, "compose.yaml")}
project, err := loader.LoadCompose(cmd.Context())
if err != nil {
printDefangHint("Check the files in your favorite editor.\nTo deploy the service, do "+cd+"and ", "compose up")
return nil
}
project, _ := loader.LoadCompose(cmd.Context())

envVars := collectUnsetEnvVars(project)
envInstructions := strings.Join(envVars, " ") // last line doesnt get \n
envVars := collectUnsetEnvVars(project) // ir err != nil -> proj == nil, which is handled in the collectUnsetEnvVars function
envInstructions = strings.Join(envVars, " ")

// TODO: should we use EDITOR env var instead?
cmdd := exec.Command("code", ".")
err = cmdd.Start()
if err != nil {
term.Debug("unable to launch VS Code:", err)
}
if envInstructions != "" { // check if any envars need to be set
if envInstructions != "" { // logic needs to be duplicated in case where no env vars in yaml file.
printDefangHint("Check the files in your favorite editor.\nTo deploy the service, do "+cd+"and ", "config set "+envInstructions)
} else {
printDefangHint("Check the files in your favorite editor.\nTo deploy the service, do "+cd+"and ", "compose up")
Expand All @@ -595,10 +595,12 @@ var generateCmd = &cobra.Command{

func collectUnsetEnvVars(project *proj.Project) []string {
var envVars []string
for _, service := range project.Services {
for key, value := range service.Environment {
if value == nil || *value == "" {
envVars = append(envVars, key)
if project != nil {
for _, service := range project.Services {
for key, value := range service.Environment {
if value == nil {
envVars = append(envVars, key)
}
}
}
}
Expand Down

0 comments on commit 2419aef

Please sign in to comment.