Example #1
0
File: lst.go Project: tgrijalva/lst
func blast(l *list.List, args []string) {
	switch len(args) {
	case 0:
		fmt.Println("What item do you want to blast?")
		os.Exit(0)
	case 1:
		// Check if first arg is int
		n, err := strconv.Atoi(args[0])
		if err != nil {
			fmt.Printf("%q is not an item number.\n", args[0])
			os.Exit(1)
		}

		// Make suer (n) > 1
		if n < 1 {
			fmt.Printf("%q is not an item number.\n", args[0])
			os.Exit(1)
		} else if n == l.Len() {
			fmt.Println("Can't blast last item.")
			os.Exit(1)
		} else if n > l.Len() {
			fmt.Printf("item [%d] is out of bounds.\n", n)
			os.Exit(1)
		}

		// Bump item (n)
		// Grab item n
		itemNumber, element := 1, l.Front()
		for itemNumber < n {
			element = element.Next()
			itemNumber++
		}

		// Update date
		element.Value.(ListItem)[DATE] = time.Now().Format(DATE_FORMAT)

		// Move it to the front
		l.MoveToBack(element)
		fmt.Printf("Blasting item [%d]\n", n)

	default:
		fmt.Println("Too many arguments.")
		os.Exit(1)
	}
}