-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (55 loc) · 1.32 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
package main
import (
"fmt"
"log"
"strings"
"time"
"github.com/alecthomas/kong"
"github.com/sauercrowd/autoalias/store"
)
var CLI struct {
Store struct {
Command []string `arg: ""`
} `cmd:"" help:"store a new command that was executed"`
Render struct {
} `cmd:"" help:"Generate a file that can be sourced by a shell"`
History struct {
} `cmd:"" help:"Print out the history of commands"`
}
func main() {
ctx := kong.Parse(&CLI)
store, err := store.New()
if err != nil {
log.Fatal(err)
}
switch ctx.Command() {
case "store <command>":
cmd := strings.Split(CLI.Store.Command[0], " ")
if err := store.AddHistory(cmd); err != nil {
log.Println(err)
return
}
// generate new aliases since this is the only chance it could've changed
if err := store.GenerateNewAlises(time.Hour*24*7, 20); err != nil {
log.Println(err)
}
case "render":
aliases, err := store.GetAliases()
if err != nil {
log.Fatal(err)
}
for _, alias := range aliases {
parsedCmd := strings.Join(alias.Command, " ")
fmt.Printf("alias %s='%s'\n", alias.Name, parsedCmd)
}
case "history":
results, err := store.GetHistory()
if err != nil {
log.Fatal(err)
}
for _, result := range results {
parsedCmd := strings.Join(result.Command, " ")
fmt.Println(result.Executed.Format(time.RFC3339), parsedCmd)
}
}
}