forked from mislav/hub
/
alias.go
106 lines (92 loc) · 2.14 KB
/
alias.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
package commands
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/github/hub/ui"
"github.com/github/hub/utils"
)
var cmdAlias = &Command{
Run: alias,
Usage: "alias [-s] [SHELL]",
Short: "Show shell instructions for wrapping git",
Long: `Shows shell instructions for wrapping git. If given, SHELL specifies the
type of shell; otherwise defaults to the value of SHELL environment
variable. With -s, outputs shell script suitable for eval.
`,
}
var flagAliasScript bool
func init() {
cmdAlias.Flag.BoolVarP(&flagAliasScript, "script", "s", false, "SCRIPT")
CmdRunner.Use(cmdAlias)
}
func alias(command *Command, args *Args) {
var shell string
if args.ParamsSize() > 0 {
shell = args.FirstParam()
} else {
shell = os.Getenv("SHELL")
}
if shell == "" {
cmd := "hub alias <shell>"
if flagAliasScript {
cmd = "hub alias -s <shell>"
}
utils.Check(fmt.Errorf("Error: couldn't detect shell type. Please specify your shell with `%s`", cmd))
}
shells := []string{"bash", "zsh", "sh", "ksh", "csh", "tcsh", "fish"}
shell = filepath.Base(shell)
var validShell bool
for _, s := range shells {
if s == shell {
validShell = true
break
}
}
if !validShell {
err := fmt.Errorf("hub alias: unsupported shell\nsupported shells: %s", strings.Join(shells, " "))
utils.Check(err)
}
if flagAliasScript {
var alias string
switch shell {
case "csh", "tcsh":
alias = "alias git hub"
default:
alias = "alias git=hub"
}
ui.Println(alias)
} else {
var profile string
switch shell {
case "bash":
profile = "~/.bash_profile"
case "zsh":
profile = "~/.zshrc"
case "ksh":
profile = "~/.profile"
case "fish":
profile = "~/.config/fish/config.fish"
case "csh":
profile = "~/.cshrc"
case "tcsh":
profile = "~/.tcshrc"
default:
profile = "your profile"
}
msg := fmt.Sprintf("# Wrap git automatically by adding the following to %s:\n", profile)
ui.Println(msg)
var eval string
switch shell {
case "fish":
eval = `eval (hub alias -s)`
case "csh", "tcsh":
eval = "eval \"`hub alias -s`\""
default:
eval = `eval "$(hub alias -s)"`
}
ui.Println(eval)
}
os.Exit(0)
}