diff --git a/cmd/signaldctl/cmd/config/get/get-config.go b/cmd/signaldctl/cmd/config/get/get-config.go new file mode 100644 index 0000000..570b7e3 --- /dev/null +++ b/cmd/signaldctl/cmd/config/get/get-config.go @@ -0,0 +1,71 @@ +// Copyright © 2021 Finn Herzfeld +// +// 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 . + +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") + } + }, + } +) diff --git a/cmd/signaldctl/cmd/config/root.go b/cmd/signaldctl/cmd/config/root.go index 6c81bbf..02680f8 100644 --- a/cmd/signaldctl/cmd/config/root.go +++ b/cmd/signaldctl/cmd/config/root.go @@ -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) } diff --git a/cmd/signaldctl/config/config.go b/cmd/signaldctl/config/config.go index 494d0e9..5883ec7 100644 --- a/cmd/signaldctl/config/config.go +++ b/cmd/signaldctl/config/config.go @@ -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", }