|
| 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 [email protected]. |
| 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 | +} |
0 commit comments