Skip to content

Commit 8cd1844

Browse files
committed
Added main folder command
1 parent 5a10fda commit 8cd1844

File tree

6 files changed

+156
-1
lines changed

6 files changed

+156
-1
lines changed

cli/cli.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/arduino/arduino-cloud-cli/cli/credentials"
2929
"github.com/arduino/arduino-cloud-cli/cli/dashboard"
3030
"github.com/arduino/arduino-cloud-cli/cli/device"
31+
"github.com/arduino/arduino-cloud-cli/cli/folders"
3132
"github.com/arduino/arduino-cloud-cli/cli/ota"
3233
"github.com/arduino/arduino-cloud-cli/cli/template"
3334
"github.com/arduino/arduino-cloud-cli/cli/thing"
@@ -67,6 +68,7 @@ func Execute() {
6768
cli.AddCommand(dashboard.NewCommand())
6869
cli.AddCommand(ota.NewCommand())
6970
cli.AddCommand(template.NewCommand())
71+
cli.AddCommand(folders.NewCommand())
7072

7173
if err := cli.Execute(); err != nil {
7274
fmt.Fprintln(os.Stderr, err)

cli/folders/folder.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 folders
19+
20+
import (
21+
"github.com/spf13/cobra"
22+
)
23+
24+
func NewCommand() *cobra.Command {
25+
foldersCommand := &cobra.Command{
26+
Use: "folders",
27+
Short: "Custom folders.",
28+
Long: "Commands to manage folders.",
29+
}
30+
31+
foldersCommand.AddCommand(initFoldersListCommand())
32+
33+
return foldersCommand
34+
}

cli/folders/list.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 folders
19+
20+
import (
21+
"context"
22+
"fmt"
23+
"os"
24+
25+
"github.com/arduino/arduino-cli/cli/errorcodes"
26+
"github.com/arduino/arduino-cli/cli/feedback"
27+
"github.com/arduino/arduino-cloud-cli/command/folder"
28+
"github.com/arduino/arduino-cloud-cli/config"
29+
"github.com/spf13/cobra"
30+
)
31+
32+
func initFoldersListCommand() *cobra.Command {
33+
listCommand := &cobra.Command{
34+
Use: "list",
35+
Short: "List folders",
36+
Long: "List available folders",
37+
Run: func(cmd *cobra.Command, args []string) {
38+
if err := runFoldersListCommand(); err != nil {
39+
feedback.Errorf("Error during folders list: %v", err)
40+
os.Exit(errorcodes.ErrGeneric)
41+
}
42+
},
43+
}
44+
45+
return listCommand
46+
}
47+
48+
func runFoldersListCommand() error {
49+
cred, err := config.RetrieveCredentials()
50+
if err != nil {
51+
return fmt.Errorf("retrieving credentials: %w", err)
52+
}
53+
ctx := context.Background()
54+
return folder.ListFolders(ctx, cred)
55+
}

command/folder/list.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 folder
19+
20+
import (
21+
"context"
22+
23+
"github.com/arduino/arduino-cli/cli/feedback"
24+
"github.com/arduino/arduino-cloud-cli/config"
25+
"github.com/arduino/arduino-cloud-cli/internal/iot"
26+
)
27+
28+
func ListFolders(ctx context.Context, cred *config.Credentials) error {
29+
iotClient, err := iot.NewClient(cred)
30+
if err != nil {
31+
return err
32+
}
33+
34+
folders, err := iotClient.FoldersList(ctx)
35+
if err != nil {
36+
return err
37+
}
38+
39+
if folders == nil {
40+
feedback.Print("No folders found")
41+
} else {
42+
//feedback.PrintResult(folders)
43+
}
44+
45+
return nil
46+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module github.com/arduino/arduino-cloud-cli
22

33
go 1.23.0
44

5+
replace github.com/arduino/iot-client-go/v2 => /home/macolomb/CODE/clients-iot-api/generated/iot-client-go
6+
57
require (
68
github.com/arduino/arduino-cli v0.0.0-20240927141754-d9dd4ba1ed71
79
github.com/arduino/go-paths-helper v1.12.1

internal/iot/client.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,22 @@ func (cl *Client) TemplateApply(ctx context.Context, id, thingId, prefix, device
560560
return dev, nil
561561
}
562562

563+
// List available folders
564+
func (cl *Client) FoldersList(ctx context.Context) ([]iotclient.Folder, error) {
565+
ctx, err := ctxWithToken(ctx, cl.token)
566+
if err != nil {
567+
return nil, err
568+
}
569+
570+
req := cl.api.FoldersApi.ListFolders(ctx)
571+
folders, _, err := cl.api.FoldersApi.ListFoldersExecute(req)
572+
if err != nil {
573+
err = fmt.Errorf("listing folders: %w", errorDetail(err))
574+
return nil, err
575+
}
576+
return folders, nil
577+
}
578+
563579
func (cl *Client) setup(client, secret, organizationId string) error {
564580
baseURL := GetArduinoAPIBaseURL()
565581

@@ -572,7 +588,7 @@ func (cl *Client) setup(client, secret, organizationId string) error {
572588
}
573589
config.Servers = iotclient.ServerConfigurations{
574590
{
575-
URL: fmt.Sprintf("%s/iot", baseURL),
591+
URL: baseURL,
576592
Description: "IoT API endpoint",
577593
},
578594
}

0 commit comments

Comments
 (0)