From 1cc88b599e1ae5f96e31f561643251c3db8b8a01 Mon Sep 17 00:00:00 2001 From: muhmuhhum Date: Sat, 17 Jul 2021 10:53:42 +0200 Subject: [PATCH 1/3] Add config get command --- cli/config.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ cli/root.go | 1 + 2 files changed, 69 insertions(+) create mode 100644 cli/config.go diff --git a/cli/config.go b/cli/config.go new file mode 100644 index 0000000..01d0fa9 --- /dev/null +++ b/cli/config.go @@ -0,0 +1,68 @@ +package cli + +import ( + "github.com/dominikbraun/timetrace/core" + "github.com/dominikbraun/timetrace/out" + "github.com/spf13/cobra" + "strconv" +) + +func configCommand(t *core.Timetrace) *cobra.Command { + configCmd := &cobra.Command{ + Use: "config", + Short: "Manage configs", + Run: func(cmd *cobra.Command, args []string) { + _ = cmd.Help() + }, + } + + configCmd.AddCommand(setConfigCommand(t)) + configCmd.AddCommand(getConfigCommand(t)) + return configCmd +} + +func setConfigCommand(t *core.Timetrace) *cobra.Command { + config := &cobra.Command{ + Use: "set ", + Short: "Set config value", + Args: cobra.ExactArgs(2), + Run: func(cmd *cobra.Command, args []string) { + + }, + } + return config +} + +func getConfigCommand(t *core.Timetrace) *cobra.Command { + config := &cobra.Command{ + Use: "get ", + Short: "Get config values", + Args: cobra.MaximumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + configKeys := make([][]string, 4) + configKeys[0] = createLine("store", t.Config().Store) + configKeys[1] = createLine("editor", t.Config().Editor) + configKeys[2] = createLine("reportPath", t.Config().ReportPath) + configKeys[3] = createLine("use12Hours", strconv.FormatBool(t.Config().Use12Hours)) + if len(args) == 0 { + + out.Table([]string{"Key", "Value"}, configKeys, nil) + return + } + for _, configKey := range configKeys { + if configKey[0] == args[0] { + slice := configKeys[0:1] + out.Table([]string{"Key", "Value"}, slice, nil) + } + } + }, + } + return config +} + +func createLine(key string, value string) []string { + line := make([]string, 2) + line[0] = key + line[1] = value + return line +} diff --git a/cli/root.go b/cli/root.go index 3b570d9..403fb79 100644 --- a/cli/root.go +++ b/cli/root.go @@ -34,6 +34,7 @@ func RootCommand(t *core.Timetrace, version string) *cobra.Command { root.AddCommand(startCommand(t)) root.AddCommand(statusCommand(t)) root.AddCommand(stopCommand(t)) + root.AddCommand(configCommand(t)) root.AddCommand(generateReportCommand(t)) root.AddCommand(versionCommand(version)) From 4d4b32bb3e82119ba7a0496f652a11c8eab95b10 Mon Sep 17 00:00:00 2001 From: muhmuhhum Date: Sat, 17 Jul 2021 11:07:46 +0200 Subject: [PATCH 2/3] Fix goimports and go fmt --- cli/config.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cli/config.go b/cli/config.go index 01d0fa9..fadf2de 100644 --- a/cli/config.go +++ b/cli/config.go @@ -1,10 +1,11 @@ package cli import ( + "strconv" + "github.com/dominikbraun/timetrace/core" "github.com/dominikbraun/timetrace/out" "github.com/spf13/cobra" - "strconv" ) func configCommand(t *core.Timetrace) *cobra.Command { From ddc3b1470da24b42b97f0b64ebd80b411af1f983 Mon Sep 17 00:00:00 2001 From: muhmuhhum Date: Sun, 18 Jul 2021 19:29:55 +0200 Subject: [PATCH 3/3] Change config get to use reflection --- cli/config.go | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/cli/config.go b/cli/config.go index fadf2de..e35151e 100644 --- a/cli/config.go +++ b/cli/config.go @@ -1,7 +1,9 @@ package cli import ( + "reflect" "strconv" + "strings" "github.com/dominikbraun/timetrace/core" "github.com/dominikbraun/timetrace/out" @@ -40,30 +42,31 @@ func getConfigCommand(t *core.Timetrace) *cobra.Command { Short: "Get config values", Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - configKeys := make([][]string, 4) - configKeys[0] = createLine("store", t.Config().Store) - configKeys[1] = createLine("editor", t.Config().Editor) - configKeys[2] = createLine("reportPath", t.Config().ReportPath) - configKeys[3] = createLine("use12Hours", strconv.FormatBool(t.Config().Use12Hours)) + header := []string{"Key", "Value"} + valueOfConfig := reflect.ValueOf(t.Config()) + typeOfConfig := reflect.Indirect(valueOfConfig).Type() + values := make([][]string, reflect.Indirect(valueOfConfig).NumField()) + for i := 0; i < reflect.Indirect(valueOfConfig).NumField(); i++ { + switch reflect.Indirect(valueOfConfig).Field(i).Interface().(type) { + case bool: + values[i] = []string{typeOfConfig.Field(i).Name, strconv.FormatBool(reflect.Indirect(valueOfConfig).Field(i).Bool())} + default: + values[i] = []string{typeOfConfig.Field(i).Name, reflect.Indirect(valueOfConfig).Field(i).String()} + } + } if len(args) == 0 { - - out.Table([]string{"Key", "Value"}, configKeys, nil) + out.Table(header, values, nil) return } - for _, configKey := range configKeys { - if configKey[0] == args[0] { - slice := configKeys[0:1] - out.Table([]string{"Key", "Value"}, slice, nil) + for _, configSetting := range values { + + if strings.ToUpper(configSetting[0]) == strings.ToUpper(args[0]) { + out.Table(header, [][]string{configSetting}, nil) + return } } + }, } return config } - -func createLine(key string, value string) []string { - line := make([]string, 2) - line[0] = key - line[1] = value - return line -}