Skip to content

Commit ac57bdc

Browse files
feature: add --force flag
Add `--force` flag to forcefully terminate the app in case it's running, and then clean the related cache.
1 parent 4f25421 commit ac57bdc

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

cmd/arduino-app-cli/cache/clean.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import (
88

99
cmdApp "github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/app"
1010
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/completion"
11+
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/internal/servicelocator"
1112
"github.com/arduino/arduino-app-cli/cmd/feedback"
1213
"github.com/arduino/arduino-app-cli/internal/orchestrator"
1314
"github.com/arduino/arduino-app-cli/internal/orchestrator/app"
1415
"github.com/arduino/arduino-app-cli/internal/orchestrator/config"
1516
)
1617

1718
func newCacheCleanCmd(cfg config.Configuration) *cobra.Command {
19+
var forceClean bool
1820
appCmd := &cobra.Command{
1921
Use: "clean",
2022
Short: "Delete app cache",
@@ -26,16 +28,23 @@ func newCacheCleanCmd(cfg config.Configuration) *cobra.Command {
2628
if err != nil {
2729
return err
2830
}
29-
return cacheCleanHandler(cmd.Context(), app)
31+
return cacheCleanHandler(cmd.Context(), app, forceClean)
3032
},
3133
ValidArgsFunction: completion.ApplicationNames(cfg),
3234
}
35+
appCmd.Flags().BoolVarP(&forceClean, "force", "", false, "Forcefully clean the cache even if the app is running")
3336

3437
return appCmd
3538
}
3639

37-
func cacheCleanHandler(ctx context.Context, app app.ArduinoApp) error {
38-
if err := orchestrator.CleanAppCache(ctx, app); err != nil {
40+
func cacheCleanHandler(ctx context.Context, app app.ArduinoApp, forceClean bool) error {
41+
err := orchestrator.CleanAppCache(
42+
ctx,
43+
servicelocator.GetDockerClient(),
44+
app,
45+
orchestrator.CleanAppCacheRequest{ForceClean: forceClean},
46+
)
47+
if err != nil {
3948
feedback.Fatal(err.Error(), feedback.ErrGeneric)
4049
}
4150
feedback.PrintResult(cacheCleanResult{

internal/orchestrator/cache.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,39 @@ package orchestrator
22

33
import (
44
"context"
5+
"errors"
6+
7+
"github.com/docker/cli/cli/command"
58

69
"github.com/arduino/arduino-app-cli/internal/orchestrator/app"
710
)
811

12+
type CleanAppCacheRequest struct {
13+
ForceClean bool
14+
}
15+
16+
var ErrCleanCacheRunningApp = errors.New("cannot remove cache of a running app")
17+
918
// CleanAppCache removes the `.cache` folder. If it detects that the app is running
1019
// it tries to stop it first.
11-
func CleanAppCache(ctx context.Context, app app.ArduinoApp) error {
12-
if app.AppComposeFilePath().Exist() {
20+
func CleanAppCache(
21+
ctx context.Context,
22+
docker command.Cli,
23+
app app.ArduinoApp,
24+
req CleanAppCacheRequest,
25+
) error {
26+
runningApp, err := getRunningApp(ctx, docker.Client())
27+
if err != nil {
28+
return err
29+
}
30+
if runningApp.FullPath.EqualsTo(app.FullPath) {
31+
return ErrCleanCacheRunningApp
32+
}
33+
34+
if req.ForceClean && app.AppComposeFilePath().Exist() {
1335
// We try to remove docker related resources at best effort
1436
_ = StopAndDestroyApp(ctx, app)
1537
}
38+
1639
return app.ProvisioningStateDir().RemoveAll()
1740
}

0 commit comments

Comments
 (0)