Skip to content

Commit 29e537b

Browse files
authored
Add env value check to project name detection (#392)
* check if project id is set in env variables * changed function for getting env variable value
1 parent 3653ed6 commit 29e537b

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

internal/pkg/projectname/project_name.go

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

78
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
89
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
@@ -41,7 +42,7 @@ func GetProjectName(ctx context.Context, p *print.Printer, cmd *cobra.Command) (
4142

4243
// If project ID is set in config, we store the project name in config
4344
// (So next time we can just pull it from there)
44-
if !isProjectIdSetInFlags(p, cmd) {
45+
if !(isProjectIdSetInFlags(p, cmd) || isProjectIdSetInEnvVar()) {
4546
viper.Set(config.ProjectNameKey, projectName)
4647
err = config.Write()
4748
if err != nil {
@@ -57,13 +58,14 @@ func useProjectNameFromConfig(p *print.Printer, cmd *cobra.Command) bool {
5758
// We use the project name from the config file, if:
5859
// - Project id is not set to a different value than the one in the config file
5960
// - Project name in the config file is not empty
60-
projectIdSet := isProjectIdSetInFlags(p, cmd)
61+
projectIdSetInFlags := isProjectIdSetInFlags(p, cmd)
62+
projectIdSetInEnv := isProjectIdSetInEnvVar()
6163
projectName := viper.GetString(config.ProjectNameKey)
6264
projectNameSet := false
6365
if projectName != "" {
6466
projectNameSet = true
6567
}
66-
return !projectIdSet && projectNameSet
68+
return !projectIdSetInFlags && !projectIdSetInEnv && projectNameSet
6769
}
6870

6971
func isProjectIdSetInFlags(p *print.Printer, cmd *cobra.Command) bool {
@@ -77,3 +79,9 @@ func isProjectIdSetInFlags(p *print.Printer, cmd *cobra.Command) bool {
7779
}
7880
return projectIdSetInFlag
7981
}
82+
83+
func isProjectIdSetInEnvVar() bool {
84+
// Reads the project Id from the environment variable PROJECT_ID
85+
_, projectIdSetInEnv := os.LookupEnv("STACKIT_PROJECT_ID")
86+
return projectIdSetInEnv
87+
}

0 commit comments

Comments
 (0)