mirror of
https://github.com/sstent/go-garth-cli.git
synced 2026-01-25 16:42:48 +00:00
47 lines
779 B
Go
47 lines
779 B
Go
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)
|
|
}
|
|
}
|