|
| 1 | +// This file is part of arduino-cloud-cli. |
| 2 | +// |
| 3 | +// Copyright (C) 2024 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU Affero General Public License as published |
| 7 | +// by the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU Affero General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU Affero General Public License |
| 16 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +package device |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + "os" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/arduino/arduino-cli/cli/errorcodes" |
| 27 | + "github.com/arduino/arduino-cli/cli/feedback" |
| 28 | + "github.com/arduino/arduino-cli/table" |
| 29 | + "github.com/arduino/arduino-cloud-cli/command/device" |
| 30 | + "github.com/arduino/arduino-cloud-cli/config" |
| 31 | + "github.com/sirupsen/logrus" |
| 32 | + "github.com/spf13/cobra" |
| 33 | +) |
| 34 | + |
| 35 | +type showFlags struct { |
| 36 | + deviceId string |
| 37 | +} |
| 38 | + |
| 39 | +func initShowCommand() *cobra.Command { |
| 40 | + flags := &showFlags{} |
| 41 | + showCommand := &cobra.Command{ |
| 42 | + Use: "show", |
| 43 | + Short: "Show device properties", |
| 44 | + Long: "Show device properties on Arduino IoT Cloud", |
| 45 | + Run: func(cmd *cobra.Command, args []string) { |
| 46 | + if err := runShowCommand(flags); err != nil { |
| 47 | + feedback.Errorf("Error during device show: %v", err) |
| 48 | + os.Exit(errorcodes.ErrGeneric) |
| 49 | + } |
| 50 | + }, |
| 51 | + } |
| 52 | + showCommand.Flags().StringVarP(&flags.deviceId, "device-id", "d", "", "device ID") |
| 53 | + |
| 54 | + showCommand.MarkFlagRequired("device-id") |
| 55 | + |
| 56 | + return showCommand |
| 57 | +} |
| 58 | + |
| 59 | +func runShowCommand(flags *showFlags) error { |
| 60 | + logrus.Info("Show device") |
| 61 | + |
| 62 | + cred, err := config.RetrieveCredentials() |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("retrieving credentials: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + dev, _, err := device.Show(context.TODO(), flags.deviceId, cred) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + feedback.PrintResult(showResult{dev}) |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +type showResult struct { |
| 77 | + device *device.DeviceInfo |
| 78 | +} |
| 79 | + |
| 80 | +func (r showResult) Data() interface{} { |
| 81 | + return r.device |
| 82 | +} |
| 83 | + |
| 84 | +func (r showResult) String() string { |
| 85 | + if r.device == nil { |
| 86 | + return "No device found." |
| 87 | + } |
| 88 | + t := table.New() |
| 89 | + t.SetHeader("Name", "ID", "Board", "FQBN", "SerialNumber", "Status", "Connection type", "Thing", "Tags") |
| 90 | + t.AddRow( |
| 91 | + r.device.Name, |
| 92 | + r.device.ID, |
| 93 | + r.device.Board, |
| 94 | + r.device.FQBN, |
| 95 | + r.device.Serial, |
| 96 | + dereferenceString(r.device.Status), |
| 97 | + dereferenceString(r.device.ConnectionType), |
| 98 | + dereferenceString(r.device.ThingID), |
| 99 | + strings.Join(r.device.Tags, ","), |
| 100 | + ) |
| 101 | + return t.Render() |
| 102 | +} |
0 commit comments