forked from barthr/redo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (80 loc) · 2.23 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
package main
import (
"flag"
"fmt"
"github.com/barthr/redo/config"
"github.com/barthr/redo/repository"
"github.com/barthr/redo/ui"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"log"
"os"
"os/exec"
)
var (
cfg = new(config.Config)
)
const (
helpText = `
|‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾|
| REDO |
|__________________________________________________________________|
A Command Line tool to manage all of your shell aliases at one place.
Usage:
redo (opens up the default browser to create aliases)
redo [command]
Available Commands:
help Prints out this help text.
alias-file Prints out the path to the alias file.
edit Opens the alias file in your editor (default: %s).
`
)
func main() {
cfg.FromEnv()
if err := cfg.EnsureAliasFileExists(); err != nil {
log.Fatalf("Failed to create alias file %s with error: %s", cfg.AliasPath, err)
}
repository.InitHistoryRepository(cfg.HistoryPath)
repository.InitAliasRepository(cfg.AliasPath)
defer repository.Close()
flag.Parse()
cfg.Validate()
if len(os.Args) > 1 {
switch os.Args[1] {
case "help":
fmt.Fprintf(os.Stdout, helpText, cfg.Editor)
os.Exit(0)
case "alias-file":
fmt.Println(cfg.AliasPath)
os.Exit(0)
case "edit":
openEditor()
os.Exit(0)
default:
log.Fatalf("Command: %s not found", os.Args[1])
}
} else {
history, err := repository.GetHistoryRepository().GetHistory()
if err != nil {
log.Fatalf("Failed fetching history: %v", err)
}
var historyItems []list.Item
for _, historyItem := range history {
historyItems = append(historyItems, ui.NewHistoryItem(historyItem))
}
listComponent := ui.NewHistoryItemListComponent(historyItems)
runTeaProgram(listComponent)
}
}
func openEditor() {
cmd := exec.Command(cfg.Editor, cfg.AliasPath)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
_ = cmd.Run()
}
func runTeaProgram(root tea.Model) {
if err := tea.NewProgram(root).Start(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}