Skip to content

Commit d8d484e

Browse files
authored
Merge pull request #57 from dispatchrun/dotenv_support
Add dotenv support to `dispatch run` and `dispatch login` commands
2 parents ffb4996 + 1fe7359 commit d8d484e

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

cli/config.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import (
55
"errors"
66
"fmt"
77
"io"
8+
"log/slog"
89
"os"
910
"path/filepath"
1011

12+
"github.com/joho/godotenv"
1113
"github.com/pelletier/go-toml/v2"
1214
"golang.org/x/term"
1315
)
@@ -23,9 +25,15 @@ var (
2325
DispatchConsoleUrl string
2426

2527
DispatchConfigPath string
28+
29+
DotEnvFilePath string
2630
)
2731

2832
func init() {
33+
setVariables()
34+
}
35+
36+
func setVariables() {
2937
DispatchApiUrl = os.Getenv("DISPATCH_API_URL")
3038
if DispatchApiUrl == "" {
3139
DispatchApiUrl = "https://api.dispatch.run"
@@ -144,3 +152,18 @@ func runConfigFlow() error {
144152
}
145153
return nil
146154
}
155+
156+
func loadEnvFromFile(path string) error {
157+
if path != "" {
158+
absolutePath, err := filepath.Abs(path)
159+
if err != nil {
160+
return fmt.Errorf("failed to get absolute path for %s: %v", path, err)
161+
}
162+
if err := godotenv.Load(path); err != nil {
163+
return fmt.Errorf("failed to load env file from %s: %v", absolutePath, err)
164+
}
165+
slog.Info("loading environment variables from file", "path", absolutePath)
166+
}
167+
setVariables()
168+
return nil
169+
}

cli/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ Documentation: https://docs.dispatch.run
1919
Discord: https://dispatch.run/discord
2020
Support: support@dispatch.run
2121
`,
22+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
23+
return loadEnvFromFile(DotEnvFilePath)
24+
},
2225
RunE: func(cmd *cobra.Command, args []string) error {
2326
return cmd.Help()
2427
},
2528
}
2629
cmd.PersistentFlags().StringVarP(&DispatchApiKeyCli, "api-key", "k", "", "Dispatch API key (env: DISPATCH_API_KEY)")
30+
cmd.PersistentFlags().StringVarP(&DotEnvFilePath, "env-file", "", "", "Path to .env file")
2731

2832
cmd.AddGroup(&cobra.Group{
2933
ID: "management",

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)