示例#1
0
文件: edit.go 项目: tobstarr/wlcli
func (r *edit) Run() error {
	cl, err := loadClient()
	if err != nil {
		return err
	}
	editor := os.Getenv("EDITOR")
	if editor == "" {
		return fmt.Errorf("EDITOR must be set in ENV")
	}

	listID := r.ListID
	if listID == 0 {
		listID = r.listID
	}
	tasks, err := cl.Tasks(wlclient.ListID(listID))
	if err != nil {
		return err
	}
	list, err := cl.List(listID)
	if err != nil {
		return err
	}
	tmpPath, dir, err := writeTasksToTempFile(list, tasks)
	if err != nil {
		return err
	}
	defer os.RemoveAll(dir)
	c := exec.Command(editor, tmpPath)
	c.Stdout = os.Stdout
	c.Stderr = os.Stderr
	c.Stdin = os.Stdin
	if err := c.Run(); err != nil {
		return err
	}
	f, err := os.Open(tmpPath)
	if err != nil {
		return err
	}
	actions, err := extractActions(cl, listID, f)
	f.Close()
	if err != nil {
		return err
	}
	for _, a := range actions {
		if err := a(); err != nil {
			return err
		}
	}
	return nil
}
示例#2
0
// 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
}