From 8f812d23019713d460cea8d8c9f548e2f1e25f7e Mon Sep 17 00:00:00 2001 From: YangHau Date: Sat, 11 Jan 2020 15:44:43 +0800 Subject: [PATCH 1/3] feat: Add cache clean Add command `cache clean` to clean the cache files depending on the loaction of the caching files under different OS. --- cli/cache/cache.go | 37 +++++++++++++++++++++++ cli/cache/clean.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++ cli/cli.go | 2 ++ 3 files changed, 114 insertions(+) create mode 100644 cli/cache/cache.go create mode 100644 cli/cache/clean.go diff --git a/cli/cache/cache.go b/cli/cache/cache.go new file mode 100644 index 00000000000..cb544aed6ca --- /dev/null +++ b/cli/cache/cache.go @@ -0,0 +1,37 @@ +// This file is part of arduino-cli. +// +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package cache + +import ( + "os" + + "github.com/spf13/cobra" +) + +// NewCommand created a new `cache` command +func NewCommand() *cobra.Command { + cacheCommand := &cobra.Command{ + Use: "cache", + Short: "Arduino cache commands.", + Long: "Arduino cache commands.", + Example: "# Clean caches.\n" + + " " + os.Args[0] + " cache clean\n\n", + } + + cacheCommand.AddCommand(initCleanCommand()) + + return cacheCommand +} diff --git a/cli/cache/clean.go b/cli/cache/clean.go new file mode 100644 index 00000000000..e9ec5a79a49 --- /dev/null +++ b/cli/cache/clean.go @@ -0,0 +1,75 @@ +// This file is part of arduino-cli. +// +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package cache + +import ( + "os" + "path/filepath" + "runtime" + + "github.com/arduino/arduino-cli/cli/errorcodes" + "github.com/arduino/arduino-cli/cli/feedback" + "github.com/arduino/go-win32-utils" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +func initCleanCommand() *cobra.Command { + cleanCommand := &cobra.Command{ + Use: "clean", + Short: "Clean arduino cache.", + Long: "Clean the files i.e. `~/arduino15/staging` in Linux.", + Example: " " + os.Args[0] + " cache clean", + Args: cobra.NoArgs, + Run: runCleanCommand, + } + return cleanCommand +} + +func runCleanCommand(cmd *cobra.Command, args []string) { + logrus.Info("Executing `arduino cache clean`") + + cachePath := getDefaultArduinoDataDir() + "/staging" + err := os.RemoveAll(cachePath) + if err != nil { + feedback.Errorf("Error cleaning caches: %v", err) + os.Exit(errorcodes.ErrGeneric) + } +} + +func getDefaultArduinoDataDir() string { + userHomeDir, err := os.UserHomeDir() + if err != nil { + feedback.Errorf("Unable to get user home dir: %v", err) + return "." + } + + switch runtime.GOOS { + case "linux": + return filepath.Join(userHomeDir, ".arduino15") + case "darwin": + return filepath.Join(userHomeDir, "Library", "Arduino15") + case "windows": + localAppDataPath, err := win32.GetLocalAppDataFolder() + if err != nil { + feedback.Errorf("Unable to get Local App Data Folder: %v", err) + return "." + } + return filepath.Join(localAppDataPath, "Arduino15") + default: + return "." + } +} diff --git a/cli/cli.go b/cli/cli.go index 65091c9dbaa..f1c81d7a697 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -23,6 +23,7 @@ import ( "strings" "github.com/arduino/arduino-cli/cli/board" + "github.com/arduino/arduino-cli/cli/cache" "github.com/arduino/arduino-cli/cli/compile" "github.com/arduino/arduino-cli/cli/config" "github.com/arduino/arduino-cli/cli/core" @@ -67,6 +68,7 @@ func init() { // this is here only for testing func createCliCommandTree(cmd *cobra.Command) { cmd.AddCommand(board.NewCommand()) + cmd.AddCommand(cache.NewCommand()) cmd.AddCommand(compile.NewCommand()) cmd.AddCommand(config.NewCommand()) cmd.AddCommand(core.NewCommand()) From 2c25c957e1f95a630b33e0bad35cfe4fc3af006f Mon Sep 17 00:00:00 2001 From: HowJMay Date: Sat, 18 Jan 2020 13:38:19 +0800 Subject: [PATCH 2/3] feat: Add test for cache clean Clean cache files with command `arduino-cli cache clean`. --- cli/cache/clean.go | 30 ++---------------------------- test/test_cache.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 28 deletions(-) create mode 100644 test/test_cache.py diff --git a/cli/cache/clean.go b/cli/cache/clean.go index e9ec5a79a49..e8d676f7de9 100644 --- a/cli/cache/clean.go +++ b/cli/cache/clean.go @@ -17,14 +17,12 @@ package cache import ( "os" - "path/filepath" - "runtime" "github.com/arduino/arduino-cli/cli/errorcodes" "github.com/arduino/arduino-cli/cli/feedback" - "github.com/arduino/go-win32-utils" "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/spf13/viper" ) func initCleanCommand() *cobra.Command { @@ -42,34 +40,10 @@ func initCleanCommand() *cobra.Command { func runCleanCommand(cmd *cobra.Command, args []string) { logrus.Info("Executing `arduino cache clean`") - cachePath := getDefaultArduinoDataDir() + "/staging" + cachePath := viper.GetString("directories.Downloads") err := os.RemoveAll(cachePath) if err != nil { feedback.Errorf("Error cleaning caches: %v", err) os.Exit(errorcodes.ErrGeneric) } } - -func getDefaultArduinoDataDir() string { - userHomeDir, err := os.UserHomeDir() - if err != nil { - feedback.Errorf("Unable to get user home dir: %v", err) - return "." - } - - switch runtime.GOOS { - case "linux": - return filepath.Join(userHomeDir, ".arduino15") - case "darwin": - return filepath.Join(userHomeDir, "Library", "Arduino15") - case "windows": - localAppDataPath, err := win32.GetLocalAppDataFolder() - if err != nil { - feedback.Errorf("Unable to get Local App Data Folder: %v", err) - return "." - } - return filepath.Join(localAppDataPath, "Arduino15") - default: - return "." - } -} diff --git a/test/test_cache.py b/test/test_cache.py new file mode 100644 index 00000000000..c2ad7b92753 --- /dev/null +++ b/test/test_cache.py @@ -0,0 +1,41 @@ +# This file is part of arduino-cli. +# +# Copyright 2020 ARDUINO SA (http://www.arduino.cc/) +# +# This software is released under the GNU General Public License version 3, +# which covers the main part of arduino-cli. +# The terms of this license can be found at: +# https://www.gnu.org/licenses/gpl-3.0.en.html +# +# You can be released from the requirements of the above licenses by purchasing +# a commercial license. Buying such a license is mandatory if you want to modify or +# otherwise use the software for commercial activities involving the Arduino +# software without disclosing the source code of your own applications. To purchase +# a commercial license, send an email to license@arduino.cc. +import os +import platform + + +def test_cache_clean(run_command): + """ + Clean the cache under arduino caching file directory which is + "/staging" + """ + result = run_command("cache clean") + assert result.ok + + # Generate /staging directory + result = run_command("lib list") + assert result.ok + + result = run_command("cache clean") + assert result.ok + + running_platform = platform.system() + homeDir = os.path.expanduser("~") + if running_platform == "Linux": + assert not (os.path.isdir(homeDir + ".arduino15/staging")) + elif running_platform == "Darwin": + assert not (os.path.isdir(homeDir + "Library/Arduino15/staging")) + elif running_platform == "Windows": + assert not (os.path.isdir(homeDir + "Arduino15/staging")) From 87098572d2a128684ecc65c450b1ef88fbc4e4d2 Mon Sep 17 00:00:00 2001 From: Massimiliano Pippi Date: Tue, 4 Feb 2020 15:43:05 +0100 Subject: [PATCH 3/3] simplify test code --- test/test_cache.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/test/test_cache.py b/test/test_cache.py index c2ad7b92753..cd02486cf7e 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -13,10 +13,9 @@ # software without disclosing the source code of your own applications. To purchase # a commercial license, send an email to license@arduino.cc. import os -import platform -def test_cache_clean(run_command): +def test_cache_clean(run_command, data_dir): """ Clean the cache under arduino caching file directory which is "/staging" @@ -31,11 +30,4 @@ def test_cache_clean(run_command): result = run_command("cache clean") assert result.ok - running_platform = platform.system() - homeDir = os.path.expanduser("~") - if running_platform == "Linux": - assert not (os.path.isdir(homeDir + ".arduino15/staging")) - elif running_platform == "Darwin": - assert not (os.path.isdir(homeDir + "Library/Arduino15/staging")) - elif running_platform == "Windows": - assert not (os.path.isdir(homeDir + "Arduino15/staging")) + assert not os.path.isdir(os.path.join(data_dir, "staging"))