Skip to content

Commit 947e792

Browse files
howjmaymasci
andauthored
Add cache clean command (#560)
* feat: Add cache clean Add command `cache clean` to clean the cache files depending on the loaction of the caching files under different OS. * feat: Add test for cache clean Clean cache files with command `arduino-cli cache clean`. * simplify test code Co-authored-by: Massimiliano Pippi <[email protected]>
1 parent 4903373 commit 947e792

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

Diff for: 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 [email protected].
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+
}

Diff for: cli/cache/clean.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 [email protected].
15+
16+
package cache
17+
18+
import (
19+
"os"
20+
21+
"github.com/arduino/arduino-cli/cli/errorcodes"
22+
"github.com/arduino/arduino-cli/cli/feedback"
23+
"github.com/sirupsen/logrus"
24+
"github.com/spf13/cobra"
25+
"github.com/spf13/viper"
26+
)
27+
28+
func initCleanCommand() *cobra.Command {
29+
cleanCommand := &cobra.Command{
30+
Use: "clean",
31+
Short: "Clean arduino cache.",
32+
Long: "Clean the files i.e. `~/arduino15/staging` in Linux.",
33+
Example: " " + os.Args[0] + " cache clean",
34+
Args: cobra.NoArgs,
35+
Run: runCleanCommand,
36+
}
37+
return cleanCommand
38+
}
39+
40+
func runCleanCommand(cmd *cobra.Command, args []string) {
41+
logrus.Info("Executing `arduino cache clean`")
42+
43+
cachePath := viper.GetString("directories.Downloads")
44+
err := os.RemoveAll(cachePath)
45+
if err != nil {
46+
feedback.Errorf("Error cleaning caches: %v", err)
47+
os.Exit(errorcodes.ErrGeneric)
48+
}
49+
}

Diff for: 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())

Diff for: test/test_cache.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 modify or
12+
# otherwise use the software for commercial activities involving the Arduino
13+
# software without disclosing the source code of your own applications. To purchase
14+
# a commercial license, send an email to [email protected].
15+
import os
16+
17+
18+
def test_cache_clean(run_command, data_dir):
19+
"""
20+
Clean the cache under arduino caching file directory which is
21+
"<Arduino configure file path>/staging"
22+
"""
23+
result = run_command("cache clean")
24+
assert result.ok
25+
26+
# Generate /staging directory
27+
result = run_command("lib list")
28+
assert result.ok
29+
30+
result = run_command("cache clean")
31+
assert result.ok
32+
33+
assert not os.path.isdir(os.path.join(data_dir, "staging"))

0 commit comments

Comments
 (0)