Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EnvFlag util #378

Merged
merged 5 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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