Skip to content

Commit

Permalink
fix: loading connector.Options
Browse files Browse the repository at this point in the history
Fix cliopts.Load to only set flag values when set by a user, not from
defaults.
  • Loading branch information
dnephin committed Apr 29, 2022
1 parent 6bdc849 commit a992e94
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions internal/cmd/cliopts/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ two: "from-file-3"
}

flags := pflag.NewFlagSet("any", pflag.ContinueOnError)
flags.String("one", "not-the-real-default", "")
flags.String("string-field", "", "")
flags.Int32("int-32-field", 0, "")
flags.Bool("bool-field", false, "")
Expand Down
4 changes: 3 additions & 1 deletion internal/cmd/cliopts/flat.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func loadFromEnv(target interface{}, opts Options) error {
func loadFromFlags(target interface{}, opts Options) error {
source := map[string]interface{}{}
opts.Flags.VisitAll(func(flag *pflag.Flag) {
source[flag.Name] = flag.Value
if flag.Changed {
source[flag.Name] = flag.Value
}
})

walker := &flatSourceWalker{
Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func newConnectorCmd() *cobra.Command {
return err
}

return connector.Run(cmd.Context(), options)
return runConnector(cmd.Context(), options)
},
}

Expand All @@ -230,6 +230,9 @@ func newConnectorCmd() *cobra.Command {
return cmd
}

// runConnector is a shim for testing
var runConnector = connector.Run

// rootOptions are options specified by users on the command line that are
// used by the root command.
type rootOptions struct {
Expand Down
43 changes: 43 additions & 0 deletions internal/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (

"gotest.tools/v3/assert"
"gotest.tools/v3/env"
"gotest.tools/v3/fs"

"github.com/infrahq/infra/api"
"github.com/infrahq/infra/internal/connector"
"github.com/infrahq/infra/uid"
)

Expand Down Expand Up @@ -266,3 +268,44 @@ func newTestClientConfig(srv *httptest.Server, identity api.Identity) ClientConf
},
}
}

func TestConnectorCmd(t *testing.T) {
var actual connector.Options
patchRunConnector(t, func(ctx context.Context, options connector.Options) error {
actual = options
return nil
})

content := `
server: the-server
name: the-name
accessKey: /var/run/secrets/key
caCert: /path/to/cert
caKey: /path/to/key
skipTLSVerify: true
`

dir := fs.NewDir(t, t.Name(), fs.WithFile("config.yaml", content))

ctx := context.Background()
err := Run(ctx, "connector", "-f", dir.Join("config.yaml"))
assert.NilError(t, err)

expected := connector.Options{
Name: "the-name",
Server: "the-server",
AccessKey: "/var/run/secrets/key",
CACert: "/path/to/cert",
CAKey: "/path/to/key",
SkipTLSVerify: true,
}
assert.DeepEqual(t, actual, expected)
}

func patchRunConnector(t *testing.T, fn func(context.Context, connector.Options) error) {
orig := runConnector
runConnector = fn
t.Cleanup(func() {
runConnector = orig
})
}

0 comments on commit a992e94

Please sign in to comment.