hub/commands/alias.go

130 строки
2.5 KiB
Go
Исходник Обычный вид История

2013-06-29 20:54:06 +04:00
package commands
import (
"fmt"
"os"
"path/filepath"
"regexp"
2013-06-29 20:54:06 +04:00
"strings"
"github.com/github/hub/ui"
"github.com/github/hub/utils"
2013-06-29 20:54:06 +04:00
)
var cmdAlias = &Command{
Run: alias,
Usage: "alias [-s] [<SHELL>]",
Long: `Show shell instructions for wrapping git.
## Options
-s
Output shell script suitable for 'eval'.
<SHELL>
Specify the type of shell (default: "$SHELL" environment variable).
2016-01-24 18:50:01 +03:00
## See also:
hub(1)
2013-06-29 20:54:06 +04:00
`,
}
var flagAliasScript bool
func init() {
cmdAlias.Flag.BoolVarP(&flagAliasScript, "script", "s", false, "SCRIPT")
CmdRunner.Use(cmdAlias)
2013-06-29 20:54:06 +04:00
}
func alias(command *Command, args *Args) {
var shell string
2013-07-02 22:56:45 +04:00
if args.ParamsSize() > 0 {
shell = args.FirstParam()
2013-06-29 20:54:06 +04:00
} else {
shell = os.Getenv("SHELL")
}
if shell == "" {
2016-01-21 10:23:28 +03:00
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))
2013-06-29 20:54:06 +04:00
}
2017-03-02 04:16:24 +03:00
shells := []string{"bash", "zsh", "sh", "ksh", "csh", "tcsh", "fish", "rc"}
2013-06-29 20:54:06 +04:00
shell = filepath.Base(shell)
var validShell bool
for _, s := range shells {
if s == shell {
validShell = true
break
}
}
if !validShell {
2014-03-25 02:42:24 +04:00
err := fmt.Errorf("hub alias: unsupported shell\nsupported shells: %s", strings.Join(shells, " "))
2013-06-29 20:54:06 +04:00
utils.Check(err)
}
if flagAliasScript {
var alias string
switch shell {
case "csh", "tcsh":
alias = "alias git hub"
2017-03-02 04:16:24 +03:00
case "rc":
alias = "fn git { builtin hub $* }"
default:
alias = "alias git=hub"
}
ui.Println(alias)
2013-06-29 20:54:06 +04:00
} else {
var profile string
switch shell {
case "bash":
profile = "~/.bash_profile"
case "zsh":
profile = "~/.zshrc"
case "ksh":
profile = "~/.profile"
2013-12-14 19:37:16 +04:00
case "fish":
profile = "~/.config/fish/functions/git.fish"
case "csh":
profile = "~/.cshrc"
case "tcsh":
profile = "~/.tcshrc"
2017-03-02 04:16:24 +03:00
case "rc":
profile = "$home/lib/profile"
2013-06-29 20:54:06 +04:00
default:
profile = "your profile"
}
2013-12-14 19:37:16 +04:00
msg := fmt.Sprintf("# Wrap git automatically by adding the following to %s:\n", profile)
ui.Println(msg)
2013-12-14 19:37:16 +04:00
var eval string
switch shell {
case "fish":
2017-01-25 18:49:09 +03:00
eval = `function git --wraps hub --description 'Alias for hub, which wraps git to provide extra functionality with GitHub.'
hub $argv
end`
2017-03-02 04:16:24 +03:00
case "rc":
eval = "eval `{hub alias -s}"
case "csh", "tcsh":
eval = "eval \"`hub alias -s`\""
default:
2014-03-25 02:42:24 +04:00
eval = `eval "$(hub alias -s)"`
2013-12-14 19:37:16 +04:00
}
indent := regexp.MustCompile(`(?m)^\t+`)
eval = indent.ReplaceAllStringFunc(eval, func(match string) string {
2016-08-07 20:26:57 +03:00
return strings.Repeat(" ", len(match)*4)
})
ui.Println(eval)
2013-06-29 20:54:06 +04:00
}
args.NoForward()
2013-06-29 20:54:06 +04:00
}