-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for config contexts: move viper access to new config pack… (
#140) * add support for config contexts
- Loading branch information
Showing
21 changed files
with
901 additions
and
287 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 was deleted.
Oops, something went wrong.
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,18 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func ConfigRootCmd(fs afero.Fs) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "config", | ||
Short: "sub-commands relating to CLI config", | ||
} | ||
|
||
// Child root commands | ||
cmd.AddCommand(configContextRootCmd(fs)) | ||
|
||
return cmd | ||
} |
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,168 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
"github.com/ukfast/cli/internal/pkg/config" | ||
"github.com/ukfast/cli/internal/pkg/output" | ||
) | ||
|
||
func configContextRootCmd(fs afero.Fs) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "context", | ||
Short: "sub-commands relating to CLI config", | ||
} | ||
|
||
// Child commands | ||
cmd.AddCommand(configContextUpdateCmd(fs)) | ||
cmd.AddCommand(configContextListCmd()) | ||
cmd.AddCommand(configContextSwitchCmd(fs)) | ||
|
||
return cmd | ||
} | ||
|
||
func configContextUpdateCmd(fs afero.Fs) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "update", | ||
Short: "Updates context configuration", | ||
Long: "This command updates context configuration", | ||
Example: "ukfast config context update mycontext --api-key \"secretkey\"", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return configContextUpdate(fs, cmd, args) | ||
}, | ||
} | ||
|
||
// Setup flags | ||
cmd.Flags().Bool("current", false, "Specifies that current context should be updated") | ||
cmd.Flags().String("api-key", "", "Specifies API key") | ||
cmd.Flags().Int("api-timeout-seconds", 0, "Specifies API timeout in seconds") | ||
cmd.Flags().String("api-uri", "", "Specifies API URI") | ||
cmd.Flags().Bool("api-insecure", false, "Specifies API TLS validation should be disabled") | ||
cmd.Flags().Bool("api-debug", false, "Specifies API debug logging should be enabled") | ||
cmd.Flags().Int("api-pagination-perpage", 0, "Specifies how many items should be retrieved per-page for paginated API requests") | ||
cmd.Flags().Int("command-wait-timeout-seconds", 0, "Specifies how long commands supporting 'wait' parameter should wait") | ||
cmd.Flags().Int("command-wait-sleep-seconds", 0, "Specifies how often commands supporting 'wait' parameter should poll") | ||
|
||
return cmd | ||
} | ||
|
||
func configContextUpdate(fs afero.Fs, cmd *cobra.Command, args []string) error { | ||
updated := false | ||
|
||
updateCurrentContext, _ := cmd.Flags().GetBool("current") | ||
|
||
set := func(name string, flagName string, value interface{}) { | ||
if cmd.Flags().Changed(flagName) { | ||
if updateCurrentContext { | ||
err := config.SetCurrentContext(name, value) | ||
if err != nil { | ||
output.Fatalf("failed to update current context: %s", err) | ||
} | ||
} else { | ||
for _, context := range args { | ||
config.Set(context, name, value) | ||
} | ||
} | ||
updated = true | ||
} | ||
} | ||
|
||
apiKey, _ := cmd.Flags().GetString("api-key") | ||
set("api_key", "api-key", apiKey) | ||
apiTimeoutSeconds, _ := cmd.Flags().GetInt("api-timeout-seconds") | ||
set("api_timeout_seconds", "api-timeout-seconds", apiTimeoutSeconds) | ||
apiURI, _ := cmd.Flags().GetString("api-uri") | ||
set("api_uri", "api-uri", apiURI) | ||
apiInsecure, _ := cmd.Flags().GetBool("api-insecure") | ||
set("api_insecure", "api-insecure", apiInsecure) | ||
apiDebug, _ := cmd.Flags().GetBool("api-debug") | ||
set("api_debug", "api-debug", apiDebug) | ||
apiPaginationPerPage, _ := cmd.Flags().GetInt("api-pagination-perpage") | ||
set("api_pagination_perpage", "api-pagination-perpage", apiPaginationPerPage) | ||
commandWaitTimeoutSeconds, _ := cmd.Flags().GetInt("command-wait-timeout-seconds") | ||
set("command_wait_timeout_seconds", "command-wait-timeout-seconds", commandWaitTimeoutSeconds) | ||
commandWaitSleepSeconds, _ := cmd.Flags().GetInt("command-wait-sleep-seconds") | ||
set("command_wait_sleep_seconds", "command-wait-sleep-seconds", commandWaitSleepSeconds) | ||
|
||
if updated { | ||
config.SetFs(fs) | ||
return config.Save() | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func configContextListCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "list", | ||
Short: "Lists contexts", | ||
Long: "This command lists contexts", | ||
Example: "ukfast config context list", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return configContextList(cmd) | ||
}, | ||
} | ||
} | ||
|
||
func configContextList(cmd *cobra.Command) error { | ||
contextNames := config.GetContextNames() | ||
currentContextName := config.GetCurrentContextName() | ||
|
||
var data []interface{} | ||
var fields []*output.OrderedFields | ||
for _, contextName := range contextNames { | ||
activeContext := contextName == currentContextName | ||
data = append(data, struct { | ||
Name string `json:"name"` | ||
Active bool `json:"active"` | ||
}{ | ||
Name: contextName, | ||
Active: activeContext, | ||
}) | ||
field := output.NewOrderedFields() | ||
field.Set("name", output.NewFieldValue(contextName, true)) | ||
field.Set("active", output.NewFieldValue(strconv.FormatBool(activeContext), true)) | ||
fields = append(fields, field) | ||
} | ||
|
||
return output.CommandOutput(cmd, output.NewGenericOutputHandlerDataProvider( | ||
output.WithData(data), | ||
output.WithFieldDataFunc(func() ([]*output.OrderedFields, error) { | ||
return fields, nil | ||
}), | ||
)) | ||
} | ||
|
||
func configContextSwitchCmd(fs afero.Fs) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "switch", | ||
Short: "Switches current context", | ||
Long: "This command switches the current context", | ||
Example: "ukfast config context switch mycontext", | ||
Aliases: []string{"use"}, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return errors.New("Missing context") | ||
} | ||
|
||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return configContextSwitch(fs, cmd, args) | ||
}, | ||
} | ||
} | ||
|
||
func configContextSwitch(fs afero.Fs, cmd *cobra.Command, args []string) error { | ||
err := config.SwitchCurrentContext(args[0]) | ||
if err != nil { | ||
return fmt.Errorf("failed to switch context: %s", err) | ||
} | ||
|
||
config.SetFs(fs) | ||
return config.Save() | ||
} |
Oops, something went wrong.