Add get-config.go

This commit is contained in:
Finn 2021-02-03 02:14:39 -08:00
parent 9a104c051b
commit 5b623a2bd5
3 changed files with 78 additions and 0 deletions

View file

@ -0,0 +1,71 @@
// Copyright © 2021 Finn Herzfeld <finn@janky.solutions>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package get
import (
"encoding/json"
"log"
"os"
"reflect"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"gitlab.com/signald/signald-go/cmd/signaldctl/common"
"gitlab.com/signald/signald-go/cmd/signaldctl/config"
)
var (
GetConfigCmd = &cobra.Command{
Use: "get",
Short: "get signaldctl configuration values",
Annotations: map[string]string{common.AnnotationNoSocketConnection: "true"},
Run: func(_ *cobra.Command, _ []string) {
switch common.OutputFormat {
case common.OutputFormatJSON:
err := json.NewEncoder(os.Stdout).Encode(config.Config)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
case common.OutputFormatYAML:
err := yaml.NewEncoder(os.Stdout).Encode(config.Config)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault:
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
v := reflect.ValueOf(config.Config)
ty := reflect.TypeOf(config.Config)
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
t.AppendRow(table.Row{ty.Field(i).Name, f.String()})
}
if common.OutputFormat == common.OutputFormatCSV {
t.RenderCSV()
} else {
t.SetStyle(table.StyleLight)
t.Render()
}
default:
log.Fatal("Unsupported output format")
}
},
}
)

View file

@ -18,6 +18,7 @@ package configcmd
import (
"github.com/spf13/cobra"
"gitlab.com/signald/signald-go/cmd/signaldctl/cmd/config/get"
"gitlab.com/signald/signald-go/cmd/signaldctl/cmd/config/set"
)
@ -27,4 +28,5 @@ var ConfigCmd = &cobra.Command{
func init() {
ConfigCmd.AddCommand(set.SetConfigCmd)
ConfigCmd.AddCommand(get.GetConfigCmd)
}

View file

@ -22,6 +22,8 @@ import (
"gopkg.in/yaml.v2"
)
// Configuration is the signaldctl configuration
// remember to update cmd/signaldctl/cmd/config/set when adding here
type Configuration struct {
// DefaultAccount will be used if --account is not provided
DefaultAccount string
@ -31,6 +33,9 @@ type Configuration struct {
}
var Path string
// Config is the variable the configuration gets loaded into
// values shown here are used as defaults
var Config = Configuration{
SocketPath: "/var/run/signald/signald.sock",
}