diff --git a/command/repo.go b/command/repo.go new file mode 100644 index 000000000..e453d8b61 --- /dev/null +++ b/command/repo.go @@ -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 [{ | }]", + 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) +} diff --git a/command/repo_test.go b/command/repo_test.go new file mode 100644 index 000000000..30da540b0 --- /dev/null +++ b/command/repo_test.go @@ -0,0 +1,89 @@ +package command + +import ( + "os/exec" + "testing" + + "github.com/cli/cli/utils" +) + +func TestRepoView(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + var seenCmd *exec.Cmd + restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + seenCmd = cmd + return &outputStub{} + }) + defer restoreCmd() + + output, err := RunCommand(repoViewCmd, "repo view") + if err != nil { + t.Errorf("error running command `repo view`: %v", err) + } + + eq(t, output.String(), "") + eq(t, output.Stderr(), "Opening https://github.com/OWNER/REPO in your browser.\n") + + if seenCmd == nil { + t.Fatal("expected a command to run") + } + url := seenCmd.Args[len(seenCmd.Args)-1] + eq(t, url, "https://github.com/OWNER/REPO") +} + +func TestRepoView_ownerRepo(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + var seenCmd *exec.Cmd + restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + seenCmd = cmd + return &outputStub{} + }) + defer restoreCmd() + + output, err := RunCommand(repoViewCmd, "repo view cli/cli") + if err != nil { + t.Errorf("error running command `repo view`: %v", err) + } + + eq(t, output.String(), "") + eq(t, output.Stderr(), "Opening https://github.com/cli/cli in your browser.\n") + + if seenCmd == nil { + t.Fatal("expected a command to run") + } + url := seenCmd.Args[len(seenCmd.Args)-1] + eq(t, url, "https://github.com/cli/cli") +} + +func TestRepoView_fullURL(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + var seenCmd *exec.Cmd + restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + seenCmd = cmd + return &outputStub{} + }) + defer restoreCmd() + + output, err := RunCommand(repoViewCmd, "repo view https://github.com/cli/cli") + if err != nil { + t.Errorf("error running command `repo view`: %v", err) + } + + eq(t, output.String(), "") + eq(t, output.Stderr(), "Opening https://github.com/cli/cli in your browser.\n") + + if seenCmd == nil { + t.Fatal("expected a command to run") + } + url := seenCmd.Args[len(seenCmd.Args)-1] + eq(t, url, "https://github.com/cli/cli") +}