-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexec.go
86 lines (75 loc) · 1.58 KB
/
exec.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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
)
var cmdBase = []string{"sh", "-c"}
func init() {
if runtime.GOOS == "windows" {
cmdBase = []string{"cmd", "/c"}
}
}
// Exec fillin
func Exec() error {
if len(os.Args) >= 2 {
switch os.Args[1] {
case "-v", "version", "-version", "--version":
printVersion()
return nil
case "-h", "help", "-help", "--help":
printHelp()
return nil
}
}
sh, err := exec.LookPath(cmdBase[0])
if err != nil {
return err
}
configDir, err := getConfigDir()
if err != nil {
return err
}
cmd, err := Run(configDir, os.Args[1:], newPrompt())
if err != nil {
return err
}
if err := syscallExec(sh, append(cmdBase, cmd), os.Environ()); err != nil {
return err
}
return nil
}
func getConfigDir() (string, error) {
if dir := os.Getenv("FILLIN_CONFIG_DIR"); dir != "" {
return dir, nil
}
if dir := os.Getenv("XDG_CONFIG_HOME"); dir != "" {
return filepath.Join(dir, name), nil
}
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".config", name), nil
}
func printVersion() {
fmt.Printf("%s %s\n", name, version)
}
func printHelp() {
fmt.Printf(`NAME:
%[1]s - %[2]s
USAGE:
%[1]s command...
EXAMPLES:
%[1]s echo {{message}} # in bash/zsh shell
%[1]s echo [[message]] # in fish shell
%[1]s psql -h {{psql:hostname}} -U {{psql:username}} -d {{psql:dbname}}
%[1]s curl {{example-api:base-url}}/api/1/example/info -H 'Authorization: Bearer {{example-api:access-token}}'
VERSION:
%[3]s
AUTHOR:
%[4]s
`, name, description, version, author)
}