Skip to content

Commit 45415db

Browse files
authored
Merge pull request #88 from Peefy/enhance-clean-cmd
feat: enhance clean command with extra msgbox
2 parents b804a5d + 25e5d73 commit 45415db

File tree

1 file changed

+70
-20
lines changed

1 file changed

+70
-20
lines changed

Diff for: cmd/kcl/commands/clean.go

+70-20
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,28 @@
33
package cmd
44

55
import (
6+
"bufio"
67
"fmt"
78
"os"
89
"path/filepath"
10+
"strings"
911

1012
"github.com/spf13/cobra"
1113
"kcl-lang.io/cli/pkg/fs"
1214
"kcl-lang.io/kcl-go/pkg/utils"
15+
"kcl-lang.io/kpm/pkg/env"
1316
)
1417

1518
const (
16-
cleanDesc = `This command cleans the kcl build cache.
19+
cleanDesc = `This command cleans the kcl build and module cache.
1720
`
18-
cleanExample = ` # Clean the build cache
21+
cleanExample = ` # Clean the build and module cache
1922
kcl clean`
2023
)
2124

2225
// NewCleanCmd returns the clean command.
2326
func NewCleanCmd() *cobra.Command {
27+
var assumeYes bool
2428
cmd := &cobra.Command{
2529
Use: "clean",
2630
Short: "KCL clean tool",
@@ -30,31 +34,77 @@ func NewCleanCmd() *cobra.Command {
3034
if len(args) == 0 {
3135
args = append(args, ".")
3236
}
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+
}
4341
}
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
5245
}
5346
}
5447
return nil
5548
},
5649
SilenceUsage: true,
5750
}
5851

52+
cmd.Flags().BoolVarP(&assumeYes, "yes", "y", false, "Automatically say yes to prompts")
53+
5954
return cmd
6055
}
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

Comments
 (0)