]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add "go clean -cache"
authorRuss Cox <rsc@golang.org>
Thu, 2 Nov 2017 02:16:24 +0000 (22:16 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 3 Nov 2017 17:44:54 +0000 (17:44 +0000)
Give users a way to remove their caches.

Change-Id: I0b041aa54b318e98605675f168fed54ab9b6fd14
Reviewed-on: https://go-review.googlesource.com/75470
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
src/cmd/dist/deps.go
src/cmd/go/alldocs.go
src/cmd/go/internal/clean/clean.go

index 55891d41d2f1535434df49711b0b4e176eea3cc0..fa525f0545e68fcf6d15540faaf3c2e793472382 100644 (file)
@@ -112,10 +112,11 @@ var builddeps = map[string][]string{
        },
 
        "cmd/go/internal/clean": {
-               "cmd/go/internal/base", // cmd/go/internal/clean
-               "cmd/go/internal/cfg",  // cmd/go/internal/clean
-               "cmd/go/internal/load", // cmd/go/internal/clean
-               "cmd/go/internal/work", // cmd/go/internal/clean
+               "cmd/go/internal/base",  // cmd/go/internal/clean
+               "cmd/go/internal/cache", // cmd/go/internal/clean
+               "cmd/go/internal/cfg",   // cmd/go/internal/clean
+               "cmd/go/internal/load",  // cmd/go/internal/clean
+               "cmd/go/internal/work",  // cmd/go/internal/clean
                "fmt",           // cmd/go/internal/clean
                "io/ioutil",     // cmd/go/internal/clean
                "os",            // cmd/go/internal/clean
index 7facbd45a267628f1a0eef1dca00e8e69e5f410b..a61aba824994c07d25f76eaf0b52e35f5f822653 100644 (file)
 //
 // Usage:
 //
-//     go clean [-i] [-r] [-n] [-x] [build flags] [packages]
+//     go clean [-i] [-r] [-n] [-x] [-cache] [build flags] [packages]
 //
 // Clean removes object files from package source directories.
 // The go command builds most objects in a temporary directory,
 //
 // The -x flag causes clean to print remove commands as it executes them.
 //
+// The -cache flag causes clean to remove the entire go build cache,
+// in addition to cleaning specified packages (if any).
+//
 // For more about build flags, see 'go help build'.
 //
 // For more about specifying packages, see 'go help packages'.
index b0688e6221e9b8ccfd784c8c1eac444288a2b8b1..de0aa01cabe98243d46b6f83d07a875f0b3f0ef7 100644 (file)
@@ -13,13 +13,14 @@ import (
        "strings"
 
        "cmd/go/internal/base"
+       "cmd/go/internal/cache"
        "cmd/go/internal/cfg"
        "cmd/go/internal/load"
        "cmd/go/internal/work"
 )
 
 var CmdClean = &base.Command{
-       UsageLine: "clean [-i] [-r] [-n] [-x] [build flags] [packages]",
+       UsageLine: "clean [-i] [-r] [-n] [-x] [-cache] [build flags] [packages]",
        Short:     "remove object files",
        Long: `
 Clean removes object files from package source directories.
@@ -58,14 +59,20 @@ dependencies of the packages named by the import paths.
 
 The -x flag causes clean to print remove commands as it executes them.
 
+The -cache flag causes clean to remove the entire go build cache,
+in addition to cleaning specified packages (if any).
+
 For more about build flags, see 'go help build'.
 
 For more about specifying packages, see 'go help packages'.
        `,
 }
 
-var cleanI bool // clean -i flag
-var cleanR bool // clean -r flag
+var (
+       cleanI     bool // clean -i flag
+       cleanR     bool // clean -r flag
+       cleanCache bool // clean -cache flag
+)
 
 func init() {
        // break init cycle
@@ -73,6 +80,8 @@ func init() {
 
        CmdClean.Flag.BoolVar(&cleanI, "i", false, "")
        CmdClean.Flag.BoolVar(&cleanR, "r", false, "")
+       CmdClean.Flag.BoolVar(&cleanCache, "cache", false, "")
+
        // -n and -x are important enough to be
        // mentioned explicitly in the docs but they
        // are part of the build flags.
@@ -84,6 +93,33 @@ func runClean(cmd *base.Command, args []string) {
        for _, pkg := range load.PackagesAndErrors(args) {
                clean(pkg)
        }
+
+       if cleanCache {
+               var b work.Builder
+               b.Print = fmt.Print
+               dir := cache.DefaultDir()
+               if dir != "off" {
+                       // Remove the cache subdirectories but not the top cache directory.
+                       // The top cache directory may have been created with special permissions
+                       // and not something that we want to remove. Also, we'd like to preserve
+                       // the access log for future analysis, even if the cache is cleared.
+                       subdirs, _ := filepath.Glob(filepath.Join(dir, "[0-9a-f][0-9a-f]"))
+                       if len(subdirs) > 0 {
+                               if cfg.BuildN || cfg.BuildX {
+                                       b.Showcmd("", "rm -r %s", strings.Join(subdirs, " "))
+                               }
+                               printedErrors := false
+                               for _, d := range subdirs {
+                                       // Only print the first error - there may be many.
+                                       // This also mimics what os.RemoveAll(dir) would do.
+                                       if err := os.RemoveAll(d); err != nil && !printedErrors {
+                                               printedErrors = true
+                                               base.Errorf("go clean -cache: %v", err)
+                                       }
+                               }
+                       }
+               }
+       }
 }
 
 var cleaned = map[*load.Package]bool{}