github.com/pengwynn/gh@v1.0.1-0.20140118055701-14327ca3942e/commands/browse.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "github.com/jingweno/gh/github" 6 "github.com/jingweno/gh/utils" 7 "net/url" 8 "reflect" 9 "strings" 10 ) 11 12 var cmdBrowse = &Command{ 13 Run: browse, 14 Usage: "browse [-u] [-p] [[<USER>/]<REPOSITORY>] [SUBPAGE]", 15 Short: "Open a GitHub page in the default browser", 16 Long: `Open repository's GitHub page in the system's default web browser using 17 "open(1)" or the "BROWSER" env variable. If the repository isn't 18 specified with "-p", "browse" opens the page of the repository found in the current 19 directory. If SUBPAGE is specified, the browser will open on the specified 20 subpage: one of "wiki", "commits", "issues" or other (the default is 21 "tree"). With "-u", outputs the URL rather than opening the browser. 22 `, 23 } 24 25 var ( 26 flagBrowseURLOnly bool 27 flagBrowseProject string 28 ) 29 30 func init() { 31 cmdBrowse.Flag.BoolVarP(&flagBrowseURLOnly, "url-only", "u", false, "URL only") 32 cmdBrowse.Flag.StringVarP(&flagBrowseProject, "project", "p", "", "PROJECT") 33 34 CmdRunner.Use(cmdBrowse) 35 } 36 37 /* 38 $ gh browse 39 > open https://github.com/YOUR_USER/CURRENT_REPO 40 41 $ gh browse commit/SHA 42 > open https://github.com/YOUR_USER/CURRENT_REPO/commit/SHA 43 44 $ gh browse issues 45 > open https://github.com/YOUR_USER/CURRENT_REPO/issues 46 47 $ gh browse -p jingweno/gh 48 > open https://github.com/jingweno/gh 49 50 $ gh browse -p jingweno/gh commit/SHA 51 > open https://github.com/jingweno/gh/commit/SHA 52 53 $ gh browse -p resque 54 > open https://github.com/YOUR_USER/resque 55 56 $ gh browse -p resque network 57 > open https://github.com/YOUR_USER/resque/network 58 */ 59 func browse(command *Command, args *Args) { 60 var ( 61 project *github.Project 62 branch *github.Branch 63 err error 64 ) 65 localRepo := github.LocalRepo() 66 if flagBrowseProject != "" { 67 // gh browse -p jingweno/gh 68 // gh browse -p gh 69 project = github.NewProject("", flagBrowseProject, "") 70 } else { 71 // gh browse 72 branch, project, err = localRepo.RemoteBranchAndProject("") 73 utils.Check(err) 74 } 75 76 if project == nil { 77 err := fmt.Errorf(command.FormattedUsage()) 78 utils.Check(err) 79 } 80 81 master := localRepo.MasterBranch() 82 if branch == nil { 83 branch = master 84 } 85 86 var subpage string 87 if !args.IsParamsEmpty() { 88 subpage = args.RemoveParam(0) 89 } 90 91 if subpage == "commits" { 92 subpage = fmt.Sprintf("commits/%s", branchInURL(branch)) 93 } else if subpage == "tree" || subpage == "" { 94 if !reflect.DeepEqual(branch, master) && branch.IsRemote() { 95 subpage = fmt.Sprintf("tree/%s", branchInURL(branch)) 96 } 97 } 98 99 pageUrl := project.WebURL("", "", subpage) 100 launcher, err := utils.BrowserLauncher() 101 utils.Check(err) 102 103 if flagBrowseURLOnly { 104 args.Replace("echo", pageUrl) 105 } else { 106 args.Replace(launcher[0], "", launcher[1:]...) 107 args.AppendParams(pageUrl) 108 } 109 } 110 111 func branchInURL(branch *github.Branch) string { 112 parts := strings.Split(strings.Replace(branch.ShortName(), ".", "/", -1), "/") 113 newPath := make([]string, len(parts)) 114 for i, s := range parts { 115 newPath[i] = url.QueryEscape(s) 116 } 117 return strings.Join(newPath, "/") 118 }