Add repo view command

```
$ gh repo
Work with GitHub repositories.

A repository can be supplied as an argument in any of the following formats:
- by owner/repo, e.g. "cli/cli"
- by URL, e.g. "https://github.com/cli/cli"

Usage:
  gh repo [command]

Available Commands:
  view        View a repository in the browser
```
This commit is contained in:
Scott Penrose 2020-02-09 00:00:05 -05:00
parent f6856d9783
commit 7c76fb645a
No known key found for this signature in database
GPG key ID: F4666FC471E2FE70
2 changed files with 146 additions and 0 deletions

57
command/repo.go Normal file
View file

@ -0,0 +1,57 @@
package command
import (
"fmt"
"strings"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/utils"
"github.com/spf13/cobra"
)
func init() {
RootCmd.AddCommand(repoCmd)
repoCmd.AddCommand(repoViewCmd)
}
var repoCmd = &cobra.Command{
Use: "repo",
Short: "View repositories",
Long: `Work with GitHub repositories.
A repository can be supplied as an argument in any of the following formats:
- by owner/repo, e.g. "cli/cli"
- by URL, e.g. "https://github.com/cli/cli"`,
}
var repoViewCmd = &cobra.Command{
Use: "view [{<owner/repo> | <url>}]",
Short: "View a repository in the browser",
Long: `View a repository specified by the argument in the browser.
Without an argument, the repository that belongs to the current
branch is opened.`,
RunE: repoView,
}
func repoView(cmd *cobra.Command, args []string) error {
ctx := contextForCommand(cmd)
baseRepo, err := determineBaseRepo(cmd, ctx)
if err != nil {
return err
}
var openURL string
if len(args) == 0 {
openURL = fmt.Sprintf("https://github.com/%s", ghrepo.FullName(*baseRepo))
} else {
if strings.HasPrefix(args[0], "http") {
openURL = args[0]
} else {
openURL = fmt.Sprintf("https://github.com/%s", args[0])
}
}
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", openURL)
return utils.OpenInBrowser(openURL)
}