Skip to content

Commit

Permalink
wip: time entry defaults commads
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassabreu committed Apr 6, 2024
1 parent 719f9cd commit 7850a28
Show file tree
Hide file tree
Showing 6 changed files with 1,079 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pkg/cmd/time-entry/defaults/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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"
)

// NewCmdDefaults creates commands to manage default parameters when creating
// time entries
func NewCmdDefaults(f cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "defaults",
Aliases: []string{"def"},
Short: "Manages the default parameters for time entries " +
"in the current folder",
Args: cobra.ExactArgs(0),
}

cmd.AddCommand(
set.NewCmdSet(f, func(of outd.OutputFlags, w io.Writer, dte defaults.DefaultTimeEntry) error {
return nil
}),
show.NewCmdShow(f),
)

return cmd
}
347 changes: 347 additions & 0 deletions pkg/cmd/time-entry/defaults/set/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,347 @@
package set

import (
"io"

"github.com/lucassabreu/clockify-cli/api"
"github.com/lucassabreu/clockify-cli/pkg/cmd/time-entry/util/defaults"
"github.com/lucassabreu/clockify-cli/pkg/cmdcompl"
"github.com/lucassabreu/clockify-cli/pkg/cmdcomplutil"
"github.com/lucassabreu/clockify-cli/pkg/cmdutil"
outd "github.com/lucassabreu/clockify-cli/pkg/output/defaults"
"github.com/lucassabreu/clockify-cli/pkg/search"
"github.com/lucassabreu/clockify-cli/pkg/ui"
"github.com/lucassabreu/clockify-cli/pkg/uiutil"
"github.com/lucassabreu/clockify-cli/strhlp"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

// NewCmdSet sets the default parameters for time entries in the current folder
func NewCmdSet(
f cmdutil.Factory,
report func(outd.OutputFlags, io.Writer, defaults.DefaultTimeEntry) error,
) *cobra.Command {
if report == nil {
panic(errors.New("report parameter should not be nil"))

Check warning on line 27 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#L27

Added line #L27 was not covered by tests
}

short := "Sets the default parameters for the current folder"
of := outd.OutputFlags{}
cmd := &cobra.Command{
Use: "set",
Short: short,
Long: short + "\n" +
"The parameters will be saved in the current working directory " +
"in the file " + defaults.DEFAULT_FILENAME + ".yaml",
Example: "",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
if err := cmdutil.XorFlagSet(
cmd.Flags(), "billable", "not-billable"); err != nil {
return err
}

d, err := f.TimeEntryDefaults().Read()
if err != nil && err != defaults.DefaultsFileNotFoundErr {
return err
}

n, changed := readFlags(d, cmd.Flags())

if n.Workspace, err = f.GetWorkspaceID(); err != nil {
return err
}

if changed || d.Workspace != n.Workspace {
if n.TaskID != "" && n.ProjectID == "" {
return errors.New("can't set task without project")
}

c, err := f.Client()
if err != nil {
return err
}

if f.Config().IsAllowNameForID() {
if n, err = updateIDsByNames(
c, n, f.Config()); err != nil {
return err
}
}

if f.Config().IsInteractive() {
if n, err = ask(n, f.Config(), c, f.UI()); err != nil {
return err

Check warning on line 76 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#L76

Added line #L76 was not covered by tests
}
}

if !f.Config().IsAllowNameForID() {
if err = checkIDs(c, n); err != nil {
return err
}
}
}

if err = f.TimeEntryDefaults().Write(n); err != nil {
return err

Check warning on line 88 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#L88

Added line #L88 was not covered by tests
}

return report(of, cmd.OutOrStdout(), n)
},
}

cmd.Flags().BoolP("billable", "b", false,
"time entry should be billable by default")
cmd.Flags().BoolP("not-billable", "n", false,
"time entry should not be billable by default")
cmd.Flags().String("task", "", "default task")
_ = cmdcompl.AddSuggestionsToFlag(cmd, "task",
cmdcomplutil.NewTaskAutoComplete(f, true))

cmd.Flags().StringSliceP("tag", "T", []string{},
"add tags be used by default")
_ = cmdcompl.AddSuggestionsToFlag(cmd, "tag",
cmdcomplutil.NewTagAutoComplete(f))

cmd.Flags().StringP("project", "p", "", "project to used by default")
_ = cmdcompl.AddSuggestionsToFlag(cmd, "project",
cmdcomplutil.NewProjectAutoComplete(f))

return cmd
}

func readFlags(
d defaults.DefaultTimeEntry,
f *pflag.FlagSet,
) (defaults.DefaultTimeEntry, bool) {
changed := false
if f.Changed("project") {
d.ProjectID, _ = f.GetString("project")
changed = true
}

if f.Changed("task") {
d.TaskID, _ = f.GetString("task")
changed = true
}

if f.Changed("tag") {
d.TagIDs, _ = f.GetStringSlice("tag")
d.TagIDs = strhlp.Unique(d.TagIDs)
changed = true
}

if f.Changed("billable") {
b := true
d.Billable = &b
changed = true
} else if f.Changed("not-billable") {
b := false
d.Billable = &b
changed = true
}

return d, changed
}

