Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 918977f

Browse files
committedJun 3, 2024·
Added internal command implementation
1 parent 3455456 commit 918977f

File tree

8 files changed

+207
-6
lines changed

8 files changed

+207
-6
lines changed
 

‎cli/template/export.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 template
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/template"
27+
"github.com/arduino/arduino-cloud-cli/config"
28+
"github.com/spf13/cobra"
29+
)
30+
31+
type exportFlags struct {
32+
templateId string
33+
}
34+
35+
func initTemplateExportCommand() *cobra.Command {
36+
flags := &exportFlags{}
37+
uploadCommand := &cobra.Command{
38+
Use: "export",
39+
Short: "Export template",
40+
Long: "Export template to a file",
41+
Run: func(cmd *cobra.Command, args []string) {
42+
if err := runTemplateExportCommand(flags); err != nil {
43+
feedback.Errorf("Error during template export status: %v", err)
44+
os.Exit(errorcodes.ErrGeneric)
45+
}
46+
},
47+
}
48+
49+
uploadCommand.Flags().StringVarP(&flags.templateId, "id", "t", "", "Template file id")
50+
51+
return uploadCommand
52+
}
53+
54+
func runTemplateExportCommand(flags *exportFlags) error {
55+
cred, err := config.RetrieveCredentials()
56+
if err != nil {
57+
return fmt.Errorf("retrieving credentials: %w", err)
58+
}
59+
return template.ExportCustomTemplate(cred, flags.templateId)
60+
}

‎cli/template/import.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
package template
1919

2020
import (
21+
"fmt"
2122
"os"
2223

2324
"github.com/arduino/arduino-cli/cli/errorcodes"
2425
"github.com/arduino/arduino-cli/cli/feedback"
26+
"github.com/arduino/arduino-cloud-cli/command/template"
27+
"github.com/arduino/arduino-cloud-cli/config"
2528
"github.com/spf13/cobra"
2629
)
2730

@@ -37,7 +40,7 @@ func initTemplateImportCommand() *cobra.Command {
3740
Long: "Import a template from a file",
3841
Run: func(cmd *cobra.Command, args []string) {
3942
if err := runTemplateImportCommand(flags); err != nil {
40-
feedback.Errorf("Error during ota get status: %v", err)
43+
feedback.Errorf("Error during template status: %v", err)
4144
os.Exit(errorcodes.ErrGeneric)
4245
}
4346
},
@@ -49,5 +52,9 @@ func initTemplateImportCommand() *cobra.Command {
4952
}
5053

5154
func runTemplateImportCommand(flags *importFlags) error {
52-
return nil
55+
cred, err := config.RetrieveCredentials()
56+
if err != nil {
57+
return fmt.Errorf("retrieving credentials: %w", err)
58+
}
59+
return template.ImportCustomTemplate(cred, flags.templateFile)
5360
}

‎command/ota/cancel.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
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+
118
package ota
219

320
import (

‎command/ota/massupload_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
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+
118
package ota
219

320
import (

‎command/ota/status.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
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+
118
package ota
219

320
import (

‎command/template/export.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 template
19+
20+
import (
21+
"fmt"
22+
23+
"github.com/sirupsen/logrus"
24+
25+
"github.com/arduino/arduino-cloud-cli/config"
26+
storageapi "github.com/arduino/arduino-cloud-cli/internal/storage-api"
27+
)
28+
29+
func ExportCustomTemplate(cred *config.Credentials, templateId string) error {
30+
31+
apiclient := storageapi.NewClient(cred)
32+
33+
filecreaed, err := apiclient.ExportCustomTemplate(templateId)
34+
if err != nil {
35+
return err
36+
}
37+
38+
outf := ""
39+
if filecreaed != nil {
40+
outf = *filecreaed
41+
}
42+
logrus.Infof(fmt.Sprintf("Template %s exported to file: %s", templateId, outf))
43+
44+
return nil
45+
}

‎command/template/import.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 template
19+
20+
import (
21+
"github.com/sirupsen/logrus"
22+
23+
"github.com/arduino/arduino-cloud-cli/config"
24+
storageapi "github.com/arduino/arduino-cloud-cli/internal/storage-api"
25+
)
26+
27+
func ImportCustomTemplate(cred *config.Credentials, filePath string) error {
28+
29+
apiclient := storageapi.NewClient(cred)
30+
31+
_, err := apiclient.ImportCustomTemplate(filePath)
32+
if err != nil {
33+
return err
34+
}
35+
36+
logrus.Infof("Template imported")
37+
38+
return nil
39+
}

‎internal/storage-api/client.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ func (c *StorageApiClient) ExportCustomTemplate(templateId string) (*string, err
180180
return nil, err
181181
}
182182
defer res.Body.Close()
183-
184183

185184
if res.StatusCode == 200 {
186185
outfile, fileExportPath, err := createNewLocalFile(templateId, res)
@@ -222,9 +221,9 @@ func composeNewLocalFileName(templateId string, res *http.Response) (string, err
222221
if fileExportPath == "" {
223222
fileExportPath = templateId + TemplateFileExtension
224223
}
225-
224+
226225
i := 1
227-
for ; i < 51 ; i++ {
226+
for ; i < 51; i++ {
228227
_, err := os.Stat(fileExportPath)
229228
if err != nil {
230229
if os.IsNotExist(err) {
@@ -236,7 +235,7 @@ func composeNewLocalFileName(templateId string, res *http.Response) (string, err
236235
}
237236
}
238237
}
239-
if i >=50 {
238+
if i >= 50 {
240239
return "", errors.New("cannot create a new file name. Max number of copy reached.")
241240
}
242241

0 commit comments

Comments
 (0)
Please sign in to comment.