/
get.go
46 lines (37 loc) · 779 Bytes
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"bytes"
"encoding/json"
"io"
"os"
"github.com/spf13/cobra"
)
var (
formatJSON bool
)
func init() {
getCmd := &cobra.Command{
Use: "get <URL>",
Short: "Get data from Garmin Connect, print to stdout",
Run: get,
Args: cobra.ExactArgs(1),
}
getCmd.Flags().BoolVarP(&formatJSON, "json", "j", false, "Format output as indented JSON")
rootCmd.AddCommand(getCmd)
}
func get(_ *cobra.Command, args []string) {
url := args[0]
if formatJSON {
raw := bytes.NewBuffer(nil)
buffer := bytes.NewBuffer(nil)
err := client.Download(url, raw)
bail(err)
err = json.Indent(buffer, raw.Bytes(), "", " ")
bail(err)
_, err = io.Copy(os.Stdout, buffer)
bail(err)
} else {
err := client.Download(url, os.Stdout)
bail(err)
}
}