imap-notes/cmd/config.go

67 lines
1.3 KiB
Go

package cmd
import (
"errors"
"fmt"
"git.iamthefij.com/iamthefij/slog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var ErrMissingExpectedArguments = errors.New("missing expected arguments")
const (
ConfigHostname = "hostname"
ConfigUsername = "username"
ConfigPassword = "password"
)
var configCmd = &cobra.Command{
Use: "config",
Short: "Configure imap-notes",
Long: `Get and set configuration values for IMAP Notes`,
Run: func(cmd *cobra.Command, args []string) {
slog.Infof("Running config cobra command!")
},
}
var configGetCmd = WrapPositionalArgs(
&cobra.Command{
Use: "get",
Short: "Get a config value",
Run: func(cmd *cobra.Command, args []string) {
for _, arg := range args {
fmt.Printf("%s: %s\n", arg, viper.Get(arg))
}
},
},
[]string{"key1", "key2", "..."},
1,
)
var configSetCmd = WrapPositionalArgs(
&cobra.Command{
Use: "set",
Short: "Set a config value",
ValidArgs: []string{
ConfigHostname,
ConfigUsername,
ConfigPassword,
},
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
viper.Set(args[0], args[1])
slog.OnErrFatalf(viper.WriteConfig(), "failed writing value to config")
},
},
[]string{"key", "value"},
2,
)
func addConfigCmd() {
rootCmd.AddCommand(configCmd)
configCmd.AddCommand(configGetCmd)
configCmd.AddCommand(configSetCmd)
}