-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
943 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"reflect" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/hetznercloud/cli/internal/cmd/cmpl" | ||
"github.com/hetznercloud/cli/internal/cmd/util" | ||
"github.com/hetznercloud/cli/internal/state" | ||
"github.com/hetznercloud/cli/internal/state/config" | ||
) | ||
|
||
func NewAddCommand(s state.State) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "add <key> <value>...", | ||
Short: "Set a configuration value", | ||
Args: util.Validate, | ||
TraverseChildren: true, | ||
DisableFlagsInUseLine: true, | ||
RunE: state.Wrap(s, runAdd), | ||
ValidArgsFunction: cmpl.NoFileCompletion(cmpl.SuggestArgs( | ||
cmpl.SuggestCandidatesF(func() []string { | ||
var keys []string | ||
for key, opt := range config.Options { | ||
if opt.HasFlag(config.OptionFlagPreference) { | ||
keys = append(keys, key) | ||
} | ||
} | ||
return keys | ||
}), | ||
cmpl.SuggestCandidatesCtx(func(_ *cobra.Command, args []string) []string { | ||
var comps []string | ||
if opt, ok := config.Options[args[0]]; ok { | ||
comps = opt.Completions() | ||
} | ||
return comps | ||
}), | ||
)), | ||
} | ||
cmd.Flags().Bool("global", false, "Set the value globally (for all contexts)") | ||
return cmd | ||
} | ||
|
||
func runAdd(s state.State, cmd *cobra.Command, args []string) error { | ||
global, _ := cmd.Flags().GetBool("global") | ||
|
||
var prefs config.Preferences | ||
|
||
if global { | ||
prefs = s.Config().Preferences() | ||
} else { | ||
ctx := s.Config().ActiveContext() | ||
if reflect.ValueOf(ctx).IsNil() { | ||
return fmt.Errorf("no active context (use --global flag to set a global option)") | ||
} | ||
prefs = ctx.Preferences() | ||
} | ||
|
||
key, values := args[0], args[1:] | ||
if err := prefs.Add(key, values); err != nil { | ||
return err | ||
} | ||
|
||
_ = s.Config().Write(nil) | ||
return s.Config().Write(os.Stdout) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/hetznercloud/cli/internal/cmd/util" | ||
"github.com/hetznercloud/cli/internal/state" | ||
"github.com/hetznercloud/cli/internal/state/config" | ||
) | ||
|
||
func NewGetCommand(s state.State) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "get <key>", | ||
Short: "Get a configuration value", | ||
Args: util.Validate, | ||
TraverseChildren: true, | ||
DisableFlagsInUseLine: true, | ||
RunE: state.Wrap(s, runGet), | ||
} | ||
cmd.Flags().Bool("global", false, "Get the value globally") | ||
cmd.Flags().Bool("allow-sensitive", false, "Allow showing sensitive values") | ||
return cmd | ||
} | ||
|
||
func runGet(s state.State, cmd *cobra.Command, args []string) error { | ||
global, _ := cmd.Flags().GetBool("global") | ||
allowSensitive, _ := cmd.Flags().GetBool("allow-sensitive") | ||
|
||
if global { | ||
viper.Reset() | ||
config.ResetFlags() | ||
viper.Set("context", nil) | ||
if err := s.Config().ParseConfig(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
key := args[0] | ||
var opt config.IOption | ||
for name, o := range config.Options { | ||
if name == key { | ||
opt = o | ||
break | ||
} | ||
} | ||
if opt == nil { | ||
return fmt.Errorf("unknown key: %s", key) | ||
} | ||
|
||
val := opt.ValueAny() | ||
if opt.HasFlag(config.OptionFlagSensitive) && !allowSensitive { | ||
return fmt.Errorf("'%s' is sensitive. use --allow-sensitive to show the value", key) | ||
} | ||
cmd.Println(val) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package config_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
configCmd "github.com/hetznercloud/cli/internal/cmd/config" | ||
"github.com/hetznercloud/cli/internal/testutil" | ||
) | ||
|
||
func TestGet(t *testing.T) { | ||
type testCase struct { | ||
key string | ||
args []string | ||
expOut string | ||
expErr string | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
key: "context", | ||
expOut: "test_context\n", | ||
}, | ||
{ | ||
key: "debug", | ||
expOut: "true\n", | ||
}, | ||
{ | ||
key: "endpoint", | ||
expOut: "https://test-endpoint.com\n", | ||
}, | ||
{ | ||
key: "poll-interval", | ||
expOut: "1.234s\n", | ||
}, | ||
} | ||
|
||
for _, tt := range testCases { | ||
t.Run(tt.key, func(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := configCmd.NewGetCommand(fx.State()) | ||
|
||
setTestValues() | ||
out, errOut, err := fx.Run(cmd, append(tt.args, tt.key)) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expErr, errOut) | ||
assert.Equal(t, tt.expOut, out) | ||
}) | ||
} | ||
} |
Oops, something went wrong.