diff --git a/pkg/cmd/time-entry/defaults/show/show.go b/pkg/cmd/time-entry/defaults/show/show.go index 5fbb9e88..4c39b95e 100644 --- a/pkg/cmd/time-entry/defaults/show/show.go +++ b/pkg/cmd/time-entry/defaults/show/show.go @@ -1,12 +1,56 @@ package show import ( + "encoding/json" + "errors" + "io" + "strings" + + "github.com/lucassabreu/clockify-cli/pkg/cmd/time-entry/util/defaults" + "github.com/lucassabreu/clockify-cli/pkg/cmdcompl" "github.com/lucassabreu/clockify-cli/pkg/cmdutil" "github.com/spf13/cobra" + "gopkg.in/yaml.v3" ) +const formatYAML = "yaml" +const formatJSON = "json" + // NewCmdShow prints the default options for the current folder func NewCmdShow(f cmdutil.Factory) *cobra.Command { - cmd := &cobra.Command{} + var format string + cmd := &cobra.Command{ + Use: "show", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + d, err := f.TimeEntryDefaults().Read() + if err != nil { + return err + } + + return report(cmd.OutOrStdout(), format, d) + }, + } + + cmd.Flags().StringVarP(&format, "format", "f", formatYAML, "output format") + _ = cmdcompl.AddFixedSuggestionsToFlag(cmd, "format", + cmdcompl.ValidArgsSlide{formatYAML, formatJSON}) + return cmd } + +func report(out io.Writer, format string, v defaults.DefaultTimeEntry) error { + format = strings.ToLower(format) + var b []byte + switch format { + case formatJSON: + b, _ = json.Marshal(v) + case formatYAML: + b, _ = yaml.Marshal(v) + default: + return errors.New("invalid format") + } + + _, err := out.Write(b) + return err +} diff --git a/pkg/cmd/time-entry/defaults/show/show_test.go b/pkg/cmd/time-entry/defaults/show/show_test.go index f0595ab8..e17c2211 100644 --- a/pkg/cmd/time-entry/defaults/show/show_test.go +++ b/pkg/cmd/time-entry/defaults/show/show_test.go @@ -12,16 +12,13 @@ import ( ) var bFalse = false +var bTrue = true func TestNewCmdShow_ShouldPrintDefaults(t *testing.T) { - dte := defaults.DefaultTimeEntry{ - Workspace: "w", - ProjectID: "p", - Billable: &bFalse, - TagIDs: []string{"t1"}, - } - ft := func(name string, args []string, expected string) { + ft := func(name string, + dte defaults.DefaultTimeEntry, + args []string, expected string) { t.Helper() t.Run(name, func(t *testing.T) { f := mocks.NewMockFactory(t) @@ -51,14 +48,37 @@ func TestNewCmdShow_ShouldPrintDefaults(t *testing.T) { }) } - ft("as json", []string{"--json"}, - `{"workspace": "w","project":"p","billable":false,"tags":["t1"]}`) + dte := defaults.DefaultTimeEntry{ + Workspace: "w", + ProjectID: "p", + Billable: &bFalse, + TagIDs: []string{"t1"}, + } + + ft("as json", dte, []string{"--format=json"}, + `{"workspace":"w","project":"p","billable":false,"tags":["t1"]}`) - ft("as yaml", []string{"--yaml"}, heredoc.Doc(` + ft("as yaml", dte, []string{"--format=yaml"}, heredoc.Doc(` workspace: w project: p billable: false - tags: - - t1 + tags: [t1] + `)) + + dte = defaults.DefaultTimeEntry{ + Workspace: "w", + ProjectID: "p", + TaskID: "t", + Billable: &bTrue, + } + + ft("as json", dte, []string{"--format=json"}, + `{"workspace":"w","project":"p","task":"t","billable":true}`) + + ft("as yaml", dte, []string{"--format=yaml"}, heredoc.Doc(` + workspace: w + project: p + task: t + billable: true `)) }