Skip to content

Commit

Permalink
(wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassabreu committed Jul 15, 2024
1 parent 7c2428a commit 8ebbb6d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 13 deletions.
46 changes: 45 additions & 1 deletion pkg/cmd/time-entry/defaults/show/show.go
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 28 in pkg/cmd/time-entry/defaults/show/show.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/time-entry/defaults/show/show.go#L28

Added line #L28 was not covered by tests
}

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")

Check warning on line 51 in pkg/cmd/time-entry/defaults/show/show.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/time-entry/defaults/show/show.go#L50-L51

Added lines #L50 - L51 were not covered by tests
}

_, err := out.Write(b)
return err
}
44 changes: 32 additions & 12 deletions pkg/cmd/time-entry/defaults/show/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
`))
}

0 comments on commit 8ebbb6d

Please sign in to comment.