Skip to content

Commit afc95a8

Browse files
committed
Fixed export command
1 parent f07e865 commit afc95a8

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

command/template/export.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ package template
2020
import (
2121
"fmt"
2222

23-
"github.com/sirupsen/logrus"
24-
23+
"github.com/arduino/arduino-cli/cli/feedback"
2524
"github.com/arduino/arduino-cloud-cli/config"
2625
storageapi "github.com/arduino/arduino-cloud-cli/internal/storage-api"
2726
)
@@ -30,6 +29,8 @@ func ExportCustomTemplate(cred *config.Credentials, templateId string) error {
3029

3130
apiclient := storageapi.NewClient(cred)
3231

32+
feedback.Printf("Exporting template %s", templateId)
33+
3334
filecreaed, err := apiclient.ExportCustomTemplate(templateId)
3435
if err != nil {
3536
return err
@@ -39,7 +40,7 @@ func ExportCustomTemplate(cred *config.Credentials, templateId string) error {
3940
if filecreaed != nil {
4041
outf = *filecreaed
4142
}
42-
logrus.Infof(fmt.Sprintf("Template %s exported to file: %s", templateId, outf))
43+
feedback.Printf(fmt.Sprintf("Template %s exported to file: %s", templateId, outf))
4344

4445
return nil
4546
}

internal/storage-api/client.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ import (
2626
"mime/multipart"
2727
"net/http"
2828
"os"
29+
"strconv"
2930
"strings"
3031

3132
"github.com/arduino/arduino-cloud-cli/config"
3233
"github.com/arduino/arduino-cloud-cli/internal/iot"
34+
"github.com/sirupsen/logrus"
3335
"golang.org/x/oauth2"
3436
)
3537

@@ -190,13 +192,18 @@ func (c *StorageApiClient) ExportCustomTemplate(templateId string) (*string, err
190192
}
191193
defer res.Body.Close()
192194

193-
if res.StatusCode == 200 {
195+
logrus.Debugf("Export API call status: %d", res.StatusCode)
196+
197+
if res.StatusCode == 200 || res.StatusCode == 201 {
194198
outfile, fileExportPath, err := createNewLocalFile(templateId, res)
195199
if err != nil {
196200
return nil, err
197201
}
198202
defer outfile.Close()
199-
io.Copy(outfile, res.Body)
203+
_, err = io.Copy(outfile, res.Body)
204+
if err != nil {
205+
return nil, err
206+
}
200207
return &fileExportPath, nil
201208
} else if res.StatusCode == 400 {
202209
bodyb, _ := io.ReadAll(res.Body)
@@ -233,19 +240,20 @@ func composeNewLocalFileName(templateId string, res *http.Response) (string, err
233240

234241
i := 1
235242
for ; i < 51; i++ {
236-
_, err := os.Stat(fileExportPath)
243+
fileE, err := os.Stat(fileExportPath)
237244
if err != nil {
238245
if os.IsNotExist(err) {
239246
break
240-
} else {
241-
newbase := strings.TrimSuffix(originalFileExportName, TemplateFileExtension)
242-
newbase = newbase + "_" + string(i) + TemplateFileExtension
243-
fileExportPath = newbase
244247
}
245248
}
249+
if fileE != nil {
250+
newbase := strings.TrimSuffix(originalFileExportName, TemplateFileExtension)
251+
newbase = newbase + "_" + strconv.Itoa(i) + TemplateFileExtension
252+
fileExportPath = newbase
253+
}
246254
}
247255
if i >= 50 {
248-
return "", errors.New("cannot create a new file name. Max number of copy reached.")
256+
return "", errors.New("cannot create a new file name. Max number of copy reached")
249257
}
250258

251259
return fileExportPath, nil

0 commit comments

Comments
 (0)