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

fix: improve CLI remote sourcing #474

Merged
merged 1 commit into from
Mar 9, 2021
Merged
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
24 changes: 11 additions & 13 deletions cmd/client/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"fmt"
"os"
"time"

Expand All @@ -20,28 +21,25 @@ const (
EnvWriteRemote = "KETO_WRITE_REMOTE"
)

func getReadRemote(cmd *cobra.Command) string {
remote := flagx.MustGetString(cmd, FlagReadRemote)
if remote == "" {
remote = os.Getenv(EnvReadRemote)
func getRemote(cmd *cobra.Command, flagRemote, envRemote string) string {
if cmd.Flags().Changed(flagRemote) {
return flagx.MustGetString(cmd, flagRemote)
} else if remote, isSet := os.LookupEnv(envRemote); isSet {
return remote
}
return remote
}

func getWriteRemote(cmd *cobra.Command) string {
remote := flagx.MustGetString(cmd, FlagWriteRemote)
if remote == "" {
remote = os.Getenv(EnvWriteRemote)
}
// no value is set, use fallback from the flag and warn about that
remote := flagx.MustGetString(cmd, flagRemote)
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "neither flag --%s nor env var %s are set, falling back to %s\n", flagRemote, envRemote, remote)
return remote
}

func GetReadConn(cmd *cobra.Command) (*grpc.ClientConn, error) {
return Conn(cmd.Context(), getReadRemote(cmd))
return Conn(cmd.Context(), getRemote(cmd, FlagReadRemote, EnvReadRemote))
}

func GetWriteConn(cmd *cobra.Command) (*grpc.ClientConn, error) {
return Conn(cmd.Context(), getWriteRemote(cmd))
return Conn(cmd.Context(), getRemote(cmd, FlagWriteRemote, EnvWriteRemote))
}

func Conn(ctx context.Context, remote string) (*grpc.ClientConn, error) {
Expand Down
62 changes: 62 additions & 0 deletions cmd/client/grpc_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package client

import (
"bytes"
"os"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGetRemote(t *testing.T) {
setup := func() (cmd *cobra.Command, stdOut, stdErr *bytes.Buffer) {
cmd = &cobra.Command{}
stdOut, stdErr = &bytes.Buffer{}, &bytes.Buffer{}
cmd.SetOut(stdOut)
cmd.SetErr(stdErr)
RegisterRemoteURLFlags(cmd.Flags())
return
}

setEnv := func(t *testing.T, env, val string) {
require.NoError(t, os.Setenv(env, val))
t.Cleanup(func() {
require.NoError(t, os.Unsetenv(EnvReadRemote))
})
}

t.Run("case=prefers flag value", func(t *testing.T) {
cmd, stdOut, stdErr := setup()

expectedRemote := "ketotest.oryapis.com"
require.NoError(t, cmd.Flags().Set(FlagReadRemote, "ketotest.oryapis.com"))
setEnv(t, EnvReadRemote, "not"+expectedRemote)

assert.Equal(t, expectedRemote, getRemote(cmd, FlagReadRemote, EnvReadRemote))
assert.Equal(t, 0, stdOut.Len())
assert.Equal(t, 0, stdErr.Len())
})

t.Run("case=uses env value", func(t *testing.T) {
cmd, stdOut, stdErr := setup()

expectedRemote := "ketotest.oryapis.com"
setEnv(t, EnvReadRemote, expectedRemote)

assert.Equal(t, expectedRemote, getRemote(cmd, FlagReadRemote, EnvReadRemote))
assert.Equal(t, 0, stdOut.Len())
assert.Equal(t, 0, stdErr.Len())
})

t.Run("case=falls back to flag default and prints warning", func(t *testing.T) {
cmd, stdOut, stdErr := setup()

expectedRemote := cmd.Flags().Lookup(FlagReadRemote).DefValue

assert.Equal(t, expectedRemote, getRemote(cmd, FlagReadRemote, EnvReadRemote))
assert.Equal(t, 0, stdOut.Len())
assert.Contains(t, stdErr.String(), "falling back")
})
}