Skip to content

Commit bef313a

Browse files
feat: implement app cache deletion
Introduce a `cache clean <app-id>` command to clean app cache. This implementation also tries to stop the related app if running. In case we do not care about that we can simply rm -rf `.cache/*`. Closes #52
1 parent c2b5eda commit bef313a

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cache
2+
3+
import (
4+
"github.com/arduino/arduino-app-cli/internal/orchestrator/config"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
func NewCacheCmd(cfg config.Configuration) *cobra.Command {
9+
appCmd := &cobra.Command{
10+
Use: "cache",
11+
Short: "Manage Arduino App cache",
12+
}
13+
14+
appCmd.AddCommand(newCacheCleanCmd(cfg))
15+
16+
return appCmd
17+
}

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package cache
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
cmdApp "github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/app"
8+
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/completion"
9+
"github.com/arduino/arduino-app-cli/cmd/feedback"
10+
"github.com/arduino/arduino-app-cli/internal/orchestrator"
11+
"github.com/arduino/arduino-app-cli/internal/orchestrator/app"
12+
"github.com/arduino/arduino-app-cli/internal/orchestrator/config"
13+
"github.com/spf13/cobra"
14+
)
15+
16+
func newCacheCleanCmd(cfg config.Configuration) *cobra.Command {
17+
appCmd := &cobra.Command{
18+
Use: "clean",
19+
Short: "Delete app cache",
20+
RunE: func(cmd *cobra.Command, args []string) error {
21+
if len(args) == 0 {
22+
return cmd.Help()
23+
}
24+
app, err := cmdApp.Load(args[0])
25+
if err != nil {
26+
return err
27+
}
28+
return cacheCleanHandler(cmd.Context(), app)
29+
},
30+
ValidArgsFunction: completion.ApplicationNames(cfg),
31+
}
32+
33+
return appCmd
34+
}
35+
36+
func cacheCleanHandler(ctx context.Context, app app.ArduinoApp) error {
37+
if err := orchestrator.CleanAppCache(ctx, app); err != nil {
38+
feedback.Fatal(err.Error(), feedback.ErrGeneric)
39+
}
40+
feedback.PrintResult(cacheCleanResult{
41+
AppName: app.Name,
42+
Path: app.ProvisioningStateDir().String(),
43+
})
44+
return nil
45+
}
46+
47+
type cacheCleanResult struct {
48+
AppName string `json:"appName"`
49+
Path string `json:"path"`
50+
}
51+
52+
func (r cacheCleanResult) String() string {
53+
return fmt.Sprintf("✓ Cache of %q App cleaned", r.AppName)
54+
}
55+
56+
func (r cacheCleanResult) Data() interface{} {
57+
return r
58+
}

cmd/arduino-app-cli/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/app"
2828
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/brick"
29+
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/cache"
2930
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/completion"
3031
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/config"
3132
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/daemon"
@@ -78,6 +79,7 @@ func run(configuration cfg.Configuration) error {
7879
config.NewConfigCmd(configuration),
7980
system.NewSystemCmd(configuration),
8081
version.NewVersionCmd(Version),
82+
cache.NewCacheCmd(configuration),
8183
)
8284

8385
ctx := context.Background()

internal/orchestrator/cache.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package orchestrator
2+
3+
import (
4+
"context"
5+
6+
"github.com/arduino/arduino-app-cli/internal/orchestrator/app"
7+
)
8+
9+
// CleanAppCache removes the `.cache` folder. If it detects that the app is running
10+
// it tries to stop it first.
11+
func CleanAppCache(ctx context.Context, app app.ArduinoApp) error {
12+
if app.AppComposeFilePath().Exist() {
13+
// We try to remove docker related resources at best effort
14+
_ = StopAndDestroyApp(ctx, app)
15+
}
16+
return app.ProvisioningStateDir().RemoveAll()
17+
}

0 commit comments

Comments
 (0)