-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (125 loc) · 2.83 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
package main
import (
"flag"
"fmt"
"os"
"os/exec"
)
func main() {
var helpFlag bool
// flag.BoolVar(&saveCommandFlag, "s", false, "Save the command")
flag.BoolVar(&helpFlag, "h", false, "Show help")
flag.Parse()
if helpFlag {
fmt.Println("falsename - a simple cross-shell command aliaser. For help: fn help")
return
}
if flag.NArg() == 0 { // choose
commands := GetAllCommands()
selectedAlias := ChooseAlias(commands)
if selectedAlias == -1 {
return
}
if selectedAlias == -2 {
fmt.Println("No aliases found. Use 'fn save' to save a command.")
return
}
runCommand(commands[selectedAlias].Command)
return
}
if flag.Arg(0) == "list" { // list
commands := GetAllCommands()
for _, cmd := range commands {
fmt.Printf("%s: %s\n", cmd.Name, cmd.Command)
}
return
}
if flag.Arg(0) == "get" { // get
cmd := GetCommand(flag.Arg(1))
fmt.Println(cmd)
return
}
if flag.Arg(0) == "shell" { // shell
if flag.NArg() == 1 {
shell := GetShell()
fmt.Println(shell)
return
}
if flag.NArg() == 2 {
SetShell(flag.Arg(1))
return
}
}
if flag.Arg(0) == "help" { // help
showHelp()
return
}
if flag.Arg(0) == "delete" { // delete
DeleteCommand(flag.Arg(1))
return
}
if flag.Arg(0) == "save" {
if flag.NArg() != 3 {
fmt.Println("Usage: fn save <alias> <command>")
return
}
SaveCommand(flag.Arg(1), flag.Arg(2))
return
}
if flag.Arg(0) == "config" {
if flag.NArg() == 1 {
fmt.Println(DataDirPath)
return
}
if flag.NArg() == 2 {
DataDirPath = flag.Arg(1)
SetDataDirPath(DataDirPath)
return
}
}
if flag.NArg() >= 1 { // run
cmd := GetCommand(flag.Arg(0))
if cmd == "--not found--" {
fmt.Println("Alias not found. Use fn save to save a command.")
return
}
remainingArgs := flag.Args()[1:]
argStr := ""
for _, arg := range remainingArgs {
argStr += " " + arg
}
cmd += argStr
runCommand(cmd)
return
}
showHelp()
}
func showHelp() {
configText := fmt.Sprintf(`
falsename - a simple cross-shell command aliaser
Usage:
fn -> choose an alias
fn <alias> [args] -> run an alias with optional arguments
fn list -> list all aliases
fn get <alias> -> get the command for an alias
fn save <alias> <command> -> save an alias
fn delete <alias> -> delete an alias
fn shell -> get configured shell (default /bin/sh)
fn shell <shell> -> set shell (not recommended unless you don't have /bin/sh)
fn config -> get the config directory
fn config <dir> -> set the config directory
The config directory is %s
`, DataDirPath)
fmt.Println(configText)
}
func runCommand(cmdStr string) {
if cmdStr == "--not found--" {
fmt.Println("Alias not found. Use fn save to save a command.")
return
}
cmd := exec.Command(GetShell(), "-c", cmdStr)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}