forked from metafates/mangal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
clear.go
66 lines (57 loc) 路 1.65 KB
/
clear.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
64
65
66
package cmd
import (
"fmt"
"github.com/belphemur/mangal/filesystem"
"github.com/belphemur/mangal/icon"
"github.com/belphemur/mangal/util"
"github.com/belphemur/mangal/where"
"github.com/samber/lo"
"github.com/samber/mo"
"github.com/spf13/cobra"
)
type clearTarget struct {
name string
argLong string
argShort mo.Option[string]
location func() string
}
var clearTargets = []clearTarget{
{"cache directory", "cache", mo.Some("c"), where.Cache},
{"history file", "history", mo.Some("s"), where.History},
{"anilist binds", "anilist", mo.Some("a"), where.AnilistBinds},
{"queries history", "queries", mo.Some("q"), where.Queries},
}
func init() {
rootCmd.AddCommand(clearCmd)
for _, target := range clearTargets {
help := fmt.Sprintf("clear %s", target.name)
if target.argShort.IsPresent() {
clearCmd.Flags().BoolP(target.argLong, target.argShort.MustGet(), false, help)
} else {
clearCmd.Flags().Bool(target.argLong, false, help)
}
}
}
var clearCmd = &cobra.Command{
Use: "clear",
Short: "Clears a sidelined files",
Run: func(cmd *cobra.Command, args []string) {
var anyCleared bool
doClear := func(what string) bool {
return lo.Must(cmd.Flags().GetBool(what))
}
for _, target := range clearTargets {
if doClear(target.argLong) {
anyCleared = true
e := util.PrintErasable(fmt.Sprintf("%s Clearing %s...", icon.Get(icon.Progress), util.Capitalize(target.name)))
_ = util.Delete(target.location())
e()
fmt.Printf("%s %s cleared\n", icon.Get(icon.Success), util.Capitalize(target.name))
handleErr(filesystem.Api().RemoveAll(target.location()))
}
}
if !anyCleared {
handleErr(cmd.Help())
}
},
}