-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from wunderio/feature/config-proxy
Persistent configuration; Load and use proxy configuration.
- Loading branch information
Showing
8 changed files
with
207 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/wunderio/silta-cli/internal/common" | ||
) | ||
|
||
var configFile = common.ConfigStore() | ||
|
||
// configCmd represents the config command | ||
var configCmd = &cobra.Command{ | ||
Use: "config", | ||
Short: "Silta configuration commands", | ||
Long: `Silta configuration commands, allows setting and getting configuration values. | ||
Configuration is persistent and is stored in file "` + configFile.ConfigFileUsed() + `".`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println(cmd.Usage()) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(configCmd) | ||
} |
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,44 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/wunderio/silta-cli/internal/common" | ||
) | ||
|
||
var configGetCmd = &cobra.Command{ | ||
Use: "get", | ||
Short: "Get configuration", | ||
Long: `This will print configuration information. If no arguments are provided, the entire configuration file will be printed. | ||
If a single argument is provided, the value of the configuration key will be printed. Supports nested keys using dot notation.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
configStore := common.ConfigStore() | ||
|
||
if len(args) < 1 { | ||
cfg := configStore.ConfigFileUsed() | ||
// Read raw file content and print it | ||
content, err := os.ReadFile(cfg) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
fmt.Printf("%s", content) | ||
|
||
} | ||
|
||
if len(args) == 1 { | ||
// Print single configuration item | ||
key := args[0] | ||
value := configStore.Get(key) | ||
if value != nil { | ||
fmt.Printf("%s", value) | ||
} | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
configCmd.AddCommand(configGetCmd) | ||
} |
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,43 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/wunderio/silta-cli/internal/common" | ||
) | ||
|
||
var configSetCmd = &cobra.Command{ | ||
Use: "set", | ||
Short: "Set configuration", | ||
Long: `This will set configuration information. The first argument is the key and the second argument is the value. | ||
If the key already exists, the value will be overwritten. Supports nested keys using dot notation. | ||
Usage: silta config set <key> <value> | ||
Example: silta config set mykey | ||
Example: silta config set mykey myvalue | ||
Example: silta config set mykey.subkey myvalue | ||
`, | ||
Args: cobra.MinimumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
key := args[0] | ||
value := strings.Join(args[1:], " ") | ||
|
||
configStore := common.ConfigStore() | ||
configStore.Set(key, value) | ||
|
||
// Create a new configuration file if it doesn't exist | ||
err := configStore.WriteConfig() | ||
if err != nil { | ||
log.Fatalf("Error writing config file, %s", err) | ||
} | ||
|
||
fmt.Println("Configuration set") | ||
}, | ||
} | ||
|
||
func init() { | ||
configCmd.AddCommand(configSetCmd) | ||
} |
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,52 @@ | ||
package common | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"os/user" | ||
"path/filepath" | ||
"runtime" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
func ConfigStore() viper.Viper { | ||
|
||
// Default configuration subpath | ||
siltaConfigDir := ".config/silta" | ||
|
||
// running on Windows | ||
if runtime.GOOS == "windows" { | ||
siltaConfigDir = filepath.Join("AppData", "Local", "silta") | ||
} | ||
|
||
// running on MacOS | ||
if runtime.GOOS == "darwin" { | ||
siltaConfigDir = "Library/Application Support/silta" | ||
} | ||
|
||
// Get the user's home directory | ||
usr, err := user.Current() | ||
if err != nil { | ||
log.Fatalf("Error getting user home directory, %s", err) | ||
} | ||
configDir := filepath.Join(usr.HomeDir, siltaConfigDir) | ||
|
||
// Create the configuration directory if it doesn't exist | ||
_, err = os.Stat(configDir) | ||
if !os.IsExist(err) { | ||
err = os.MkdirAll(configDir, 0700) | ||
if err != nil { | ||
log.Fatalf("Error creating config directory, %s", err) | ||
} | ||
} | ||
|
||
// Set the configuration file | ||
viper.SetConfigFile(filepath.Join(configDir, "config.yaml")) | ||
viper.AddConfigPath(configDir) | ||
|
||
// Read the configuration file if it exists | ||
viper.ReadInConfig() | ||
|
||
return *viper.GetViper() | ||
} |