99 "flag"
1010 "fmt"
1111 "io"
12+ "os"
1213 "path/filepath"
1314
1415 "golang.org/x/benchmarks/sweet/cli/assets"
@@ -32,6 +33,7 @@ type getCmd struct {
3233 cache string
3334 copyDir string
3435 version string
36+ clean bool
3537}
3638
3739func (* getCmd ) Name () string { return "get" }
@@ -44,12 +46,37 @@ func (c *getCmd) SetFlags(f *flag.FlagSet) {
4446 f .StringVar (& c .cache , "cache" , assets .CacheDefault (), "cache location for assets" )
4547 f .StringVar (& c .version , "version" , common .Version , "the version to download assets for" )
4648 f .StringVar (& c .copyDir , "copy" , "" , "location to extract assets into, useful for development" )
49+ f .BoolVar (& c .clean , "clean" , false , "delete all cached assets before installing new ones" )
4750}
4851
4952func (c * getCmd ) Run (_ []string ) error {
5053 log .SetActivityLog (true )
5154 ctx := context .Background ()
5255
56+ // Do some cleanup, if needed.
57+ if c .clean {
58+ for {
59+ log .Printf ("Deleting cache directory %s" , c .cache )
60+ fmt .Print ("This is a destructive action. Please confirm. (y/n): " )
61+ var r string
62+ _ , err := fmt .Scanf ("%s\n " , & r )
63+ if err != nil {
64+ fmt .Printf ("Invalid input: %v\n " , err )
65+ } else {
66+ if r == "y" {
67+ break
68+ } else if r == "n" {
69+ return nil
70+ } else {
71+ fmt .Println ("Input must be exactly 'y' or 'n'." )
72+ }
73+ }
74+ }
75+ if err := os .RemoveAll (c .cache ); err != nil {
76+ return fmt .Errorf ("failed to delete cache directory %s: %v" , c .cache , err )
77+ }
78+ }
79+
5380 // Load CIPD options, including auth, cache dir, etc. from env. The package is public, but we
5481 // want to be authenticated transparently when we pull the assets down on the builders.
5582 var opts cipd.ClientOptions
0 commit comments