Skip to content

Commit 3345c6d

Browse files
committed
feat: Add cache clean
Add command `cache clean` to clean the cache files depending on the loaction of the caching files under different OS.
1 parent ffa84fd commit 3345c6d

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

cli/cache/cache.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package cache
17+
18+
import (
19+
"os"
20+
21+
"github.com/spf13/cobra"
22+
)
23+
24+
// NewCommand created a new `cache` command
25+
func NewCommand() *cobra.Command {
26+
cacheCommand := &cobra.Command{
27+
Use: "cache",
28+
Short: "Arduino cache commands.",
29+
Long: "Arduino cache commands.",
30+
Example: "# Clean caches.\n" +
31+
" " + os.Args[0] + " cache clean\n\n",
32+
}
33+
34+
cacheCommand.AddCommand(initCleanCommand())
35+
36+
return cacheCommand
37+
}

cli/cache/clean.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package cache
17+
18+
import (
19+
"os"
20+
"path/filepath"
21+
"runtime"
22+
23+
"github.com/arduino/arduino-cli/cli/errorcodes"
24+
"github.com/arduino/arduino-cli/cli/feedback"
25+
"github.com/arduino/go-win32-utils"
26+
"github.com/sirupsen/logrus"
27+
"github.com/spf13/cobra"
28+
)
29+
30+
func initCleanCommand() *cobra.Command {
31+
cleanCommand := &cobra.Command{
32+
Use: "clean",
33+
Short: "Clean arduino cache.",
34+
Long: "Clean the files i.e. `~/arduino15/staging` in Linux.",
35+
Example: " " + os.Args[0] + " cache clean",
36+
Args: cobra.NoArgs,
37+
Run: runCleanCommand,
38+
}
39+
return cleanCommand
40+
}
41+
42+
func runCleanCommand(cmd *cobra.Command, args []string) {
43+
logrus.Info("Executing `arduino cache clean`")
44+
45+
cachePath := getDefaultArduinoDataDir() + "/staging"
46+
err := os.RemoveAll(cachePath)
47+
if err != nil {
48+
feedback.Errorf("Error cleaning caches: %v", err)
49+
os.Exit(errorcodes.ErrGeneric)
50+
}
51+
}
52+
53+
func getDefaultArduinoDataDir() string {
54+
userHomeDir, err := os.UserHomeDir()
55+
if err != nil {
56+
feedback.Errorf("Unable to get user home dir: %v", err)
57+
return "."
58+
}
59+
60+
switch runtime.GOOS {
61+
case "linux":
62+
return filepath.Join(userHomeDir, ".arduino15")
63+
case "darwin":
64+
return filepath.Join(userHomeDir, "Library", "Arduino15")
65+
case "windows":
66+
localAppDataPath, err := win32.GetLocalAppDataFolder()
67+
if err != nil {
68+
feedback.Errorf("Unable to get Local App Data Folder: %v", err)
69+
return "."
70+
}
71+
return filepath.Join(localAppDataPath, "Arduino15")
72+
default:
73+
return "."
74+
}
75+
}

cli/cli.go

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strings"
2424

2525
"github.com/arduino/arduino-cli/cli/board"
26+
"github.com/arduino/arduino-cli/cli/cache"
2627
"github.com/arduino/arduino-cli/cli/compile"
2728
"github.com/arduino/arduino-cli/cli/config"
2829
"github.com/arduino/arduino-cli/cli/core"
@@ -67,6 +68,7 @@ func init() {
6768
// this is here only for testing
6869
func createCliCommandTree(cmd *cobra.Command) {
6970
cmd.AddCommand(board.NewCommand())
71+
cmd.AddCommand(cache.NewCommand())
7072
cmd.AddCommand(compile.NewCommand())
7173
cmd.AddCommand(config.NewCommand())
7274
cmd.AddCommand(core.NewCommand())

0 commit comments

Comments
 (0)