Skip to content

Commit faae232

Browse files
committed
Add dotenv support to run and login commands
1 parent 7fdf712 commit faae232

File tree

7 files changed

+40
-0
lines changed

7 files changed

+40
-0
lines changed

cli/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@ var (
2323
DispatchConsoleUrl string
2424

2525
DispatchConfigPath string
26+
27+
DotEnvFilePath string
2628
)
2729

2830
func init() {
31+
setVariables()
32+
}
33+
34+
func setVariables() {
2935
DispatchApiUrl = os.Getenv("DISPATCH_API_URL")
3036
if DispatchApiUrl == "" {
3137
DispatchApiUrl = "https://api.dispatch.run"

cli/env_file.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"log/slog"
6+
"path/filepath"
7+
8+
"github.com/joho/godotenv"
9+
)
10+
11+
func loadEnvFromFile(path string) error {
12+
if path != "" {
13+
absolutePath, err := filepath.Abs(path)
14+
if err != nil {
15+
return fmt.Errorf("failed to get absolute path for %s: %v", path, err)
16+
}
17+
if godotenv.Load(path) != nil {
18+
return fmt.Errorf("failed to load env file from %s", absolutePath)
19+
}
20+
slog.Info("loading environment variables from file", "path", absolutePath)
21+
}
22+
setVariables()
23+
return nil
24+
}

cli/login.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ account or login to an existing account.
2222
2323
After authenticating with Dispatch, the API key will be persisted locally.`,
2424
GroupID: "management",
25+
PreRunE: func(cmd *cobra.Command, args []string) error {
26+
return loadEnvFromFile(DotEnvFilePath)
27+
},
2528
RunE: func(cmd *cobra.Command, args []string) error {
2629
token, err := generateToken()
2730
if err != nil {

cli/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Support: [email protected]
2424
},
2525
}
2626
cmd.PersistentFlags().StringVarP(&DispatchApiKeyCli, "api-key", "k", "", "Dispatch API key (env: DISPATCH_API_KEY)")
27+
cmd.PersistentFlags().StringVarP(&DotEnvFilePath, "env-file", "", "", "Path to .env file")
2728

2829
cmd.AddGroup(&cobra.Group{
2930
ID: "management",

cli/run.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ previous run.`, defaultEndpoint),
8484
SilenceUsage: true,
8585
GroupID: "dispatch",
8686
PreRunE: func(cmd *cobra.Command, args []string) error {
87+
if err := loadEnvFromFile(DotEnvFilePath); err != nil {
88+
return err
89+
}
8790
return runConfigFlow()
8891
},
8992
RunE: func(c *cobra.Command, args []string) error {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
2222
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
2323
github.com/inconshreveable/mousetrap v1.1.0 // indirect
24+
github.com/joho/godotenv v1.5.1 // indirect
2425
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
2526
github.com/mattn/go-isatty v0.0.18 // indirect
2627
github.com/mattn/go-localereader v0.0.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
2323
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
2424
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
2525
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
26+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
27+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
2628
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
2729
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
2830
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=

0 commit comments

Comments
 (0)