-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_clean.go
More file actions
60 lines (52 loc) · 1.44 KB
/
Copy pathcommand_clean.go
File metadata and controls
60 lines (52 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package contexting
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
)
func newCleanCommand() *cobra.Command {
var dryRun bool
var rootPath string
cmd := &cobra.Command{
Use: "clean",
Short: "Remove .ctxt/ directory and all ctxt files from a project",
RunE: func(cmd *cobra.Command, args []string) error {
absRoot, err := filepath.Abs(rootPath)
if err != nil {
return fmt.Errorf("resolve root path: %w", err)
}
ctxDir := filepath.Join(absRoot, ".ctxt")
info, err := os.Stat(ctxDir)
if os.IsNotExist(err) {
LogInfof("No .ctxt/ directory found at %s — nothing to clean.", ctxDir)
return nil
}
if err != nil {
return fmt.Errorf("check .ctxt directory: %w", err)
}
if !info.IsDir() {
return fmt.Errorf("%s exists but is not a directory", ctxDir)
}
if dryRun {
entries, err := os.ReadDir(ctxDir)
if err != nil {
return fmt.Errorf("read .ctxt directory: %w", err)
}
LogInfof("Would remove %s (%d entries):", ctxDir, len(entries))
for _, e := range entries {
fmt.Printf(" %s\n", e.Name())
}
return nil
}
if err := os.RemoveAll(ctxDir); err != nil {
return fmt.Errorf("remove .ctxt directory: %w", err)
}
LogInfof("Removed %s", ctxDir)
return nil
},
}
cmd.Flags().BoolVar(&dryRun, "dry-run", false, "Show what would be removed without deleting")
cmd.Flags().StringVar(&rootPath, "root", ".", "Project root path")
return cmd
}