-
Notifications
You must be signed in to change notification settings - Fork 7
[CLI] Add a destroy app command #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
9da6e0b
f17e499
7ebf7f2
1c30eba
ef16820
a5d8f3d
274a600
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // This file is part of arduino-app-cli. | ||
| // | ||
| // Copyright 2025 ARDUINO SA (http://www.arduino.cc/) | ||
| // | ||
| // This software is released under the GNU General Public License version 3, | ||
| // which covers the main part of arduino-app-cli. | ||
| // The terms of this license can be found at: | ||
| // https://www.gnu.org/licenses/gpl-3.0.en.html | ||
| // | ||
| // You can be released from the requirements of the above licenses by purchasing | ||
| // a commercial license. Buying such a license is mandatory if you want to | ||
| // modify or otherwise use the software for commercial activities involving the | ||
| // Arduino software without disclosing the source code of your own applications. | ||
| // To purchase a commercial license, send an email to license@arduino.cc. | ||
|
|
||
| package app | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/completion" | ||
| "github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/internal/servicelocator" | ||
| "github.com/arduino/arduino-app-cli/cmd/feedback" | ||
| "github.com/arduino/arduino-app-cli/internal/orchestrator" | ||
| "github.com/arduino/arduino-app-cli/internal/orchestrator/app" | ||
| "github.com/arduino/arduino-app-cli/internal/orchestrator/config" | ||
| ) | ||
|
|
||
| func newDestroyCmd(cfg config.Configuration) *cobra.Command { | ||
| return &cobra.Command{ | ||
| Use: "destroy app_path", | ||
| Short: "Destroy an Arduino App", | ||
| Args: cobra.MaximumNArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if len(args) == 0 { | ||
| return cmd.Help() | ||
| } | ||
| app, err := Load(args[0]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return destroyHandler(cmd.Context(), app) | ||
| }, | ||
| ValidArgsFunction: completion.ApplicationNamesWithFilterFunc(cfg, func(apps orchestrator.AppInfo) bool { | ||
| return apps.Status != orchestrator.StatusUninitialized | ||
| }), | ||
| } | ||
| } | ||
|
|
||
| func destroyHandler(ctx context.Context, app app.ArduinoApp) error { | ||
| out, _, getResult := feedback.OutputStreams() | ||
|
|
||
| for message := range orchestrator.StopAndDestroyApp(ctx, servicelocator.GetDockerClient(), app) { | ||
| switch message.GetType() { | ||
| case orchestrator.ProgressType: | ||
| fmt.Fprintf(out, "Progress[%s]: %.0f%%\n", message.GetProgress().Name, message.GetProgress().Progress) | ||
| case orchestrator.InfoType: | ||
| fmt.Fprintln(out, "[INFO]", message.GetData()) | ||
| case orchestrator.ErrorType: | ||
| feedback.Fatal(message.GetError().Error(), feedback.ErrGeneric) | ||
| return nil | ||
| } | ||
| } | ||
| outputResult := getResult() | ||
|
|
||
| feedback.PrintResult(destroyAppResult{ | ||
| AppName: app.Name, | ||
| Status: "uninitialized", | ||
| Output: outputResult, | ||
| }) | ||
| return nil | ||
| } | ||
|
|
||
| type destroyAppResult struct { | ||
| AppName string `json:"appName"` | ||
| Status string `json:"status"` | ||
| Output *feedback.OutputStreamsResult `json:"output,omitempty"` | ||
| } | ||
|
|
||
| func (r destroyAppResult) String() string { | ||
| return fmt.Sprintf("✓ App '%q destroyed successfully.", r.AppName) | ||
| } | ||
|
|
||
| func (r destroyAppResult) Data() interface{} { | ||
| return r | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -150,7 +150,10 @@ func getAppStatusByPath( | |
| return nil, fmt.Errorf("failed to list containers: %w", err) | ||
| } | ||
| if len(containers) == 0 { | ||
| return nil, nil | ||
| return &AppStatusInfo{ | ||
| AppPath: paths.New(pathLabel), | ||
| Status: StatusUninitialized, | ||
| }, nil | ||
| } | ||
|
|
||
| app := parseAppStatus(containers) | ||
|
|
@@ -160,23 +163,17 @@ func getAppStatusByPath( | |
| return &app[0], nil | ||
| } | ||
|
|
||
| // TODO: merge this with the more efficient getAppStatusByPath | ||
| func getAppStatus( | ||
|
||
| ctx context.Context, | ||
| docker command.Cli, | ||
| app app.ArduinoApp, | ||
| ) (AppStatusInfo, error) { | ||
| apps, err := getAppsStatus(ctx, docker.Client()) | ||
| statusInfo, err := getAppStatusByPath(ctx, docker.Client(), app.FullPath.String()) | ||
|
|
||
| if err != nil { | ||
| return AppStatusInfo{}, fmt.Errorf("failed to get app status: %w", err) | ||
| } | ||
| idx := slices.IndexFunc(apps, func(a AppStatusInfo) bool { | ||
| return a.AppPath.String() == app.FullPath.String() | ||
| }) | ||
| if idx == -1 { | ||
| return AppStatusInfo{}, fmt.Errorf("app %s not found", app.FullPath) | ||
| } | ||
| return apps[idx], nil | ||
| return *statusInfo, nil | ||
| } | ||
|
|
||
| func getRunningApp( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you revert all the changes related to
getAppStatus? I would prefer to do it in another PR. Here, for instance, you are saying that an app isuninitializedwithout checking if it actually exists.