3
3
package cmd
4
4
5
5
import (
6
+ "bufio"
6
7
"fmt"
7
8
"os"
8
9
"path/filepath"
10
+ "strings"
9
11
10
12
"github.com/spf13/cobra"
11
13
"kcl-lang.io/cli/pkg/fs"
12
14
"kcl-lang.io/kcl-go/pkg/utils"
15
+ "kcl-lang.io/kpm/pkg/env"
13
16
)
14
17
15
18
const (
16
- cleanDesc = `This command cleans the kcl build cache.
19
+ cleanDesc = `This command cleans the kcl build and module cache.
17
20
`
18
- cleanExample = ` # Clean the build cache
21
+ cleanExample = ` # Clean the build and module cache
19
22
kcl clean`
20
23
)
21
24
22
25
// NewCleanCmd returns the clean command.
23
26
func NewCleanCmd () * cobra.Command {
27
+ var assumeYes bool
24
28
cmd := & cobra.Command {
25
29
Use : "clean" ,
26
30
Short : "KCL clean tool" ,
@@ -30,31 +34,77 @@ func NewCleanCmd() *cobra.Command {
30
34
if len (args ) == 0 {
31
35
args = append (args , "." )
32
36
}
33
- pkgroot , err := utils .FindPkgRoot (args [0 ])
34
- if err != nil {
35
- fmt .Println ("no cache found" )
36
- return err
37
- }
38
- cachePaths := []string {
39
- filepath .Join (pkgroot , ".kclvm/cache" ),
40
- filepath .Join (pkgroot , "__main__/.kclvm/cache" ),
41
- filepath .Join (args [0 ], ".kclvm/cache" ),
42
- filepath .Join (args [0 ], "__main__/.kclvm/cache" ),
37
+ if ok := cmdBox ("Are you sure you want to clean the build cache? [y/N]" , assumeYes ); ok {
38
+ if err := cleanBuildCache (args [0 ]); err != nil {
39
+ return err
40
+ }
43
41
}
44
- for _ , cachePath := range cachePaths {
45
- if fs .IsDir (cachePath ) {
46
- if err := os .RemoveAll (cachePath ); err == nil {
47
- fmt .Printf ("%s removed\n " , cachePath )
48
- } else {
49
- fmt .Printf ("remove %s failed\n " , cachePath )
50
- return err
51
- }
42
+ if ok := cmdBox ("Are you sure you want to clean the module cache? [y/N]" , assumeYes ); ok {
43
+ if err := cleanModCache (); err != nil {
44
+ return err
52
45
}
53
46
}
54
47
return nil
55
48
},
56
49
SilenceUsage : true ,
57
50
}
58
51
52
+ cmd .Flags ().BoolVarP (& assumeYes , "yes" , "y" , false , "Automatically say yes to prompts" )
53
+
59
54
return cmd
60
55
}
56
+
57
+ func cmdBox (msg string , assumeYes bool ) bool {
58
+ if ! assumeYes {
59
+ fmt .Println (msg )
60
+ reader := bufio .NewReader (os .Stdin )
61
+ response , err := reader .ReadString ('\n' )
62
+ if err != nil {
63
+ fmt .Println ("Failed to read input:" , err )
64
+ return false
65
+ }
66
+ if strings .TrimSpace (strings .ToLower (response )) != "y" {
67
+ fmt .Println ("Aborted." )
68
+ return false
69
+ }
70
+ }
71
+ return true
72
+ }
73
+
74
+ func cleanBuildCache (pwd string ) error {
75
+ cachePaths := []string {
76
+ filepath .Join (pwd , ".kclvm/cache" ),
77
+ filepath .Join (pwd , "__main__/.kclvm/cache" ),
78
+ }
79
+ pkgroot , err := utils .FindPkgRoot (pwd )
80
+ if err == nil {
81
+ cachePaths = append (cachePaths , filepath .Join (pkgroot , ".kclvm/cache" ), filepath .Join (pkgroot , "__main__/.kclvm/cache" ))
82
+ }
83
+ for _ , cachePath := range cachePaths {
84
+ if fs .IsDir (cachePath ) {
85
+ if err := os .RemoveAll (cachePath ); err == nil {
86
+ fmt .Printf ("%s removed\n " , cachePath )
87
+ } else {
88
+ fmt .Printf ("remove %s failed\n " , cachePath )
89
+ return err
90
+ }
91
+ }
92
+ }
93
+ return nil
94
+ }
95
+
96
+ func cleanModCache () error {
97
+ modulePath , err := env .GetAbsPkgPath ()
98
+ if err != nil {
99
+ return err
100
+ }
101
+ if fs .IsDir (modulePath ) {
102
+ if err := os .RemoveAll (modulePath ); err == nil {
103
+ fmt .Printf ("%s removed\n " , modulePath )
104
+ } else {
105
+ fmt .Printf ("remove %s failed\n " , modulePath )
106
+ return err
107
+ }
108
+ }
109
+ return nil
110
+ }
0 commit comments