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 9fcda19

Browse files
committedJun 4, 2024·
Added export output directory configuration
1 parent 1715c17 commit 9fcda19

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed
 

‎cli/template/export.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
type exportFlags struct {
3232
templateId string
33+
path string
3334
}
3435

3536
func initTemplateExportCommand() *cobra.Command {
@@ -46,7 +47,8 @@ func initTemplateExportCommand() *cobra.Command {
4647
},
4748
}
4849

49-
uploadCommand.Flags().StringVarP(&flags.templateId, "template-id", "t", "", "Template file id")
50+
uploadCommand.Flags().StringVarP(&flags.templateId, "template-id", "t", "", "Template id")
51+
uploadCommand.Flags().StringVarP(&flags.path, "directory", "d", "", "Output directory")
5052

5153
uploadCommand.MarkFlagRequired("template-id")
5254

@@ -58,5 +60,5 @@ func runTemplateExportCommand(flags *exportFlags) error {
5860
if err != nil {
5961
return fmt.Errorf("retrieving credentials: %w", err)
6062
}
61-
return template.ExportCustomTemplate(cred, flags.templateId)
63+
return template.ExportCustomTemplate(cred, flags.templateId, flags.path)
6264
}

‎command/template/export.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ import (
2525
storageapi "github.com/arduino/arduino-cloud-cli/internal/storage-api"
2626
)
2727

28-
func ExportCustomTemplate(cred *config.Credentials, templateId string) error {
28+
func ExportCustomTemplate(cred *config.Credentials, templateId, path string) error {
2929

3030
apiclient := storageapi.NewClient(cred)
3131

3232
feedback.Printf("Exporting template %s", templateId)
3333

34-
filecreaed, err := apiclient.ExportCustomTemplate(templateId)
34+
filecreaed, err := apiclient.ExportCustomTemplate(templateId, path)
3535
if err != nil {
3636
return err
3737
}

‎internal/storage-api/client.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"mime/multipart"
2727
"net/http"
2828
"os"
29+
"path/filepath"
2930
"strconv"
3031
"strings"
3132

@@ -171,7 +172,7 @@ func (c *StorageApiClient) ImportCustomTemplate(templateFile string) (*ImportCus
171172
return nil, err
172173
}
173174

174-
func (c *StorageApiClient) ExportCustomTemplate(templateId string) (*string, error) {
175+
func (c *StorageApiClient) ExportCustomTemplate(templateId, path string) (*string, error) {
175176

176177
if templateId == "" {
177178
return nil, fmt.Errorf("invalid template id: no id provided")
@@ -195,7 +196,7 @@ func (c *StorageApiClient) ExportCustomTemplate(templateId string) (*string, err
195196
logrus.Debugf("Export API call status: %d", res.StatusCode)
196197

197198
if res.StatusCode == 200 || res.StatusCode == 201 {
198-
outfile, fileExportPath, err := createNewLocalFile(templateId, res)
199+
outfile, fileExportPath, err := createNewLocalFile(templateId, path, res)
199200
if err != nil {
200201
return nil, err
201202
}
@@ -219,8 +220,8 @@ func (c *StorageApiClient) ExportCustomTemplate(templateId string) (*string, err
219220
return nil, err
220221
}
221222

222-
func createNewLocalFile(templateId string, res *http.Response) (*os.File, string, error) {
223-
fileExportPath, err := composeNewLocalFileName(templateId, res)
223+
func createNewLocalFile(templateId, path string, res *http.Response) (*os.File, string, error) {
224+
fileExportPath, err := composeNewLocalFileName(templateId, path, res)
224225
if err != nil {
225226
return nil, "", err
226227
}
@@ -231,8 +232,11 @@ func createNewLocalFile(templateId string, res *http.Response) (*os.File, string
231232
return outfile, fileExportPath, nil
232233
}
233234

234-
func composeNewLocalFileName(templateId string, res *http.Response) (string, error) {
235+
func composeNewLocalFileName(templateId, path string, res *http.Response) (string, error) {
235236
fileExportPath := extractFileNameFromHeader(res)
237+
if path != "" {
238+
fileExportPath = filepath.Join(path, fileExportPath)
239+
}
236240
originalFileExportName := fileExportPath
237241
if fileExportPath == "" {
238242
fileExportPath = templateId + TemplateFileExtension

0 commit comments

Comments
 (0)
Please sign in to comment.