|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/arduino/go-paths-helper" |
| 7 | + "github.com/spf13/cobra" |
| 8 | + |
| 9 | + "github.com/bcmi-labs/orchestrator/cmd/arduino-app-cli/results" |
| 10 | + "github.com/bcmi-labs/orchestrator/cmd/feedback" |
| 11 | + "github.com/bcmi-labs/orchestrator/internal/orchestrator" |
| 12 | +) |
| 13 | + |
| 14 | +func newCreateCmd() *cobra.Command { |
| 15 | + var ( |
| 16 | + icon string |
| 17 | + bricks []string |
| 18 | + noPyton bool |
| 19 | + noSketch bool |
| 20 | + fromApp string |
| 21 | + ) |
| 22 | + |
| 23 | + cmd := &cobra.Command{ |
| 24 | + Use: "new name", |
| 25 | + Short: "Creates a new app", |
| 26 | + Args: cobra.ExactArgs(1), |
| 27 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 28 | + cobra.MinimumNArgs(1) |
| 29 | + name := args[0] |
| 30 | + return createHandler(cmd.Context(), name, icon, bricks, noPyton, noSketch, fromApp) |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + cmd.Flags().StringVarP(&icon, "icon", "i", "", "Icon for the app") |
| 35 | + cmd.Flags().StringVarP(&fromApp, "from-app", "", "", "Create the new app from the path of an existing app") |
| 36 | + cmd.Flags().StringArrayVarP(&bricks, "bricks", "b", []string{}, "List of bricks to include in the app") |
| 37 | + cmd.Flags().BoolVarP(&noPyton, "no-python", "", false, "Do not include Python files") |
| 38 | + cmd.Flags().BoolVarP(&noSketch, "no-sketch", "", false, "Do not include Sketch files") |
| 39 | + cmd.MarkFlagsMutuallyExclusive("no-python", "no-sketch") |
| 40 | + |
| 41 | + return cmd |
| 42 | +} |
| 43 | + |
| 44 | +func createHandler(ctx context.Context, name string, icon string, bricks []string, noPython, noSketch bool, fromApp string) error { |
| 45 | + if fromApp != "" { |
| 46 | + wd, err := paths.Getwd() |
| 47 | + if err != nil { |
| 48 | + feedback.Fatal(err.Error(), feedback.ErrGeneric) |
| 49 | + return nil |
| 50 | + } |
| 51 | + fromPath := paths.New(fromApp) |
| 52 | + if !fromPath.IsAbs() { |
| 53 | + fromPath = wd.JoinPath(fromPath) |
| 54 | + } |
| 55 | + id, err := orchestrator.NewIDFromPath(fromPath) |
| 56 | + if err != nil { |
| 57 | + feedback.Fatal(err.Error(), feedback.ErrBadArgument) |
| 58 | + return nil |
| 59 | + } |
| 60 | + |
| 61 | + resp, err := orchestrator.CloneApp(ctx, orchestrator.CloneAppRequest{ |
| 62 | + Name: &name, |
| 63 | + FromID: id, |
| 64 | + }) |
| 65 | + if err != nil { |
| 66 | + feedback.Fatal(err.Error(), feedback.ErrGeneric) |
| 67 | + return nil |
| 68 | + } |
| 69 | + dst := resp.ID.ToPath() |
| 70 | + |
| 71 | + feedback.PrintResult(results.CreateAppResult{ |
| 72 | + Result: "ok", |
| 73 | + Message: "App created successfully", |
| 74 | + Path: dst.String(), |
| 75 | + }) |
| 76 | + |
| 77 | + } else { |
| 78 | + resp, err := orchestrator.CreateApp(ctx, orchestrator.CreateAppRequest{ |
| 79 | + Name: name, |
| 80 | + Icon: icon, |
| 81 | + Bricks: bricks, |
| 82 | + SkipPython: noPython, |
| 83 | + SkipSketch: noSketch, |
| 84 | + }) |
| 85 | + if err != nil { |
| 86 | + feedback.Fatal(err.Error(), feedback.ErrGeneric) |
| 87 | + return nil |
| 88 | + } |
| 89 | + feedback.PrintResult(results.CreateAppResult{ |
| 90 | + Result: "ok", |
| 91 | + Message: "App created successfully", |
| 92 | + Path: resp.ID.ToPath().String(), |
| 93 | + }) |
| 94 | + } |
| 95 | + return nil |
| 96 | +} |
0 commit comments