func (r *initAction) Run() error { path := configFileName if _, err := os.Stat(path); err == nil { return fmt.Errorf("config file %s already exists", path) } cl, err := loadClient() if err != nil { return err } lists, err := cl.Lists() if err != nil { return err } t := gocli.NewTable() for _, l := range lists { t.Add(l.Title) } t.SortBy = 0 sort.Sort(t) list := lists[t.Select("select a list")] l.Printf("initializing list %d %s", list.ID, list.Title) c := &Config{ListID: list.ID} f, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0644) if err != nil { return err } defer f.Close() return toml.NewEncoder(f).Encode(c) }
func (r *listLists) Run() error { c, err := loadClient() if err != nil { return err } lists, err := c.Lists() if err != nil { return err } t := gocli.NewTable() for _, list := range lists { t.Add(list.ID, list.Title) } fmt.Println(t) return nil }
// this should be a generic inbox action func (r *listInboxAction) Run() error { cl, err := loadClient() if err != nil { return err } listID := r.listID if listID == 0 { ib, err := cl.Inbox() if err != nil { return err } listID = ib.ID } if dataOnStdin() { b, err := ioutil.ReadAll(os.Stdin) if err != nil { return err } task, err := cl.CreateTask(&wlclient.Task{ListID: listID, Title: strings.TrimSpace(string(b))}) if err != nil { return err } return json.NewEncoder(os.Stdout).Encode(task) return nil } tasks, err := cl.Tasks(wlclient.ListID(listID)) if err != nil { return err } pos, err := cl.TaskPositions(listID) if err != nil { return err } if len(pos) == 0 { return fmt.Errorf("unable to get positions for list %d", listID) } if len(tasks) == 0 { fmt.Printf("no tasks found. use `wlcli push` to create one\n") return nil } sort.Sort(tasks) t := gocli.NewTable() tm := map[int]*wlclient.Task{} for _, t := range tasks { tm[t.ID] = t } positions := pos[0].Values for i, p := range positions { task, ok := tm[p] if !ok { continue } delete(tm, p) if r.Tag != "" && !matchesTag(task.Title, r.Tag) { continue } title := task.Title if !r.Full { title = truncate(title, 64) } if i < r.Limit && !r.All { t.Add(task.ID, title) } } for _, task := range tm { if r.Tag != "" && !matchesTag(task.Title, r.Tag) { continue } t.Add(task.ID, truncate(task.Title, 64)) } fmt.Println(t) return nil }