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

Load environment variables as a resolver #480

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func New(grammar interface{}, options ...Option) (*Kong, error) {
},
}

options = append(options, Bind(k))
options = append(options, Bind(k), Resolvers(EnvResolver()))

for _, option := range options {
if err := option.Apply(k); err != nil {
Expand Down
14 changes: 0 additions & 14 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package kong
import (
"fmt"
"math"
"os"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -377,19 +376,6 @@ func (v *Value) ApplyDefault() error {
// Does not include resolvers.
func (v *Value) Reset() error {
v.Target.Set(reflect.Zero(v.Target.Type()))
if len(v.Tag.Envs) != 0 {
for _, env := range v.Tag.Envs {
envar, ok := os.LookupEnv(env)
// Parse the first non-empty ENV in the list
if ok {
err := v.Parse(ScanFromTokens(Token{Type: FlagValueToken, Value: envar}), v.Target)
if err != nil {
return fmt.Errorf("%s (from envar %s=%q)", err, env, envar)
}
return nil
}
}
}
if v.HasDefault {
return v.Parse(ScanFromTokens(Token{Type: FlagValueToken, Value: v.Default}), v.Target)
}
Expand Down
62 changes: 62 additions & 0 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package kong

import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
)

Expand Down Expand Up @@ -66,3 +68,63 @@ func snakeCase(name string) string {
name = strings.Join(strings.Split(strings.Title(name), "-"), "")
return strings.ToLower(name[:1]) + name[1:]
}

func EnvResolver() Resolver {
once := Once()
return ResolverFunc(func(context *Context, parent *Path, flag *Flag) (interface{}, error) {
if err := once(func() error { return visit(context.Path) }); err != nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this once function?

return nil, err
}
for _, env := range flag.Tag.Envs {
envar, ok := os.LookupEnv(env)
// Parse the first non-empty ENV in the list
if ok {
return envar, nil
}
}
return nil, nil
})
}

func visit(paths []*Path) error {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be called something more specific than "visit". Same with function below.

for _, path := range paths {
if path.Command == nil {
continue
}
for _, positional := range path.Command.Positional {
if positional.Tag == nil {
continue
}
visitValue(positional)
}
if path.Command.Argument != nil {
visitValue(path.Command.Argument)
}
}
return nil
}

func visitValue(value *Value) error {
for _, env := range value.Tag.Envs {
envar, ok := os.LookupEnv(env)
if !ok {
continue
}
token := Token{Type: FlagValueToken, Value: envar}
if err := value.Parse(ScanFromTokens(token), value.Target); err != nil {
return fmt.Errorf("%s (from envar %s=%q)", err, env, envar)
}
}
return nil
}

func Once() func(func() error) error {
done := false
return func(fn func() error) error {
if !done {
done = true
return fn()
}
return nil
}
}
Loading