-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (73 loc) · 1.86 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
package main
import (
"fmt"
"log/slog"
"os"
"time"
"github.com/allaman/werkzeugkasten/cli"
"github.com/allaman/werkzeugkasten/tool"
"github.com/allaman/werkzeugkasten/tui/model"
tea "github.com/charmbracelet/bubbletea"
)
func main() {
cfg := cli.Cli()
if cfg.Debug {
opts := &slog.HandlerOptions{
Level: slog.LevelDebug,
AddSource: true,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
if t, ok := a.Value.Any().(time.Time); ok {
return slog.String(a.Key, t.Format("2006-01-02 15:04:05"))
}
}
return a
},
}
logger := slog.New(slog.NewTextHandler(os.Stdout, opts))
slog.SetDefault(logger)
}
tools, err := tool.CreateToolData()
if err != nil {
slog.Error("could not parse tools data", "error", err)
os.Exit(1)
}
slog.Debug("download dir", "dir", cfg.DownloadDir)
if cfg.Tools {
tool.PrintTools(tools)
slog.Debug("found tools", "count", len(tools.Tools))
os.Exit(0)
}
if cfg.Categories {
tool.PrintCategories(tool.GetCategories(tools))
os.Exit(0)
}
if cfg.Category != "" {
result := tool.GetToolsByCategory(cfg.Category, tools)
if len(result.Tools) == 0 {
slog.Warn("no results found", "category", cfg.Category)
os.Exit(0)
}
tool.PrintTools(result)
slog.Debug("found tools", "category", cfg.Category, "count", len(result.Tools))
os.Exit(0)
}
// interactive mode
if len(cfg.ToolList) == 0 {
p := tea.NewProgram(model.InitialModel(tools), tea.WithAltScreen())
if _, err := p.Run(); err != nil {
fmt.Printf("Error: %v", err)
os.Exit(1)
}
} else {
// non-interactive mode
tool.InstallEget(cfg.DownloadDir)
for _, toolName := range cfg.ToolList {
err = tool.DownloadToolWithEget(cfg.DownloadDir, tools.Tools[toolName])
if err != nil {
slog.Warn("could not download tool", "tool", toolName, "error", err)
continue
}
}
}
}