-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (135 loc) · 3.81 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/charmbracelet/lipgloss"
"github.com/hay-kot/gofind/internal/core/config"
"github.com/hay-kot/gofind/internal/gofind"
"github.com/hay-kot/gofind/internal/tui"
"github.com/hay-kot/gofind/internal/ui"
"github.com/muesli/termenv"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v3"
)
func main() {
// Sets colors so they show up in stderr:
//
// Stolen from
//
// - https://github.com/charmbracelet/gum/blob/6d405c49b1b929b771cda5fe939cf5900e392b70/main.go#L31
lipgloss.SetColorProfile(termenv.NewOutput(os.Stderr).Profile)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).
With().Caller().Logger().
Level(zerolog.WarnLevel)
app := &cli.Command{
Version: "0.3.0",
Name: "gofind",
Usage: "an interactive search for directories using the filepath.Match function",
Commands: []*cli.Command{
{
Name: "cache",
Usage: "cache all config entries",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "entry",
Usage: "specific entry to re-cache",
},
},
Action: func(ctx context.Context, c *cli.Command) error {
cfg, err := config.ReadFile(config.XDGConfigPath())
if err != nil {
return err
}
finder := gofind.GoFind{Conf: cfg}
start := time.Now()
err = finder.CacheAll()
if err != nil {
log.Err(err).Msg("failed to update cache")
return err
}
log.Info().Dur("elapsed", time.Since(start)).Msg("cache updated")
fmt.Println("cached updated in", time.Since(start))
return nil
},
},
{
Name: "find",
Usage: "run interactive finder for entry",
UsageText: "gofind find [config-entry string] e.g. `gofind find repos`",
Action: func(ctx context.Context, c *cli.Command) error {
cfg, err := config.ReadFile(config.XDGConfigPath())
if err != nil {
return err
}
finder := gofind.GoFind{Conf: cfg}
entry := c.Args().Get(0)
matches, err := finder.Run(entry)
if err != nil {
log.Err(err).Str("arg", entry).Msg("finder.Run failed")
return err
}
result, err := tui.FuzzyFinder(matches)
fmt.Println(result)
return err
},
},
{
Name: "config",
Aliases: []string{"c"},
Usage: "add, remove, or list configuration entries",
Commands: []*cli.Command{
{
Name: "list",
Usage: "list all config entries",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "path",
Usage: "returns only the path",
},
},
Action: func(ctx context.Context, c *cli.Command) error {
cfg, err := config.ReadFile(config.XDGConfigPath())
if err != nil {
return err
}
p := config.XDGConfigPath()
if c.Bool("path") {
fmt.Println(p)
return nil
}
str := strings.Builder{}
str.WriteString("\n")
str.WriteString(ui.Bold("Config Path: ") + p + "\n\n")
values := [][]string{
{"Key", "Option", "Value"},
{"default", "Default Argument", cfg.Default},
{"cache", "Cache Dir", cfg.CacheDir},
{"ignore", "Ignore Patterns", strings.Join(cfg.Ignore, ", ")},
{"max_recursion", "Max Recursion", fmt.Sprintf("%d", cfg.MaxRecursion)},
}
str.WriteString(ui.Table(values))
str.WriteString("\n")
items := [][]string{
{"Arg", "Root", "Match"},
}
for key, entry := range cfg.Commands {
items = append(items, []string{key, strings.Join(entry.Roots, ", "), entry.MatchStr})
}
str.WriteString(ui.Table(items))
fmt.Println(str.String())
return nil
},
},
},
},
},
}
err := app.Run(context.Background(), os.Args)
if err != nil {
log.Fatal().Err(err).Msg("gofind failed")
}
}