Skip to content

Add cache clean command #560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions cli/cache/cache.go
Original file line number Diff line number Diff line change
@@ -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 [email protected].

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
}
49 changes: 49 additions & 0 deletions cli/cache/clean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 [email protected].

package cache

import (
"os"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

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 := viper.GetString("directories.Downloads")
err := os.RemoveAll(cachePath)
if err != nil {
feedback.Errorf("Error cleaning caches: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
}
2 changes: 2 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down
33 changes: 33 additions & 0 deletions test/test_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 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 [email protected].
import os


def test_cache_clean(run_command, data_dir):
"""
Clean the cache under arduino caching file directory which is
"<Arduino configure file path>/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

assert not os.path.isdir(os.path.join(data_dir, "staging"))