diff --git a/pkg/cmd/time-entry/defaults/defaults.go b/pkg/cmd/time-entry/defaults/defaults.go index 33aa89e1..3d0929ba 100644 --- a/pkg/cmd/time-entry/defaults/defaults.go +++ b/pkg/cmd/time-entry/defaults/defaults.go @@ -1,11 +1,8 @@ package defaults import ( - "io" - "github.com/lucassabreu/clockify-cli/pkg/cmd/time-entry/defaults/set" "github.com/lucassabreu/clockify-cli/pkg/cmd/time-entry/defaults/show" - "github.com/lucassabreu/clockify-cli/pkg/cmd/time-entry/util/defaults" "github.com/lucassabreu/clockify-cli/pkg/cmdutil" outd "github.com/lucassabreu/clockify-cli/pkg/output/defaults" "github.com/spf13/cobra" @@ -23,10 +20,8 @@ func NewCmdDefaults(f cmdutil.Factory) *cobra.Command { } cmd.AddCommand( - set.NewCmdSet(f, func(of outd.OutputFlags, w io.Writer, dte defaults.DefaultTimeEntry) error { - return nil - }), - show.NewCmdShow(f), + set.NewCmdSet(f, outd.Report), + show.NewCmdShow(f, outd.Report), ) return cmd diff --git a/pkg/cmd/time-entry/defaults/set/set.go b/pkg/cmd/time-entry/defaults/set/set.go index bdc87e83..20279282 100644 --- a/pkg/cmd/time-entry/defaults/set/set.go +++ b/pkg/cmd/time-entry/defaults/set/set.go @@ -50,7 +50,8 @@ func NewCmdSet( n, changed := readFlags(d, cmd.Flags()) - if n.Workspace, err = f.GetWorkspaceID(); err != nil { + var w string + if w, err = f.GetWorkspaceID(); err != nil { return err } @@ -59,25 +60,25 @@ func NewCmdSet( return err } - if changed || d.Workspace != n.Workspace { + if changed { if n.TaskID != "" && n.ProjectID == "" { return errors.New("can't set task without project") } if f.Config().IsAllowNameForID() { if n, err = updateIDsByNames( - c, n, f.Config()); err != nil { + c, n, f.Config(), w); err != nil { return err } } else { - if err = checkIDs(c, n); err != nil { + if err = checkIDs(c, w, n); err != nil { return err } } } if f.Config().IsInteractive() { - if n, err = ask(n, f.Config(), c, f.UI()); err != nil { + if n, err = ask(n, w, f.Config(), c, f.UI()); err != nil { return err } } @@ -90,6 +91,11 @@ func NewCmdSet( }, } + cmd.Flags().StringVarP(&of.Format, + "format", "f", outd.FORMAT_YAML, "output format") + _ = cmdcompl.AddFixedSuggestionsToFlag(cmd, "format", + cmdcompl.ValidArgsSlide{outd.FORMAT_YAML, outd.FORMAT_JSON}) + cmd.Flags().BoolP("billable", "b", false, "time entry should be billable by default") cmd.Flags().BoolP("not-billable", "n", false, @@ -144,10 +150,10 @@ func readFlags( return d, changed } -func checkIDs(c api.Client, d defaults.DefaultTimeEntry) error { +func checkIDs(c api.Client, w string, d defaults.DefaultTimeEntry) error { if d.ProjectID != "" { p, err := c.GetProject(api.GetProjectParam{ - Workspace: d.Workspace, + Workspace: w, ProjectID: d.ProjectID, Hydrate: d.TaskID != "", }) @@ -176,7 +182,7 @@ func checkIDs(c api.Client, d defaults.DefaultTimeEntry) error { } tags, err := c.GetTags(api.GetTagsParam{ - Workspace: d.Workspace, + Workspace: w, Archived: &archived, PaginationParam: api.AllPages(), }) @@ -201,14 +207,15 @@ func checkIDs(c api.Client, d defaults.DefaultTimeEntry) error { var archived = false func updateIDsByNames( - c api.Client, d defaults.DefaultTimeEntry, cnf cmdutil.Config) ( + c api.Client, d defaults.DefaultTimeEntry, + cnf cmdutil.Config, w string) ( defaults.DefaultTimeEntry, error, ) { var err error if d.ProjectID != "" { d.ProjectID, err = search.GetProjectByName(c, cnf, - d.Workspace, d.ProjectID, "") + w, d.ProjectID, "") if err != nil { d.ProjectID = "" d.TaskID = "" @@ -220,7 +227,7 @@ func updateIDsByNames( if d.TaskID != "" { d.TaskID, err = search.GetTaskByName(c, api.GetTasksParam{ - Workspace: d.Workspace, + Workspace: w, ProjectID: d.ProjectID, Active: true, }, d.TaskID) @@ -231,7 +238,7 @@ func updateIDsByNames( if len(d.TagIDs) > 0 { d.TagIDs, err = search.GetTagsByName( - c, d.Workspace, !cnf.IsAllowArchivedTags(), d.TagIDs) + c, w, !cnf.IsAllowArchivedTags(), d.TagIDs) if err != nil && !cnf.IsInteractive() { return d, err } @@ -242,6 +249,7 @@ func updateIDsByNames( func ask( d defaults.DefaultTimeEntry, + w string, cnf cmdutil.Config, c api.Client, ui ui.UI, @@ -252,7 +260,7 @@ func ask( ui.SetPageSize(uint(cnf.InteractivePageSize())) ps, err := c.GetProjects(api.GetProjectsParam{ - Workspace: d.Workspace, + Workspace: w, Archived: &archived, PaginationParam: api.AllPages(), }) @@ -276,7 +284,7 @@ func ask( if d.ProjectID != "" { ts, err := c.GetTasks(api.GetTasksParam{ - Workspace: d.Workspace, + Workspace: w, ProjectID: d.ProjectID, Active: true, PaginationParam: api.AllPages(), @@ -309,7 +317,7 @@ func ask( } tags, err := c.GetTags(api.GetTagsParam{ - Workspace: d.Workspace, + Workspace: w, Archived: archived, PaginationParam: api.AllPages(), }) diff --git a/pkg/cmd/time-entry/defaults/set/set_test.go b/pkg/cmd/time-entry/defaults/set/set_test.go index 655a7ea2..41dc8be4 100644 --- a/pkg/cmd/time-entry/defaults/set/set_test.go +++ b/pkg/cmd/time-entry/defaults/set/set_test.go @@ -108,7 +108,6 @@ func TestNewCmdSet_ShouldAskInfo_WhenInteractive(t *testing.T) { return err } - assert.Equal(t, "w", d.Workspace) assert.Equal(t, "p3", d.ProjectID) assert.Equal(t, "t2", d.TaskID) assert.Equal(t, []string{"tg1", "tg2", "tg3"}, d.TagIDs) @@ -589,9 +588,9 @@ func TestNewCmdSet_ShouldUpdateDefaultsFile_OnlyByFlags(t *testing.T) { name: "no arguments, no changes", args: []string{}, current: defaults.DefaultTimeEntry{ - Workspace: "w1", ProjectID: "p1"}, + ProjectID: "p1"}, expected: defaults.DefaultTimeEntry{ - Workspace: "w1", ProjectID: "p1"}, + ProjectID: "p1"}, }, { name: "all arguments", @@ -602,7 +601,6 @@ func TestNewCmdSet_ShouldUpdateDefaultsFile_OnlyByFlags(t *testing.T) { "--billable", }, expected: defaults.DefaultTimeEntry{ - Workspace: "w2", ProjectID: "p2", TaskID: "t2", Billable: &bTrue, @@ -613,14 +611,12 @@ func TestNewCmdSet_ShouldUpdateDefaultsFile_OnlyByFlags(t *testing.T) { name: "not billable", args: []string{"--not-billable"}, current: defaults.DefaultTimeEntry{ - Workspace: "w2", ProjectID: "p2", TaskID: "t2", Billable: &bTrue, TagIDs: []string{"tg1", "tg2"}, }, expected: defaults.DefaultTimeEntry{ - Workspace: "w2", ProjectID: "p2", TaskID: "t2", Billable: &bFalse, @@ -673,7 +669,7 @@ func TestNewCmdSet_ShouldUpdateDefaultsFile_OnlyByFlags(t *testing.T) { } } - f.EXPECT().GetWorkspaceID().Return(tt.expected.Workspace, nil) + f.EXPECT().GetWorkspaceID().Return("w", nil) ted := mocks.NewMockTimeEntryDefaults(t) ted.EXPECT().Read().Return(tt.current, nil) diff --git a/pkg/cmd/time-entry/defaults/show/show.go b/pkg/cmd/time-entry/defaults/show/show.go index 4c39b95e..d4c33c71 100644 --- a/pkg/cmd/time-entry/defaults/show/show.go +++ b/pkg/cmd/time-entry/defaults/show/show.go @@ -1,24 +1,21 @@ 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" + outd "github.com/lucassabreu/clockify-cli/pkg/output/defaults" "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 { - var format string +func NewCmdShow( + f cmdutil.Factory, + report func(outd.OutputFlags, io.Writer, defaults.DefaultTimeEntry) error, +) *cobra.Command { + of := outd.OutputFlags{} cmd := &cobra.Command{ Use: "show", Args: cobra.NoArgs, @@ -28,29 +25,14 @@ func NewCmdShow(f cmdutil.Factory) *cobra.Command { return err } - return report(cmd.OutOrStdout(), format, d) + return report(of, cmd.OutOrStdout(), d) }, } - cmd.Flags().StringVarP(&format, "format", "f", formatYAML, "output format") + cmd.Flags().StringVarP(&of.Format, + "format", "f", outd.FORMAT_YAML, "output format") _ = cmdcompl.AddFixedSuggestionsToFlag(cmd, "format", - cmdcompl.ValidArgsSlide{formatYAML, formatJSON}) + cmdcompl.ValidArgsSlide{outd.FORMAT_YAML, outd.FORMAT_JSON}) 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 3a9b7cdc..0eca25e7 100644 --- a/pkg/cmd/time-entry/defaults/show/show_test.go +++ b/pkg/cmd/time-entry/defaults/show/show_test.go @@ -4,6 +4,8 @@ import ( "bytes" "testing" + outd "github.com/lucassabreu/clockify-cli/pkg/output/defaults" + "github.com/MakeNowJust/heredoc" "github.com/lucassabreu/clockify-cli/internal/mocks" "github.com/lucassabreu/clockify-cli/pkg/cmd/time-entry/defaults/show" @@ -26,7 +28,7 @@ func TestNewCmdShow_ShouldPrintDefaults(t *testing.T) { f.EXPECT().TimeEntryDefaults().Return(ted) ted.EXPECT().Read().Return(dte, nil) - cmd := show.NewCmdShow(f) + cmd := show.NewCmdShow(f, outd.Report) cmd.SilenceUsage = true cmd.SilenceErrors = true @@ -48,34 +50,30 @@ func TestNewCmdShow_ShouldPrintDefaults(t *testing.T) { } 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"]}`) + `{"project":"p","billable":false,"tags":["t1"]}`) ft("as yaml", dte, []string{"--format=yaml"}, heredoc.Doc(` - workspace: w project: p billable: false 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}`) + `{"project":"p","task":"t","billable":true}`) ft("as yaml", dte, []string{"--format=yaml"}, heredoc.Doc(` - workspace: w project: p task: t billable: true diff --git a/pkg/cmd/time-entry/in/in_test.go b/pkg/cmd/time-entry/in/in_test.go index 7291bba1..9845c92a 100644 --- a/pkg/cmd/time-entry/in/in_test.go +++ b/pkg/cmd/time-entry/in/in_test.go @@ -421,7 +421,6 @@ func TestNewCmdIn_ShouldUseDefaults(t *testing.T) { ft("only defaults", &defaults.DefaultTimeEntry{ - Workspace: w.ID, ProjectID: "p1", TaskID: "t", Billable: &bTrue, @@ -441,7 +440,6 @@ func TestNewCmdIn_ShouldUseDefaults(t *testing.T) { ft("flags over defaults", &defaults.DefaultTimeEntry{ - Workspace: w.ID, ProjectID: "p1", TaskID: "t", TagIDs: []string{"t1", "t2"}, diff --git a/pkg/cmd/time-entry/util/defaults/defaults.go b/pkg/cmd/time-entry/util/defaults/defaults.go index 9568cd65..1b40bfd0 100644 --- a/pkg/cmd/time-entry/util/defaults/defaults.go +++ b/pkg/cmd/time-entry/util/defaults/defaults.go @@ -32,7 +32,6 @@ const DEFAULT_FILENAME = ".clockify-defaults.json" // DefaultTimeEntry has the default properties for the working directory type DefaultTimeEntry struct { - Workspace string `json:"workspace,omitempty" yaml:"workspace,omitempty"` ProjectID string `json:"project,omitempty" yaml:"project,omitempty"` TaskID string `json:"task,omitempty" yaml:"task,omitempty"` Billable *bool `json:"billable,omitempty" yaml:"billable,omitempty"` diff --git a/pkg/cmd/time-entry/util/defaults/defaults_test.go b/pkg/cmd/time-entry/util/defaults/defaults_test.go index 115678c6..01d66002 100644 --- a/pkg/cmd/time-entry/util/defaults/defaults_test.go +++ b/pkg/cmd/time-entry/util/defaults/defaults_test.go @@ -27,7 +27,6 @@ func TestWriteDefaults(t *testing.T) { { filename: "j_complete.json", d: defaults.DefaultTimeEntry{ - Workspace: "w", ProjectID: "p", TaskID: "t", TagIDs: []string{"t1", "t2"}, @@ -36,7 +35,6 @@ func TestWriteDefaults(t *testing.T) { { filename: "y_complete.yaml", d: defaults.DefaultTimeEntry{ - Workspace: "w", ProjectID: "p", TaskID: "t", TagIDs: []string{"t1", "t2"}, @@ -173,7 +171,6 @@ func TestScanForDefaults_ShouldLookUpperDirs(t *testing.T) { Filename: ".clockify-defaults.yaml", }, expected: defaults.DefaultTimeEntry{ - Workspace: "w", ProjectID: "p", TaskID: "t", TagIDs: []string{"t1", "t2"}, @@ -186,7 +183,6 @@ func TestScanForDefaults_ShouldLookUpperDirs(t *testing.T) { Filename: "defaults.json", }, expected: defaults.DefaultTimeEntry{ - Workspace: "W", ProjectID: "P", TaskID: "T", }, @@ -198,7 +194,6 @@ func TestScanForDefaults_ShouldLookUpperDirs(t *testing.T) { Filename: ".clockify-defaults.yaml", }, expected: defaults.DefaultTimeEntry{ - Workspace: "w", ProjectID: "p", TaskID: "t", TagIDs: []string{"t1", "t2"}, @@ -211,7 +206,6 @@ func TestScanForDefaults_ShouldLookUpperDirs(t *testing.T) { Filename: "defaults.json", }, expected: defaults.DefaultTimeEntry{ - Workspace: "W", ProjectID: "P", TaskID: "T", }, @@ -223,7 +217,6 @@ func TestScanForDefaults_ShouldLookUpperDirs(t *testing.T) { Filename: ".clockify-defaults.yaml", }, expected: defaults.DefaultTimeEntry{ - Workspace: "w", ProjectID: "p", }, }, diff --git a/pkg/output/defaults/default.go b/pkg/output/defaults/default.go index f97bb14d..7bb8039a 100644 --- a/pkg/output/defaults/default.go +++ b/pkg/output/defaults/default.go @@ -1,7 +1,35 @@ package defaults +import ( + "encoding/json" + "errors" + "io" + + "github.com/lucassabreu/clockify-cli/pkg/cmd/time-entry/util/defaults" + "gopkg.in/yaml.v3" +) + +const ( + FORMAT_JSON = "json" + FORMAT_YAML = "yaml" +) + type OutputFlags struct { Format string - CSV bool - JSON bool +} + +// Report prints a DefaultTimeEntry using user's flags +func Report(of OutputFlags, out io.Writer, v defaults.DefaultTimeEntry) error { + var b []byte + switch of.Format { + case FORMAT_JSON: + b, _ = json.Marshal(v) + case FORMAT_YAML: + b, _ = yaml.Marshal(v) + default: + return errors.New("invalid format") + } + + _, err := out.Write(b) + return err }