mirror of
https://gitlab.com/psuapp/psu.git
synced 2024-08-30 18:12:34 +00:00
Move config listing to new command config list
This commit is contained in:
parent
5d723af9fd
commit
1ca444a79d
@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
### Added
|
### Added
|
||||||
- `config` command to get and set configuration options.
|
- `config` command to get and set configuration options.
|
||||||
- `-l, --list` flag to list all available configuration keys.
|
- `config list|ls` command to list configuration options.
|
||||||
- `endpoint list|ls` command to print the endpoints list as a table.
|
- `endpoint list|ls` command to print the endpoints list as a table.
|
||||||
- `--format` flag to format output using a Go template.
|
- `--format` flag to format output using a Go template.
|
||||||
- `stack list|ls` command to print the stacks list as a table.
|
- `stack list|ls` command to print the stacks list as a table.
|
||||||
|
@ -3,7 +3,6 @@ FROM alpine
|
|||||||
ENV PSU_AUTHENTICATION_PASSWORD="" \
|
ENV PSU_AUTHENTICATION_PASSWORD="" \
|
||||||
PSU_AUTHENTICATION_USER="" \
|
PSU_AUTHENTICATION_USER="" \
|
||||||
PSU_CONFIG="" \
|
PSU_CONFIG="" \
|
||||||
PSU_CONFIG_LIST="" \
|
|
||||||
PSU_CONNECTION_INSECURE="" \
|
PSU_CONNECTION_INSECURE="" \
|
||||||
PSU_CONNECTION_TIMEOUT="" \
|
PSU_CONNECTION_TIMEOUT="" \
|
||||||
PSU_CONNECTION_URL="" \
|
PSU_CONNECTION_URL="" \
|
||||||
|
114
cmd/config.go
114
cmd/config.go
@ -8,7 +8,6 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// configCmd represents the config command
|
// configCmd represents the config command
|
||||||
@ -16,75 +15,63 @@ var configCmd = &cobra.Command{
|
|||||||
Use: "config KEY [VALUE]",
|
Use: "config KEY [VALUE]",
|
||||||
Short: "Get and set configuration options",
|
Short: "Get and set configuration options",
|
||||||
Example: "psu config user admin",
|
Example: "psu config user admin",
|
||||||
|
Args: cobra.MinimumNArgs(1),
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
if viper.GetBool("config.list") {
|
// Check if it's a valid key
|
||||||
// List config keys
|
var keyExists bool
|
||||||
keys := viper.AllKeys()
|
for _, key := range viper.AllKeys() {
|
||||||
sort.Slice(keys, func(i, j int) bool {
|
if key == args[0] {
|
||||||
return keys[i] < keys[j]
|
keyExists = true
|
||||||
})
|
break
|
||||||
for _, key := range keys {
|
|
||||||
fmt.Println(key)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if !keyExists {
|
||||||
|
log.Fatalf("Unkonwn configuration key \"%s\"", args[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new viper
|
||||||
|
commandViper := viper.New()
|
||||||
|
|
||||||
|
// Set config file name
|
||||||
|
var configFile string
|
||||||
|
if viper.ConfigFileUsed() != "" {
|
||||||
|
// Use config file from viper
|
||||||
|
configFile = viper.ConfigFileUsed()
|
||||||
} else {
|
} else {
|
||||||
if len(args) >= 1 {
|
// Find home directory
|
||||||
// Check if it's a valid key
|
home, err := homedir.Dir()
|
||||||
var keyExists bool
|
if err != nil {
|
||||||
for _, key := range viper.AllKeys() {
|
fmt.Println(err)
|
||||||
if key == args[0] {
|
os.Exit(1)
|
||||||
keyExists = true
|
}
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !keyExists {
|
|
||||||
log.Fatalf("Unkonwn configuration key \"%s\"", args[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create new viper
|
// Use $HOME/.psu.yaml
|
||||||
commandViper := viper.New()
|
configFile = fmt.Sprintf("%s%s.psu.yaml", home, string(os.PathSeparator))
|
||||||
|
}
|
||||||
|
commandViper.SetConfigFile(configFile)
|
||||||
|
|
||||||
// Set config file name
|
// Read config from file
|
||||||
var configFile string
|
if configReadingErr := commandViper.ReadInConfig(); configReadingErr != nil {
|
||||||
if viper.ConfigFileUsed() != "" {
|
common.PrintVerbose(fmt.Sprintf("Could not read configuration from \"%s\". Expect all configuration values to be unset.", configFile))
|
||||||
// Use config file from viper
|
}
|
||||||
configFile = viper.ConfigFileUsed()
|
|
||||||
} else {
|
|
||||||
// Find home directory
|
|
||||||
home, err := homedir.Dir()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use $HOME/.psu.yaml
|
if len(args) == 1 {
|
||||||
configFile = fmt.Sprintf("%s%s.psu.yaml", home, string(os.PathSeparator))
|
// Get config
|
||||||
}
|
fmt.Println(commandViper.Get(args[0]))
|
||||||
commandViper.SetConfigFile(configFile)
|
} else {
|
||||||
|
// Set config
|
||||||
|
commandViper.Set(args[0], args[1])
|
||||||
|
|
||||||
// Read config from file
|
// Make sure the config file exists
|
||||||
if configReadingErr := commandViper.ReadInConfig(); configReadingErr != nil {
|
_, fileCreationErr := os.Create(configFile)
|
||||||
common.PrintVerbose(fmt.Sprintf("Could not read configuration from \"%s\". Expect all configuration values to be unset.", configFile))
|
if fileCreationErr != nil {
|
||||||
}
|
common.CheckError(fileCreationErr)
|
||||||
|
}
|
||||||
|
|
||||||
if len(args) == 1 {
|
// Write te config file
|
||||||
// Get config
|
configWritingErr := commandViper.WriteConfig()
|
||||||
fmt.Println(commandViper.Get(args[0]))
|
if configWritingErr != nil {
|
||||||
} else {
|
common.CheckError(configWritingErr)
|
||||||
// Set config
|
|
||||||
commandViper.Set(args[0], args[1])
|
|
||||||
|
|
||||||
// Make sure the config file exists
|
|
||||||
_, fileCreationErr := os.Create(configFile)
|
|
||||||
if fileCreationErr != nil {
|
|
||||||
common.CheckError(fileCreationErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write te config file
|
|
||||||
configWritingErr := commandViper.WriteConfig()
|
|
||||||
if configWritingErr != nil {
|
|
||||||
common.CheckError(configWritingErr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -92,7 +79,4 @@ var configCmd = &cobra.Command{
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(configCmd)
|
rootCmd.AddCommand(configCmd)
|
||||||
|
|
||||||
configCmd.Flags().BoolP("list", "l", false, "list all config keys")
|
|
||||||
viper.BindPFlag("config.list", configCmd.Flags().Lookup("list"))
|
|
||||||
}
|
}
|
||||||
|
32
cmd/configList.go
Normal file
32
cmd/configList.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
"sort"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// configListCmd represents the list command
|
||||||
|
var configListCmd = &cobra.Command{
|
||||||
|
Use: "list",
|
||||||
|
Short: "List configs",
|
||||||
|
Aliases: []string{"ls"},
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
// Get alphabetically ordered list of config keys
|
||||||
|
keys := viper.AllKeys()
|
||||||
|
sort.Slice(keys, func(i, j int) bool {
|
||||||
|
return keys[i] < keys[j]
|
||||||
|
})
|
||||||
|
|
||||||
|
// List keys and values
|
||||||
|
for _, key := range keys {
|
||||||
|
fmt.Printf("%s: %v\n", key, viper.Get(key))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
configCmd.AddCommand(configListCmd)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user