github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/command/thing/list.go (about) 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 thing 19 20 import ( 21 "context" 22 "fmt" 23 24 "github.com/arduino/arduino-cloud-cli/config" 25 "github.com/arduino/arduino-cloud-cli/internal/iot" 26 ) 27 28 // ListParams contains the optional parameters needed 29 // to filter the things to be listed. 30 type ListParams struct { 31 IDs []string // If IDs is not nil, only things belonging to that list are returned 32 DeviceID *string // If DeviceID is provided, only the thing associated to that device is listed. 33 Variables bool // If Variables is true, variable names are retrieved. 34 Tags map[string]string // If tags are provided, only things that have all these tags are listed. 35 } 36 37 // List command is used to list 38 // the things of Arduino IoT Cloud. 39 func List(ctx context.Context, params *ListParams, cred *config.Credentials) ([]ThingInfo, error) { 40 iotClient, err := iot.NewClient(cred) 41 if err != nil { 42 return nil, err 43 } 44 45 foundThings, err := iotClient.ThingList(ctx, params.IDs, params.DeviceID, params.Variables, params.Tags) 46 if err != nil { 47 return nil, err 48 } 49 50 var things []ThingInfo 51 for _, foundThing := range foundThings { 52 info, err := getThingInfo(&foundThing) 53 if err != nil { 54 return nil, fmt.Errorf("parsing thing %s from cloud: %w", foundThing.Id, err) 55 } 56 things = append(things, *info) 57 } 58 59 return things, nil 60 }