@@ -31,7 +31,6 @@ func AddSetEnvVarCmd(p *cobra.Command, opts GlobalOptions) {
3131 Use : "set-env" ,
3232 Short : "Set environment varariables" ,
3333 Long : `Set environment variables in a workspace from flags or a .env file.` ,
34- // BEISPIELE AKTUALISIERT
3534 Example : io .FormatExampleCommands ("set-env" , []io.Example {
3635 {Cmd : "--workspace <id> --env-var FOO=bar" , Desc : "Set a single environment variable" },
3736 {Cmd : "--workspace <id> --env-var FOO=bar --env-var HELLO=world" , Desc : "Set multiple environment variables" },
@@ -59,34 +58,55 @@ func (l *SetEnvVarCmd) RunE(_ *cobra.Command, args []string) (err error) {
5958
6059 return l .SetEnvironmentVariables (client )
6160}
61+ func ReadEnvFile (l * SetEnvVarCmd , finalEnvVarMap map [string ]string ) error {
62+
63+ envFile := * l .Opts .EnvFile
64+ if _ , err := os .Stat (envFile ); os .IsNotExist (err ) {
65+ return fmt .Errorf ("env file does not exist: %s" , envFile )
66+ }
67+
68+ fileEnvMap , err := godotenv .Read (envFile )
69+ if err != nil {
70+ return fmt .Errorf ("failed to parse env file %s: %w" , envFile , err )
71+ }
72+
73+ for key , value := range fileEnvMap {
74+ finalEnvVarMap [key ] = value
75+ }
76+
77+ return nil
78+ }
79+
80+ func ReadEnvVarArg (l * SetEnvVarCmd , finalEnvVarMap map [string ]string ) error {
81+
82+ flagEnvVarMap , err := cs .ArgToEnvVarMap (* l .Opts .EnvVar )
83+ if err != nil {
84+ return fmt .Errorf ("failed to parse environment variables from flags: %w" , err )
85+ }
86+
87+ for key , value := range flagEnvVarMap {
88+ finalEnvVarMap [key ] = value
89+ }
90+
91+ return nil
92+ }
6293
6394func (l * SetEnvVarCmd ) SetEnvironmentVariables (client Client ) (err error ) {
6495 finalEnvVarMap := make (map [string ]string )
6596
97+ // Check if the environment file is provided and read it
6698 if l .Opts .EnvFile != nil && * l .Opts .EnvFile != "" {
67- envFile := * l .Opts .EnvFile
68- if _ , err := os .Stat (envFile ); os .IsNotExist (err ) {
69- return fmt .Errorf ("env file does not exist: %s" , envFile )
70- }
71-
72- fileEnvMap , err := godotenv .Read (envFile )
99+ err := ReadEnvFile (l , finalEnvVarMap )
73100 if err != nil {
74- return fmt .Errorf ("failed to parse env file %s: %w" , envFile , err )
75- }
76-
77- for key , value := range fileEnvMap {
78- finalEnvVarMap [key ] = value
101+ return fmt .Errorf ("failed to read environment variables from file: %w" , err )
79102 }
80103 }
81104
105+ // Check if the environment variables are provided as flags and read them
82106 if l .Opts .EnvVar != nil && len (* l .Opts .EnvVar ) > 0 {
83- flagEnvVarMap , err := cs . ArgToEnvVarMap ( * l . Opts . EnvVar )
107+ err := ReadEnvVarArg ( l , finalEnvVarMap )
84108 if err != nil {
85- return fmt .Errorf ("failed to parse environment variables from flags: %w" , err )
86- }
87-
88- for key , value := range flagEnvVarMap {
89- finalEnvVarMap [key ] = value
109+ return fmt .Errorf ("failed to read environment variables from flags: %w" , err )
90110 }
91111 }
92112
0 commit comments