Skip to content

Commit

Permalink
support label option at item add
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Sep 12, 2018
1 parent 8e7f38a commit feb6717
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
15 changes: 14 additions & 1 deletion cmd/todoist/cmd/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,22 @@ var itemAddCmd = &cobra.Command{

projectName, err := cmd.Flags().GetString("project")
if err != nil {
return errors.New("invalid project id")
return errors.New("invalid project name")
}
if project := client.Project.FindOneByName(projectName); project != nil {
item.ProjectID = project.ID
}

labelNames, err := cmd.Flags().GetString("label")
if err != nil {
return errors.New("invalid label name")
}
for _, labelName := range strings.Split(labelNames, ",") {
if label := client.Label.FindOneByName(labelName); label != nil {
item.Labels = append(item.Labels, label.ID)
}
}

if _, err = client.Item.Add(item); err != nil {
return err
}
Expand Down Expand Up @@ -187,6 +197,9 @@ func init() {
RootCmd.AddCommand(itemCmd)
itemCmd.AddCommand(itemListCmd)
itemAddCmd.Flags().StringP("project", "p", "inbox", "project name")
itemAddCmd.Flags().StringP("label", "l", "", "label name(s) (delimiter: ,)")
// itemAddCmd.Flags().StringP("due", "d", "", "due date")
// itemAddCmd.Flags().String("priority", "", "priority")
itemCmd.AddCommand(itemAddCmd)
itemCmd.AddCommand(itemDeleteCmd)
itemMoveCmd.Flags().StringP("project", "p", "", "project")
Expand Down
16 changes: 16 additions & 0 deletions todoist/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ func (c *LabelClient) Resolve(id ID) *Label {
}

func (c LabelClient) FindByName(substr string) []Label {
if r := []rune(substr); len(r) > 0 && string(r[0]) == "@" {
substr = string(r[1:])
}
var res []Label
for _, l := range c.GetAll() {
if strings.Contains(l.Name, substr) {
Expand All @@ -129,6 +132,19 @@ func (c LabelClient) FindByName(substr string) []Label {
return res
}

func (c LabelClient) FindOneByName(substr string) *Label {
labels := c.FindByName(substr)
for _, label := range labels {
if label.Name == substr {
return &label
}
}
if len(labels) > 0 {
return &labels[0]
}
return nil
}

type labelCache struct {
cache *[]Label
}
Expand Down

0 comments on commit feb6717

Please sign in to comment.