From 1170094f4615cb03ba42f2861384abe6cf910c99 Mon Sep 17 00:00:00 2001 From: Lucas dos Santos Abreu Date: Sun, 16 Jun 2024 13:00:12 -0300 Subject: [PATCH] feat: less complexity --- pkg/uiutil/ask-project.go | 62 +++++++++++++++++++++------------------ pkg/uiutil/ask-tags.go | 35 ++-------------------- 2 files changed, 36 insertions(+), 61 deletions(-) diff --git a/pkg/uiutil/ask-project.go b/pkg/uiutil/ask-project.go index 7e8b74c6..181eaddf 100644 --- a/pkg/uiutil/ask-project.go +++ b/pkg/uiutil/ask-project.go @@ -35,53 +35,59 @@ func AskProject(p AskProjectParam) (*dto.Project, error) { p.Message = "Choose your project:" } - list := make([]string, len(p.Projects)) + c, list := projectsToList(p.ProjectID, p.Projects) + p.ProjectID = c + + if !p.ForceProjects { + list = append([]string{NoProject}, list...) + } + + id, err := p.UI.AskFromOptions(p.Message, list, p.ProjectID) + if err != nil || id == NoProject || id == "" { + return nil, err + } + + id = strings.TrimSpace(id[0:strings.Index(id, " - ")]) + for i := range p.Projects { + if p.Projects[i].ID == id { + return &p.Projects[i], nil + } + } + + return nil, errors.New(`project with id "` + id + `" not found`) +} + +func projectsToList( + projectID string, projects []dto.Project) (string, []string) { + list := make([]string, len(projects)) found := -1 nameSize := 0 - for i := range p.Projects { - list[i] = p.Projects[i].ID + " - " + p.Projects[i].Name + for i := range projects { + list[i] = projects[i].ID + " - " + projects[i].Name if c := utf8.RuneCountInString(list[i]); nameSize < c { nameSize = c } - if found == -1 && p.Projects[i].ID == p.ProjectID { + if found == -1 && projects[i].ID == projectID { found = i } } format := fmt.Sprintf("%%-%ds| %%s", nameSize+1) - for i := range p.Projects { + for i := range projects { client := "Without Client" - if p.Projects[i].ClientID != "" { - client = "Client: " + p.Projects[i].ClientName + - " (" + p.Projects[i].ClientID + ")" + if projects[i].ClientID != "" { + client = "Client: " + projects[i].ClientName + + " (" + projects[i].ClientID + ")" } list[i] = fmt.Sprintf(format, list[i], client) } if found == -1 { - p.ProjectID = "" - } else { - p.ProjectID = list[found] - } - - if !p.ForceProjects { - list = append([]string{NoProject}, list...) + return list[found], list } - id, err := p.UI.AskFromOptions(p.Message, list, p.ProjectID) - if err != nil || id == NoProject || id == "" { - return nil, err - } - - id = strings.TrimSpace(id[0:strings.Index(id, " - ")]) - for i := range p.Projects { - if p.Projects[i].ID == id { - return &p.Projects[i], nil - } - } - - return nil, errors.New(`project with id "` + id + `" not found`) + return "", list } diff --git a/pkg/uiutil/ask-tags.go b/pkg/uiutil/ask-tags.go index ac828f4d..dfda90c6 100644 --- a/pkg/uiutil/ask-tags.go +++ b/pkg/uiutil/ask-tags.go @@ -1,8 +1,6 @@ package uiutil import ( - "strings" - "github.com/lucassabreu/clockify-cli/api/dto" "github.com/lucassabreu/clockify-cli/pkg/ui" "github.com/pkg/errors" @@ -31,20 +29,7 @@ func AskTags(p AskTagsParam) ([]dto.Tag, error) { p.Message = "Choose your tags:" } - list := make([]string, len(p.Tags)) - for i := range p.Tags { - list[i] = p.Tags[i].ID + " - " + p.Tags[i].Name - } - - s := make([]string, len(p.TagIDs)) - for i := range p.TagIDs { - for _, o := range list { - if strings.HasPrefix(o, p.TagIDs[i]) { - s[i] = o - } - } - } - + s, list := toList(p.TagIDs, p.Tags) v := func(s []string) error { return nil } if p.Force { v = func(s []string) error { @@ -60,21 +45,5 @@ func AskTags(p AskTagsParam) ([]dto.Tag, error) { return []dto.Tag{}, err } - tags := make([]dto.Tag, len(ids)) - for i, t := range ids { - found := false - t = strings.TrimSpace(t[0:strings.Index(t, " - ")]) - for j := range p.Tags { - if p.Tags[j].ID == t { - tags[i] = p.Tags[j] - found = true - } - } - - if !found { - return []dto.Tag{}, errors.New(`tag with id "` + t + `" not found`) - } - } - - return tags, nil + return listToEntities("tag", ids, p.Tags) }