Пример #1
0
// CartInfo prints information about a cart
func CartInfo(c *cli.Context) {
	color.Allow(c)

	api := api.Create(c.GlobalString("locale"))

	conf := config.GetConfig()
	defer conf.Flush()

	cartName := conf.CartNameFromCache(c.Args().First())

	if cart, exists := conf.Carts[cartName]; !exists {
		fmt.Fprintf(os.Stderr, "Cart %s is unknown\n", cartName)
		os.Exit(1)
	} else {
		fmt.Printf("\nCart %s\n\n", color.Header(cart.Name))

		getResponse, getErr := api.CartGet(cart.CartID, cart.HMAC)

		if getErr != nil {
			panic(getErr)
		}

		index := 1
		cache := make(map[string]string)
		for _, item := range getResponse.Cart.CartItems.CartItemList {
			fmt.Printf("(%s) %-45.45s %9s [×%d]\n",
				color.ShortID(strconv.Itoa(index)),
				item.Title,
				item.ItemTotal.FormattedPrice,
				item.Quantity)
			cache[strconv.Itoa(index)] = item.CartItemID
			index++
		}
		conf.ResultCache["Cart"+strings.Title(cartName)+"Items"] = cache

		if len(getResponse.Cart.CartItems.CartItemList) == 0 {
			fmt.Println("Cart is empty")
		} else {
			fmt.Printf("\nSubtotal %s\n\n", color.Bold(getResponse.Cart.SubTotal.FormattedPrice))
		}
	}
}
Пример #2
0
// Checkout initiates the checkout for a cart
func Checkout(c *cli.Context) {
	api := api.Create(c.GlobalString("locale"))

	conf := config.GetConfig()
	defer conf.Flush()

	cartName := conf.CartNameFromCache(c.Args().First())

	if cart, exists := conf.Carts[cartName]; exists {
		if getResponse, getErr := api.CartGet(cart.CartID, cart.HMAC); getErr == nil {
			delete(conf.Carts, cartName)
			browser.OpenURL(getResponse.Cart.PurchaseURL)
		} else {
			panic(getErr)
		}
	} else {
		fmt.Fprintf(os.Stderr, "Cart %s is unknown\n", cartName)
		os.Exit(1)
	}
}