Add config command

This commit is contained in:
2026-02-26 07:49:01 -06:00
parent dbaa6a33a7
commit 8a8afffced
7 changed files with 70 additions and 6 deletions

64
cmd/config.go Normal file
View File

@@ -0,0 +1,64 @@
/*
Copyright © Brian Buller <brian@bullercodeworks.com>
*/
package cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// configCmd represents the config command
var configCmd = &cobra.Command{
Use: "config",
Short: "View/Edit Config",
RunE: func(cmd *cobra.Command, args []string) error {
setVal := func(k, v string) error {
viper.Set(k, v)
return viper.WriteConfig()
}
switch len(args) {
case 0:
// List all config keys
all := viper.AllKeys()
for i := range all {
fmt.Printf("%s: %s\n", all[i], viper.Get(all[i]))
}
return nil
case 1:
k := args[0]
if strings.Contains(k, "=") {
// This is actually a set
pts := strings.Split(k, "=")
if viper.IsSet(pts[0]) {
return setVal(pts[0], pts[1])
} else {
return fmt.Errorf("no config value found for %s", pts[0])
}
return nil
}
// Show a config key/value
if viper.IsSet(k) {
fmt.Printf("%s: %s\n", k, viper.Get(k))
} else {
return fmt.Errorf("no config value found for %s", k)
}
return nil
case 2:
// Setting a config value
if !viper.IsSet(args[0]) {
return fmt.Errorf("no config value found for %s", args[0])
}
return setVal(args[0], args[1])
}
return nil
},
}
func init() {
rootCmd.AddCommand(configCmd)
}

View File

@@ -1,5 +1,5 @@
/*
Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
Copyright © Brian Buller <brian@bullercodeworks.com>
*/
package cmd

View File

@@ -1,5 +1,5 @@
/*
Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
Copyright © Brian Buller <brian@bullercodeworks.com>
*/
package cmd

View File

@@ -1,5 +1,5 @@
/*
Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
Copyright © Brian Buller <brian@bullercodeworks.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -1,5 +1,5 @@
/*
Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
Copyright © Brian Buller <brian@bullercodeworks.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -1,5 +1,5 @@
/*
Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
Copyright © Brian Buller <brian@bullercodeworks.com>
*/
package cmd

View File

@@ -1,5 +1,5 @@
/*
Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
Copyright © Brian Buller <brian@bullercodeworks.com>
*/
package cmd