/
cmd_gc.go
63 lines (50 loc) · 1.32 KB
/
cmd_gc.go
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
61
62
63
package cli
import (
"fmt"
"github.com/9seconds/chore/internal/cli/base"
"github.com/9seconds/chore/internal/gc"
"github.com/9seconds/chore/internal/script"
"github.com/spf13/cobra"
)
func NewGC() *cobra.Command {
cmd := &cobra.Command{
Use: "gc",
Short: "Cleanup garbage in chore directories",
Args: cobra.NoArgs,
Run: base.Main(mainGC),
ValidArgsFunction: cobra.NoFileCompletions,
}
cmd.Flags().BoolP("dry-run", "n", false, "dry run")
return cmd
}
func mainGC(cmd *cobra.Command, _ []string) error {
dryRun, _ := cmd.Flags().GetBool("dry-run")
validScripts := []*script.Script{}
namespaces, err := script.ListNamespaces()
if err != nil {
return fmt.Errorf("cannot get list of all namespaces: %w", err)
}
for _, namespace := range namespaces {
scripts, err := script.ListScripts(namespace)
if err != nil {
return fmt.Errorf("cannot list scripts in %s: %w", namespace, err)
}
for _, name := range scripts {
validScripts = append(validScripts, &script.Script{
Namespace: namespace,
Executable: name,
})
}
}
paths, err := gc.Collect(validScripts)
if err != nil {
return fmt.Errorf("cannot collect paths: %w", err)
}
if !dryRun {
return gc.Remove(paths)
}
for _, path := range paths {
cmd.Println(path)
}
return nil
}