Exemple #1
0
func cmdListAll() (err error) {
	lists, err := api.GetLists()
	if err != nil {
		return
	}

	fmt.Println("Available lists are:\n")
	for _, el := range lists {
		fmt.Printf("    - %s\n", el.Title)
	}
	fmt.Println()

	return
}
Exemple #2
0
// Gets list object by its short name.
// Works like shell tab completion.
func getListByShortName(listName string) (list api.List, err error) {
	lists, err := api.GetLists()
	if err != nil {
		return
	}

	var sel []api.List
	listNameLower := strings.ToLower(listName)
	for _, el := range lists {
		elLower := strings.ToLower(el.Title)
		if strings.HasPrefix(elLower, listNameLower) || listName == "" {
			if elLower == listNameLower {
				sel = []api.List{el}
				break
			}

			sel = append(sel, el)
		}
	}

	if len(sel) == 0 {
		err = errors.New("List not found")
		return
	} else if len(sel) > 1 {
		if listName == "" {
			fmt.Println("Currently available lists:\n")
		} else {
			fmt.Printf("There is several lists starting with \"%s\":\n\n", listName)
		}
		for i, el := range sel {
			fmt.Printf("  [%d] %s\n", i+1, el.Title)
		}
		fmt.Println()

		fmt.Print("What is the number of the list? ")
		var listNo int
		fmt.Scanln(&listNo)
		fmt.Println()

		if listNo > len(sel) || listNo < 1 {
			err = errors.New("Incorrect input.")
			return
		}

		return sel[listNo-1], nil
	} else {
		return sel[0], nil
	}
}