Skip to content

Commit

Permalink
Add EnvFlag util
Browse files Browse the repository at this point in the history
  • Loading branch information
artemklevtsov committed Sep 16, 2023
1 parent 074ccd0 commit 67c19b4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/alecthomas/kong
require (
github.com/alecthomas/assert/v2 v2.1.0
github.com/alecthomas/repr v0.1.0
github.com/joho/godotenv v1.5.1
)

require github.com/hexops/gotextdiff v1.0.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE
github.com/alecthomas/repr v0.1.0/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
17 changes: 17 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"
"reflect"

"github.com/joho/godotenv"
)

// ConfigFlag uses the configured (via kong.Configuration(loader)) configuration loader to load configuration
Expand All @@ -26,6 +28,21 @@ func (c ConfigFlag) BeforeResolve(kong *Kong, ctx *Context, trace *Path) error {
return nil
}

// EnvFlag loads environment variables from a file specified by a flag.
//
// Use this as a flag value to support loading of environment variables from a file.
type EnvFlag string

// BeforeResolve loads env file.
func (c EnvFlag) BeforeReset(ctx *Context, trace *Path) error {
path := string(ctx.FlagValue(trace.Flag).(EnvFlag)) // nolint
path = ExpandPath(path)
if err := godotenv.Load(path); err != nil {
return err
}
return nil
}

// VersionFlag is a flag type that can be used to display a version number, stored in the "version" variable.
type VersionFlag bool

Expand Down
18 changes: 18 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ func TestConfigFlag(t *testing.T) {
assert.Equal(t, "hello world", cli.Flag)
}

func TestEnvFlag(t *testing.T) {
var cli struct {
EnvFile EnvFlag
Flag string `env:"FLAG"`
}

w, err := ioutil.TempFile("", "")
assert.NoError(t, err)
defer os.Remove(w.Name())
w.WriteString(`FLAG=hello world`) // nolint: errcheck
w.Close()

p := Must(&cli)
_, err = p.Parse([]string{"--env-file", w.Name()})
assert.NoError(t, err)
assert.Equal(t, "hello world", cli.Flag)
}

func TestVersionFlag(t *testing.T) {
var cli struct {
Version VersionFlag
Expand Down

0 comments on commit 67c19b4

Please sign in to comment.