Skip to content

Commit d95e8dc

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 d95e8dc

File tree

6 files changed

+105
-2
lines changed

6 files changed

+105
-2
lines changed

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

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

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

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

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()

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ require (
129129
github.com/fsnotify/fsnotify v1.9.0 // indirect
130130
github.com/fvbommel/sortorder v1.1.0 // indirect
131131
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
132-
github.com/getkin/kin-openapi v0.132.0 // indirect
132+
github.com/getkin/kin-openapi v0.133.0 // indirect
133133
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
134134
github.com/go-git/go-billy/v5 v5.6.2 // indirect
135135
github.com/go-git/go-git/v5 v5.16.2 // indirect
@@ -206,7 +206,7 @@ require (
206206
github.com/morikuni/aec v1.0.0 // indirect
207207
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
208208
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
209-
github.com/oapi-codegen/oapi-codegen/v2 v2.5.0 // indirect
209+
github.com/oapi-codegen/oapi-codegen/v2 v2.5.1 // indirect
210210
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 // indirect
211211
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 // indirect
212212
github.com/opencontainers/go-digest v1.0.0 // indirect
@@ -257,6 +257,7 @@ require (
257257
github.com/ulikunitz/xz v0.5.15 // indirect
258258
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
259259
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
260+
github.com/woodsbury/decimal128 v1.3.0 // indirect
260261
github.com/x448/float16 v0.8.4 // indirect
261262
github.com/xanzy/ssh-agent v0.3.3 // indirect
262263
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv
312312
github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
313313
github.com/getkin/kin-openapi v0.132.0 h1:3ISeLMsQzcb5v26yeJrBcdTCEQTag36ZjaGk7MIRUwk=
314314
github.com/getkin/kin-openapi v0.132.0/go.mod h1:3OlG51PCYNsPByuiMB0t4fjnNlIDnaEDsjiKUV8nL58=
315+
github.com/getkin/kin-openapi v0.133.0 h1:pJdmNohVIJ97r4AUFtEXRXwESr8b0bD721u/Tz6k8PQ=
316+
github.com/getkin/kin-openapi v0.133.0/go.mod h1:boAciF6cXk5FhPqe/NQeBTeenbjqU4LhWBf09ILVvWE=
315317
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
316318
github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c=
317319
github.com/gliderlabs/ssh v0.3.8/go.mod h1:xYoytBv1sV0aL3CavoDuJIQNURXkkfPA/wxQ1pL1fAU=
@@ -678,6 +680,8 @@ github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
678680
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
679681
github.com/oapi-codegen/oapi-codegen/v2 v2.5.0 h1:iJvF8SdB/3/+eGOXEpsWkD8FQAHj6mqkb6Fnsoc8MFU=
680682
github.com/oapi-codegen/oapi-codegen/v2 v2.5.0/go.mod h1:fwlMxUEMuQK5ih9aymrxKPQqNm2n8bdLk1ppjH+lr9w=
683+
github.com/oapi-codegen/oapi-codegen/v2 v2.5.1 h1:5vHNY1uuPBRBWqB2Dp0G7YB03phxLQZupZTIZaeorjc=
684+
github.com/oapi-codegen/oapi-codegen/v2 v2.5.1/go.mod h1:ro0npU1BWkcGpCgGD9QwPp44l5OIZ94tB3eabnT7DjQ=
681685
github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmtpMYro=
682686
github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg=
683687
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 h1:G7ERwszslrBzRxj//JalHPu/3yz+De2J+4aLtSRlHiY=
@@ -909,6 +913,8 @@ github.com/warthog618/go-gpiocdev v0.9.1 h1:pwHPaqjJfhCipIQl78V+O3l9OKHivdRDdmgX
909913
github.com/warthog618/go-gpiocdev v0.9.1/go.mod h1:dN3e3t/S2aSNC+hgigGE/dBW8jE1ONk9bDSEYfoPyl8=
910914
github.com/warthog618/go-gpiosim v0.1.1 h1:MRAEv+T+itmw+3GeIGpQJBfanUVyg0l3JCTwHtwdre4=
911915
github.com/warthog618/go-gpiosim v0.1.1/go.mod h1:YXsnB+I9jdCMY4YAlMSRrlts25ltjmuIsrnoUrBLdqU=
916+
github.com/woodsbury/decimal128 v1.3.0 h1:8pffMNWIlC0O5vbyHWFZAt5yWvWcrHA+3ovIIjVWss0=
917+
github.com/woodsbury/decimal128 v1.3.0/go.mod h1:C5UTmyTjW3JftjUFzOVhC20BEQa2a4ZKOB5I6Zjb+ds=
912918
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
913919
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
914920
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=

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)