func checkIDs(c api.Client, d defaults.DefaultTimeEntry) error {
if d.ProjectID != "" {
p, err := c.GetProject(api.GetProjectParam{
Workspace: d.Workspace,
ProjectID: d.ProjectID,
Hydrate: d.TaskID != "",
})

if err != nil {
return err
}

if d.TaskID != "" {
found := false
for i := range p.Tasks {
if p.Tasks[i].ID == d.TaskID {
found = true
break
}
}

if !found {
return errors.New(
"can't find task with ID \"" + d.TaskID +
"\" on project \"" + d.ProjectID + "\"")
}
}
} else if d.TaskID != "" {
return errors.New("task can't be set without a project")

Check warning on line 177 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#L177

Added line #L177 was not covered by tests
}

tags, err := c.GetTags(api.GetTagsParam{
Workspace: d.Workspace,
Archived: &archived,
PaginationParam: api.AllPages(),
})
if err != nil {
return err
}

ids := make([]string, len(tags))
for i := range tags {
ids[i] = tags[i].ID
}

for _, id := range d.TagIDs {
if !strhlp.InSlice(id, ids) {
return errors.Errorf("can't find tag with ID \"%s\"", id)
}
}

return nil
}

var archived = false

func updateIDsByNames(
c api.Client, d defaults.DefaultTimeEntry, cnf cmdutil.Config) (
defaults.DefaultTimeEntry,
error,
) {
var err error
if d.ProjectID != "" {
d.ProjectID, err = search.GetProjectByName(c, cnf,
d.Workspace, d.ProjectID, "")
if err != nil {
d.ProjectID = ""
d.TaskID = ""
if !cnf.IsInteractive() {
return d, err
}
}
}

if d.TaskID != "" {
d.TaskID, err = search.GetTaskByName(c, api.GetTasksParam{
Workspace: d.Workspace,
ProjectID: d.ProjectID,
Active: true,
}, d.TaskID)
if err != nil && !cnf.IsInteractive() {
return d, err
}
}

if len(d.TagIDs) > 0 {
d.TagIDs, err = search.GetTagsByName(
c, d.Workspace, !cnf.IsAllowArchivedTags(), d.TagIDs)
if err != nil && !cnf.IsInteractive() {
return d, err
}
}

return d, nil
}

func ask(
d defaults.DefaultTimeEntry,
cnf cmdutil.Config,
c api.Client,
ui ui.UI,
) (
defaults.DefaultTimeEntry,
error,
) {
ui.SetPageSize(uint(cnf.InteractivePageSize()))

ps, err := c.GetProjects(api.GetProjectsParam{
Workspace: d.Workspace,
Archived: &archived,
PaginationParam: api.AllPages(),
})
if err != nil {
return d, err

Check warning on line 262 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#L262

Added line #L262 was not covered by tests
}

p, err := uiutil.AskProject(uiutil.AskProjectParam{
UI: ui,
ProjectID: d.ProjectID,
Projects: ps,
})
if err != nil {
return d, err

Check warning on line 271 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#L271

Added line #L271 was not covered by tests
}
if p != nil {
d.ProjectID = p.ID
} else {
d.ProjectID = ""

Check warning on line 276 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#L276

Added line #L276 was not covered by tests
}

if d.ProjectID != "" {
ts, err := c.GetTasks(api.GetTasksParam{
Workspace: d.Workspace,
ProjectID: d.ProjectID,
Active: true,
PaginationParam: api.AllPages(),
})
if err != nil {
return d, err

Check warning on line 287 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#L287

Added line #L287 was not covered by tests
}

t, err := uiutil.AskTask(uiutil.AskTaskParam{
UI: ui,
TaskID: d.TaskID,
Tasks: ts,
})
if err != nil {
return d, err

Check warning on line 296 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#L296

Added line #L296 was not covered by tests
}
if t != nil {
d.TaskID = t.ID
} else {
d.TaskID = ""

Check warning on line 301 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#L301

Added line #L301 was not covered by tests
}
} else {
d.TaskID = ""

Check warning on line 304 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#L303-L304

Added lines #L303 - L304 were not covered by tests
}

var archived *bool
if !cnf.IsAllowArchivedTags() {
b := false
archived = &b
}

tags, err := c.GetTags(api.GetTagsParam{
Workspace: d.Workspace,
Archived: archived,
PaginationParam: api.AllPages(),
})
if err != nil {
return d, err

Check warning on line 319 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#L319

Added line #L319 was not covered by tests
}

tags, err = uiutil.AskTags(uiutil.AskTagsParam{
UI: ui,
TagIDs: d.TagIDs,
Tags: tags,
})
if err != nil {
return d, err

Check warning on line 328 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#L328

Added line #L328 was not covered by tests
}
d.TagIDs = make([]string, len(tags))
for i := range tags {
d.TagIDs[i] = tags[i].ID
}

b := false
if d.Billable != nil {
b = *d.Billable
}

b, err = ui.Confirm("Should be billable?", b)
if err != nil {
return d, err

Check warning on line 342 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#L342

Added line #L342 was not covered by tests
}
d.Billable = &b

return d, err
}
Loading

0 comments on commit 7850a28

Please sign in to comment.