Beispiel #1
0
func createChickens(subParser *args.ArgParser, data interface{}) int {
	subParser.AddArgument("name").Required().Help("The name of the chicken to create")
	opts, err := subParser.ParseArgs(nil)
	if err != nil {
		fmt.Println(err.Error())
		return 1
	}

	shared := data.(*SharedStruct)

	// Create the payload
	payload, err := json.Marshal(map[string]string{
		"name":     opts.String("name"),
		"metadata": shared.Metadata,
	})
	if err != nil {
		fmt.Fprintln(os.Stderr, "JSON Marshalling Error -", err)
		return 1
	}

	// Create the new Request
	req, err := http.NewRequest("POST", joinUrl(shared, "chickens"), bytes.NewBuffer(payload))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 1
	}
	resp, err := sendRequest(opts, req, &payload)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 1
	}
	fmt.Println(resp)
	return 0
}
Beispiel #2
0
func deleteChickens(subParser *args.ArgParser, data interface{}) int {
	subParser.AddArgument("name").Required().Help("The name of the chicken to delete")
	opts, err := subParser.ParseArgs(nil)
	if err != nil {
		fmt.Println(err.Error())
		return 1
	}

	shared := data.(*SharedStruct)
	req, err := http.NewRequest("DELETE", joinUrl(shared, "chickens", opts.String("name")), nil)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 1
	}
	resp, err := sendRequest(opts, req, nil)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 1
	}
	fmt.Println(resp)
	return 0
}