github.com/pengwynn/gh@v1.0.1-0.20140118055701-14327ca3942e/commands/alias.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "github.com/jingweno/gh/utils" 6 "os" 7 "path/filepath" 8 "strings" 9 ) 10 11 var cmdAlias = &Command{ 12 Run: alias, 13 Usage: "alias [-s] [SHELL]", 14 Short: "Show shell instructions for wrapping git", 15 Long: `Shows shell instructions for wrapping git. If given, SHELL specifies the 16 type of shell; otherwise defaults to the value of SHELL environment 17 variable. With -s, outputs shell script suitable for eval. 18 `, 19 } 20 21 var flagAliasScript bool 22 23 func init() { 24 cmdAlias.Flag.BoolVarP(&flagAliasScript, "script", "s", false, "SCRIPT") 25 CmdRunner.Use(cmdAlias) 26 } 27 28 func alias(command *Command, args *Args) { 29 var shell string 30 if args.ParamsSize() > 0 { 31 shell = args.FirstParam() 32 } else { 33 shell = os.Getenv("SHELL") 34 } 35 36 if shell == "" { 37 utils.Check(fmt.Errorf("Unknown shell")) 38 } 39 40 shells := []string{"bash", "zsh", "sh", "ksh", "csh", "fish"} 41 shell = filepath.Base(shell) 42 var validShell bool 43 for _, s := range shells { 44 if s == shell { 45 validShell = true 46 break 47 } 48 } 49 50 if !validShell { 51 err := fmt.Errorf("gh alias: unsupported shell\nsupported shells: %s", strings.Join(shells, " ")) 52 utils.Check(err) 53 } 54 55 if flagAliasScript { 56 fmt.Println("alias git=gh") 57 if "zsh" == shell { 58 fmt.Println("if type compdef > /dev/null; then") 59 fmt.Println(" compdef gh=git") 60 fmt.Println("fi") 61 } 62 } else { 63 var profile string 64 switch shell { 65 case "bash": 66 profile = "~/.bash_profile" 67 case "zsh": 68 profile = "~/.zshrc" 69 case "ksh": 70 profile = "~/.profile" 71 case "fish": 72 profile = "~/.config/fish/config.fish" 73 default: 74 profile = "your profile" 75 } 76 77 msg := fmt.Sprintf("# Wrap git automatically by adding the following to %s:\n", profile) 78 fmt.Println(msg) 79 80 var eval string 81 if shell == "fish" { 82 eval = `eval (gh alias -s)` 83 } else { 84 eval = `eval "$(gh alias -s)"` 85 } 86 fmt.Println(eval) 87 } 88 89 os.Exit(0) 90 }