Skip to content

Commit

Permalink
(wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassabreu committed Jul 23, 2024
1 parent 6cdaa86 commit 6083006
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 76 deletions.
9 changes: 2 additions & 7 deletions pkg/cmd/time-entry/defaults/defaults.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down
38 changes: 23 additions & 15 deletions pkg/cmd/time-entry/defaults/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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

Check warning on line 82 in pkg/cmd/time-entry/defaults/set/set.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/time-entry/defaults/set/set.go#L82

Added line #L82 was not covered by tests
}
}
Expand All @@ -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,
Expand Down Expand Up @@ -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 != "",
})
Expand Down Expand Up @@ -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(),
})
Expand All @@ -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 = ""
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -242,6 +249,7 @@ func updateIDsByNames(

func ask(
d defaults.DefaultTimeEntry,
w string,
cnf cmdutil.Config,
c api.Client,
ui ui.UI,
Expand All @@ -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(),
})
Expand All @@ -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(),
Expand Down Expand Up @@ -309,7 +317,7 @@ func ask(
}

tags, err := c.GetTags(api.GetTagsParam{
Workspace: d.Workspace,
Workspace: w,
Archived: archived,
PaginationParam: api.AllPages(),
})
Expand Down
10 changes: 3 additions & 7 deletions pkg/cmd/time-entry/defaults/set/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand All @@ -602,7 +601,6 @@ func TestNewCmdSet_ShouldUpdateDefaultsFile_OnlyByFlags(t *testing.T) {
"--billable",
},
expected: defaults.DefaultTimeEntry{
Workspace: "w2",
ProjectID: "p2",
TaskID: "t2",
Billable: &bTrue,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
38 changes: 10 additions & 28 deletions pkg/cmd/time-entry/defaults/show/show.go
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -28,29 +25,14 @@ func NewCmdShow(f cmdutil.Factory) *cobra.Command {
return err

Check warning on line 25 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#L25

Added line #L25 was not covered by tests
}

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
}
12 changes: 5 additions & 7 deletions pkg/cmd/time-entry/defaults/show/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions pkg/cmd/time-entry/in/in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ func TestNewCmdIn_ShouldUseDefaults(t *testing.T) {

ft("only defaults",
&defaults.DefaultTimeEntry{
Workspace: w.ID,
ProjectID: "p1",
TaskID: "t",
Billable: &bTrue,
Expand All @@ -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"},
Expand Down
1 change: 0 additions & 1 deletion pkg/cmd/time-entry/util/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
Loading

0 comments on commit 6083006

Please sign in to comment.