Skip to content

Commit

Permalink
feat: less complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassabreu committed Jun 16, 2024
1 parent d4a943b commit 1170094
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 61 deletions.
62 changes: 34 additions & 28 deletions pkg/uiutil/ask-project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
35 changes: 2 additions & 33 deletions pkg/uiutil/ask-tags.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)

Check failure on line 32 in pkg/uiutil/ask-tags.go

View workflow job for this annotation

GitHub Actions / tests

undefined: toList

Check failure on line 32 in pkg/uiutil/ask-tags.go

View workflow job for this annotation

GitHub Actions / lint

undefined: toList

Check failure on line 32 in pkg/uiutil/ask-tags.go

View workflow job for this annotation

GitHub Actions / lint

undefined: toList

Check failure on line 32 in pkg/uiutil/ask-tags.go

View workflow job for this annotation

GitHub Actions / lint

undefined: toList

Check failure on line 32 in pkg/uiutil/ask-tags.go

View workflow job for this annotation

GitHub Actions / goreleaser

undefined: toList
v := func(s []string) error { return nil }
if p.Force {
v = func(s []string) error {
Expand All @@ -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)

Check failure on line 48 in pkg/uiutil/ask-tags.go

View workflow job for this annotation

GitHub Actions / tests

undefined: listToEntities

Check failure on line 48 in pkg/uiutil/ask-tags.go

View workflow job for this annotation

GitHub Actions / lint

undefined: listToEntities (typecheck)

Check failure on line 48 in pkg/uiutil/ask-tags.go

View workflow job for this annotation

GitHub Actions / lint

undefined: listToEntities) (typecheck)

Check failure on line 48 in pkg/uiutil/ask-tags.go

View workflow job for this annotation

GitHub Actions / lint

undefined: listToEntities) (typecheck)

Check failure on line 48 in pkg/uiutil/ask-tags.go

View workflow job for this annotation

GitHub Actions / goreleaser

undefined: listToEntities
}

0 comments on commit 1170094

Please sign in to comment.