Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

reading list: Set device by flag instead of arg #217

Merged
merged 1 commit into from
Jun 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions cmd/reading/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
)

var limit int32
var device string

const readingTemplate = "Reading ID\tName\tDevice\tOrigin\tValue\tCreated\tModified\tPushed\n" +
"{{range .}}" +
Expand All @@ -40,31 +41,26 @@ const readingTemplate = "Reading ID\tName\tDevice\tOrigin\tValue\tCreated\tModif
func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "A list of all device readings",
Long: `Return all device readings.`,
Short: "A list of readings across devices or pertaining to a specified device",
Long: `Return list of readings across devices or pertaining to a specified device.`,
Args: cobra.MaximumNArgs(1),
RunE: listHandler,
RunE: listHandler,
}
cmd.Flags().Int32VarP(&limit, "limit", "l", 0, "Limit number of results")
cmd.Flags().StringVarP(&device, "device", "d", "", "Readings generated by specific device with given name.")
cmd.Flags().Int32VarP(&limit, "limit", "l", 50, "Limit number of results")
return cmd
}

func listHandler(cmd *cobra.Command, args []string) (err error) {
var url string
if len(args) > 0 {
var limitUrl string
device := args[0]
if limit > 0 {
limitUrl = strconv.FormatInt(int64(limit), 10)
} else {
limitUrl = strconv.FormatInt(int64(50), 10)
}
if device != "" {
limitUrl := strconv.FormatInt(int64(limit), 10)
url = config.Conf.Clients["CoreData"].Url() + clients.ApiReadingRoute + "/device/" + device + "/" + limitUrl
} else {
url = config.Conf.Clients["CoreData"].Url() + clients.ApiReadingRoute
}
var readings []models.Reading

var readings []models.Reading
err = request.Get(url, &readings)
if err != nil {
return
Expand Down