Skip to content

Commit

Permalink
feat: enhance clean command with extra msgbox
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy committed May 29, 2024
1 parent b804a5d commit 25e5d73
Showing 1 changed file with 70 additions and 20 deletions.
90 changes: 70 additions & 20 deletions cmd/kcl/commands/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@
package cmd

import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
"kcl-lang.io/cli/pkg/fs"
"kcl-lang.io/kcl-go/pkg/utils"
"kcl-lang.io/kpm/pkg/env"
)

const (
cleanDesc = `This command cleans the kcl build cache.
cleanDesc = `This command cleans the kcl build and module cache.
`
cleanExample = ` # Clean the build cache
cleanExample = ` # Clean the build and module cache
kcl clean`
)

// NewCleanCmd returns the clean command.
func NewCleanCmd() *cobra.Command {
var assumeYes bool
cmd := &cobra.Command{
Use: "clean",
Short: "KCL clean tool",
Expand All @@ -30,31 +34,77 @@ func NewCleanCmd() *cobra.Command {
if len(args) == 0 {
args = append(args, ".")
}
pkgroot, err := utils.FindPkgRoot(args[0])
if err != nil {
fmt.Println("no cache found")
return err
}
cachePaths := []string{
filepath.Join(pkgroot, ".kclvm/cache"),
filepath.Join(pkgroot, "__main__/.kclvm/cache"),
filepath.Join(args[0], ".kclvm/cache"),
filepath.Join(args[0], "__main__/.kclvm/cache"),
if ok := cmdBox("Are you sure you want to clean the build cache? [y/N]", assumeYes); ok {
if err := cleanBuildCache(args[0]); err != nil {
return err
}
}
for _, cachePath := range cachePaths {
if fs.IsDir(cachePath) {
if err := os.RemoveAll(cachePath); err == nil {
fmt.Printf("%s removed\n", cachePath)
} else {
fmt.Printf("remove %s failed\n", cachePath)
return err
}
if ok := cmdBox("Are you sure you want to clean the module cache? [y/N]", assumeYes); ok {
if err := cleanModCache(); err != nil {
return err
}
}
return nil
},
SilenceUsage: true,
}

cmd.Flags().BoolVarP(&assumeYes, "yes", "y", false, "Automatically say yes to prompts")

return cmd
}

func cmdBox(msg string, assumeYes bool) bool {
if !assumeYes {
fmt.Println(msg)
reader := bufio.NewReader(os.Stdin)
response, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Failed to read input:", err)
return false
}
if strings.TrimSpace(strings.ToLower(response)) != "y" {
fmt.Println("Aborted.")
return false
}
}
return true
}

func cleanBuildCache(pwd string) error {
cachePaths := []string{
filepath.Join(pwd, ".kclvm/cache"),
filepath.Join(pwd, "__main__/.kclvm/cache"),
}
pkgroot, err := utils.FindPkgRoot(pwd)
if err == nil {
cachePaths = append(cachePaths, filepath.Join(pkgroot, ".kclvm/cache"), filepath.Join(pkgroot, "__main__/.kclvm/cache"))
}
for _, cachePath := range cachePaths {
if fs.IsDir(cachePath) {
if err := os.RemoveAll(cachePath); err == nil {
fmt.Printf("%s removed\n", cachePath)
} else {
fmt.Printf("remove %s failed\n", cachePath)
return err
}
}
}
return nil
}

func cleanModCache() error {
modulePath, err := env.GetAbsPkgPath()
if err != nil {
return err
}
if fs.IsDir(modulePath) {
if err := os.RemoveAll(modulePath); err == nil {
fmt.Printf("%s removed\n", modulePath)
} else {
fmt.Printf("remove %s failed\n", modulePath)
return err
}
}
return nil
}

0 comments on commit 25e5d73

Please sign in to comment.