Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 <mpippi@gmail.com>
1 parent 4903373 commit 947e792

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

‎cli/cache/cache.go‎

Lines changed: 37 additions & 0 deletions
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‎

Lines changed: 49 additions & 0 deletions
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 license@arduino.cc.
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+
}

‎cli/cli.go‎

Lines changed: 2 additions & 0 deletions
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())

‎test/test_cache.py‎

Lines changed: 33 additions & 0 deletions
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 license@arduino.cc.
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)

AltStyle によって変換されたページ (->オリジナル) /