|
| 1 | +// This file is part of arduino-cloud-cli. |
| 2 | +// |
| 3 | +// Copyright (C) 2021 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 ota |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/arduino/arduino-cli/cli/errorcodes" |
| 25 | + "github.com/arduino/arduino-cli/cli/feedback" |
| 26 | + "github.com/arduino/arduino-cloud-cli/command/ota" |
| 27 | + "github.com/arduino/arduino-cloud-cli/config" |
| 28 | + "github.com/spf13/cobra" |
| 29 | +) |
| 30 | + |
| 31 | +type statusFlags struct { |
| 32 | + otaID string |
| 33 | + otaIDs string |
| 34 | + deviceId string |
| 35 | + limit int16 |
| 36 | + sort string |
| 37 | +} |
| 38 | + |
| 39 | +func initOtaStatusCommand() *cobra.Command { |
| 40 | + flags := &statusFlags{} |
| 41 | + uploadCommand := &cobra.Command{ |
| 42 | + Use: "status", |
| 43 | + Short: "OTA status", |
| 44 | + Long: "Get OTA status by OTA or device ID", |
| 45 | + Run: func(cmd *cobra.Command, args []string) { |
| 46 | + if err := runOtaStatusCommand(flags); err != nil { |
| 47 | + feedback.Errorf("Error during ota get status: %v", err) |
| 48 | + os.Exit(errorcodes.ErrGeneric) |
| 49 | + } |
| 50 | + }, |
| 51 | + } |
| 52 | + uploadCommand.Flags().StringVarP(&flags.otaID, "ota-id", "o", "", "OTA ID") |
| 53 | + uploadCommand.Flags().StringVarP(&flags.otaIDs, "ota-ids", "", "", "OTA IDs (comma separated)") |
| 54 | + uploadCommand.Flags().StringVarP(&flags.deviceId, "device-id", "d", "", "Device ID") |
| 55 | + uploadCommand.Flags().Int16VarP(&flags.limit, "limit", "l", 10, "Output limit (default: 10)") |
| 56 | + uploadCommand.Flags().StringVarP(&flags.sort, "sort", "s", "desc", "Sorting (default: desc)") |
| 57 | + |
| 58 | + return uploadCommand |
| 59 | +} |
| 60 | + |
| 61 | +func runOtaStatusCommand(flags *statusFlags) error { |
| 62 | + if flags.otaID == "" && flags.deviceId == "" && flags.otaIDs == "" { |
| 63 | + return fmt.Errorf("required flag(s) \"ota-id\" or \"device-id\" or \"ota-ids\" not set") |
| 64 | + } |
| 65 | + |
| 66 | + cred, err := config.RetrieveCredentials() |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("retrieving credentials: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + return ota.PrintOtaStatus(flags.otaID, flags.otaIDs, flags.deviceId, cred, int(flags.limit), flags.sort) |
| 72 | +} |
0 commit comments