Skip to content

Commit 0d295ca

Browse files
Add profile dump command
It dumps the content of the project file.
1 parent a2c86ef commit 0d295ca

File tree

6 files changed

+960
-616
lines changed

6 files changed

+960
-616
lines changed

commands/service_profile_dump.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-cli.
2+
//
3+
// Copyright 2025 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package commands
17+
18+
import (
19+
"context"
20+
"encoding/json"
21+
"fmt"
22+
23+
"github.com/arduino/arduino-cli/commands/cmderrors"
24+
"github.com/arduino/arduino-cli/internal/arduino/sketch"
25+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
26+
"github.com/arduino/go-paths-helper"
27+
)
28+
29+
func (s *arduinoCoreServerImpl) ProfileDump(ctx context.Context, req *rpc.ProfileDumpRequest) (*rpc.ProfileDumpResponse, error) {
30+
sk, err := sketch.New(paths.New(req.GetSketchPath()))
31+
if err != nil {
32+
return nil, err
33+
}
34+
switch req.GetDumpFormat() {
35+
case "yaml":
36+
return &rpc.ProfileDumpResponse{EncodedProfile: sk.Project.AsYaml()}, nil
37+
case "json":
38+
data, err := json.MarshalIndent(sk.Project, "", " ")
39+
if err != nil {
40+
return nil, fmt.Errorf("error marshalling settings: %v", err)
41+
}
42+
return &rpc.ProfileDumpResponse{EncodedProfile: string(data)}, nil
43+
default:
44+
return nil, &cmderrors.InvalidArgumentError{Message: fmt.Sprintf("unsupported format: %s", req.GetDumpFormat())}
45+
}
46+
}

internal/cli/profile/dump.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2025 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package profile
17+
18+
import (
19+
"context"
20+
"os"
21+
22+
"github.com/arduino/arduino-cli/internal/cli/arguments"
23+
"github.com/arduino/arduino-cli/internal/cli/feedback"
24+
"github.com/arduino/arduino-cli/internal/i18n"
25+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
26+
"github.com/spf13/cobra"
27+
)
28+
29+
func initDumpCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
30+
dumpCommand := &cobra.Command{
31+
Use: "dump",
32+
Short: i18n.Tr("Dumps the project file."),
33+
Long: i18n.Tr("Dumps the project file."),
34+
Example: "" +
35+
" " + os.Args[0] + " profile dump\n",
36+
Args: cobra.MaximumNArgs(1),
37+
Run: func(cmd *cobra.Command, args []string) {
38+
runDumpCommand(cmd.Context(), args, srv)
39+
},
40+
}
41+
42+
return dumpCommand
43+
}
44+
45+
func runDumpCommand(ctx context.Context, args []string, srv rpc.ArduinoCoreServiceServer) {
46+
path := ""
47+
if len(args) > 0 {
48+
path = args[0]
49+
}
50+
51+
sketchPath := arguments.InitSketchPath(path)
52+
res := &rawResult{}
53+
switch feedback.GetFormat() {
54+
case feedback.JSON, feedback.MinifiedJSON:
55+
resp, err := srv.ProfileDump(ctx, &rpc.ProfileDumpRequest{SketchPath: sketchPath.String(), DumpFormat: "json"})
56+
if err != nil {
57+
feedback.Fatal(i18n.Tr("Error dumping the profile: %v", err), feedback.ErrBadArgument)
58+
}
59+
res.rawJSON = []byte(resp.GetEncodedProfile())
60+
case feedback.Text:
61+
resp, err := srv.ProfileDump(ctx, &rpc.ProfileDumpRequest{SketchPath: sketchPath.String(), DumpFormat: "yaml"})
62+
if err != nil {
63+
feedback.Fatal(i18n.Tr("Error dumping the profile: %v", err), feedback.ErrBadArgument)
64+
}
65+
res.rawYAML = []byte(resp.GetEncodedProfile())
66+
default:
67+
feedback.Fatal(i18n.Tr("Unsupported format: %s", feedback.GetFormat()), feedback.ErrBadArgument)
68+
}
69+
feedback.PrintResult(dumpResult{Config: res})
70+
}
71+
72+
type rawResult struct {
73+
rawJSON []byte
74+
rawYAML []byte
75+
}
76+
77+
func (r *rawResult) MarshalJSON() ([]byte, error) {
78+
// it is already encoded in rawJSON field
79+
return r.rawJSON, nil
80+
}
81+
82+
type dumpResult struct {
83+
Config *rawResult `json:"project"`
84+
}
85+
86+
func (dr dumpResult) Data() interface{} {
87+
return dr
88+
}
89+
90+
func (dr dumpResult) String() string {
91+
// In case of text output do not wrap the output in outer JSON or YAML structure
92+
return string(dr.Config.rawYAML)
93+
}

internal/cli/profile/profile.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
4040
profileCommand.AddCommand(initInitCommand(srv))
4141
profileCommand.AddCommand(initLibCommand(srv))
4242
profileCommand.AddCommand(initSetDefaultCommand(srv))
43+
profileCommand.AddCommand(initDumpCommand(srv))
4344

4445
return profileCommand
4546
}

0 commit comments

Comments
 (0)