Skip to content

Show tags in list commands #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion cli/device/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package device

import (
"os"
"strings"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
Expand Down Expand Up @@ -75,14 +76,15 @@ func (r listResult) String() string {
return "No devices found."
}
t := table.New()
t.SetHeader("Name", "ID", "Board", "FQBN", "SerialNumber")
t.SetHeader("Name", "ID", "Board", "FQBN", "SerialNumber", "Tags")
for _, device := range r.devices {
t.AddRow(
device.Name,
device.ID,
device.Board,
device.FQBN,
device.Serial,
strings.Join(device.Tags, ","),
)
}
return t.Render()
Expand Down
3 changes: 2 additions & 1 deletion cli/thing/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,15 @@ func (r result) String() string {
}
t := table.New()

h := []interface{}{"Name", "ID", "Device"}
h := []interface{}{"Name", "ID", "Device", "Tags"}
if listFlags.variables {
h = append(h, "Variables")
}
t.SetHeader(h...)

for _, thing := range r.things {
r := []interface{}{thing.Name, thing.ID, thing.DeviceID}
r = append(r, strings.Join(thing.Tags, ","))
if listFlags.variables {
r = append(r, strings.Join(thing.Variables, ", "))
}
Expand Down
52 changes: 52 additions & 0 deletions command/device/device.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package device

import (
"github.com/arduino/arduino-cloud-cli/command/tag"
iotclient "github.com/arduino/iot-client-go"
)

// DeviceInfo contains the most interesting
// parameters of an Arduino IoT Cloud device.
type DeviceInfo struct {
Name string `json:"name"`
ID string `json:"id"`
Board string `json:"board"`
Serial string `json:"serial-number"`
FQBN string `json:"fqbn"`
Tags []string `json:"tags,omitempty"`
}

func getDeviceInfo(device *iotclient.ArduinoDevicev2) (*DeviceInfo, error) {
// Retrieve device tags
tags, err := tag.TagsInfo(device.Tags)
if err != nil {
return nil, err
}

dev := &DeviceInfo{
Name: device.Name,
ID: device.Id,
Board: device.Type,
Serial: device.Serial,
FQBN: device.Fqbn,
Tags: tags,
}
return dev, nil
}
23 changes: 6 additions & 17 deletions command/device/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,12 @@
package device

import (
"fmt"

"github.com/arduino/arduino-cloud-cli/internal/config"
"github.com/arduino/arduino-cloud-cli/internal/iot"
)

// DeviceInfo contains the most interesting
// parameters of an Arduino IoT Cloud device.
type DeviceInfo struct {
Name string `json:"name"`
ID string `json:"id"`
Board string `json:"board"`
Serial string `json:"serial-number"`
FQBN string `json:"fqbn"`
}

// ListParams contains the optional parameters needed
// to filter the devices to be listed.
type ListParams struct {
Expand All @@ -57,14 +49,11 @@ func List(params *ListParams) ([]DeviceInfo, error) {

var devices []DeviceInfo
for _, foundDev := range foundDevices {
dev := DeviceInfo{
Name: foundDev.Name,
ID: foundDev.Id,
Board: foundDev.Type,
Serial: foundDev.Serial,
FQBN: foundDev.Fqbn,
dev, err := getDeviceInfo(&foundDev)
if err != nil {
return nil, fmt.Errorf("parsing device %s from cloud: %w", foundDev.Id, err)
}
devices = append(devices, dev)
devices = append(devices, *dev)
}

return devices, nil
Expand Down
34 changes: 34 additions & 0 deletions command/tag/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package tag

import "fmt"

// TagsInfo transforms tags into user-readable strings
// An error is returned if a tag value is not a string
func TagsInfo(tags map[string]interface{}) ([]string, error) {
var str []string
for key, value := range tags {
valStr, ok := value.(string)
if !ok {
return nil, fmt.Errorf("value of tag `%s` should be of type `string` but is of type `%T`", key, value)
}
str = append(str, key+"="+valStr)
}
return str, nil
}
6 changes: 5 additions & 1 deletion command/thing/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ func Clone(params *CloneParams) (*ThingInfo, error) {
return nil, err
}

return getThingInfo(newThing), nil
t, err := getThingInfo(newThing)
if err != nil {
return nil, fmt.Errorf("parsing thing %s from cloud: %w", newThing.Id, err)
}
return t, nil
}

func retrieve(client iot.Client, thingID string) (*iotclient.Thing, error) {
Expand Down
7 changes: 6 additions & 1 deletion command/thing/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package thing

import (
"errors"
"fmt"

"github.com/arduino/arduino-cloud-cli/internal/config"
"github.com/arduino/arduino-cloud-cli/internal/iot"
Expand Down Expand Up @@ -62,5 +63,9 @@ func Create(params *CreateParams) (*ThingInfo, error) {
return nil, err
}

return getThingInfo(newThing), nil
t, err := getThingInfo(newThing)
if err != nil {
return nil, fmt.Errorf("parsing the new thing %s from cloud: %w", newThing.Id, err)
}
return t, nil
}
7 changes: 6 additions & 1 deletion command/thing/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package thing

import (
"fmt"

"github.com/arduino/arduino-cloud-cli/internal/config"
"github.com/arduino/arduino-cloud-cli/internal/iot"
)
Expand Down Expand Up @@ -50,7 +52,10 @@ func List(params *ListParams) ([]ThingInfo, error) {

var things []ThingInfo
for _, foundThing := range foundThings {
info := getThingInfo(&foundThing)
info, err := getThingInfo(&foundThing)
if err != nil {
return nil, fmt.Errorf("parsing thing %s from cloud: %w", foundThing.Id, err)
}
things = append(things, *info)
}

Expand Down
18 changes: 15 additions & 3 deletions command/thing/thing.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

package thing

import iotclient "github.com/arduino/iot-client-go"
import (
"github.com/arduino/arduino-cloud-cli/command/tag"
iotclient "github.com/arduino/iot-client-go"
)

// ThingInfo contains the main parameters of
// an Arduino IoT Cloud thing.
Expand All @@ -26,18 +29,27 @@ type ThingInfo struct {
ID string `json:"id"`
DeviceID string `json:"device-id"`
Variables []string `json:"variables"`
Tags []string `json:"tags,omitempty"`
}

func getThingInfo(thing *iotclient.ArduinoThing) *ThingInfo {
func getThingInfo(thing *iotclient.ArduinoThing) (*ThingInfo, error) {
// Process thing variables
var vars []string
for _, p := range thing.Properties {
vars = append(vars, p.Name)
}
// Process thing tags
tags, err := tag.TagsInfo(thing.Tags)
if err != nil {
return nil, err
}

info := &ThingInfo{
Name: thing.Name,
ID: thing.Id,
DeviceID: thing.DeviceId,
Variables: vars,
Tags: tags,
}
return info
return info, nil
}