Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isolate repo view command #1409

Merged
merged 10 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/queries_pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ type NotFoundError struct {
error
}

func NewNotFoundError(err error) *NotFoundError {
return &NotFoundError{err}
}

func (err *NotFoundError) Unwrap() error {
return err.error
}
Expand Down
32 changes: 0 additions & 32 deletions api/queries_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package api
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -405,37 +404,6 @@ func RepoCreate(client *Client, input RepoCreateInput) (*Repository, error) {
return initRepoHostname(&response.CreateRepository.Repository, "github.com"), nil
}

type RepoReadme struct {
Filename string
Content string
}

func RepositoryReadme(client *Client, repo ghrepo.Interface) (*RepoReadme, error) {
var response struct {
Name string
Content string
}

err := client.REST("GET", fmt.Sprintf("repos/%s/readme", ghrepo.FullName(repo)), nil, &response)
if err != nil {
var httpError HTTPError
if errors.As(err, &httpError) && httpError.StatusCode == 404 {
return nil, &NotFoundError{err}
}
return nil, err
}

decoded, err := base64.StdEncoding.DecodeString(response.Content)
if err != nil {
return nil, fmt.Errorf("failed to decode readme: %w", err)
}

return &RepoReadme{
Filename: response.Name,
Content: string(decoded),
}, nil
}

type RepoMetadataResult struct {
AssignableUsers []RepoAssignee
Labels []RepoLabel
Expand Down
155 changes: 0 additions & 155 deletions command/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"path"
"strings"
"text/template"
"time"

"github.com/AlecAivazis/survey/v2"
Expand All @@ -21,7 +20,6 @@ import (
)

func init() {
RootCmd.AddCommand(repoCmd)
repoCmd.AddCommand(repoCloneCmd)

repoCmd.AddCommand(repoCreateCmd)
Expand All @@ -38,9 +36,6 @@ func init() {
repoForkCmd.Flags().Lookup("clone").NoOptDefVal = "true"
repoForkCmd.Flags().Lookup("remote").NoOptDefVal = "true"

repoCmd.AddCommand(repoViewCmd)
repoViewCmd.Flags().BoolP("web", "w", false, "Open a repository in the browser")

repoCmd.AddCommand(repoCreditsCmd)
repoCreditsCmd.Flags().BoolP("static", "s", false, "Print a static version of the credits")
}
Expand Down Expand Up @@ -104,17 +99,6 @@ With no argument, creates a fork of the current repository. Otherwise, forks the
RunE: repoFork,
}

var repoViewCmd = &cobra.Command{
Use: "view [<repository>]",
Short: "View a repository",
Long: `Display the description and the README of a GitHub repository.

With no argument, the repository for the current directory is displayed.

With '--web', open the repository in a web browser instead.`,
RunE: repoView,
}

var repoCreditsCmd = &cobra.Command{
Use: "credits [<repository>]",
Short: "View credits for a repository",
Expand Down Expand Up @@ -576,145 +560,6 @@ var Confirm = func(prompt string, result *bool) error {
return survey.AskOne(p, result)
}

func repoView(cmd *cobra.Command, args []string) error {
ctx := contextForCommand(cmd)
apiClient, err := apiClientForContext(ctx)
if err != nil {
return err
}

var toView ghrepo.Interface
if len(args) == 0 {
var err error
toView, err = determineBaseRepo(apiClient, cmd, ctx)
if err != nil {
return err
}
} else {
repoArg := args[0]
if isURL(repoArg) {
parsedURL, err := url.Parse(repoArg)
if err != nil {
return fmt.Errorf("did not understand argument: %w", err)
}

toView, err = ghrepo.FromURL(parsedURL)
if err != nil {
return fmt.Errorf("did not understand argument: %w", err)
}
} else {
var err error
toView, err = ghrepo.FromFullName(repoArg)
if err != nil {
return fmt.Errorf("argument error: %w", err)
}
}
}

repo, err := api.GitHubRepo(apiClient, toView)
if err != nil {
return err
}

web, err := cmd.Flags().GetBool("web")
if err != nil {
return err
}

openURL := generateRepoURL(toView, "")
if web {
if connectedToTerminal(cmd) {
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL))
}
return utils.OpenInBrowser(openURL)
}

fullName := ghrepo.FullName(toView)

if !connectedToTerminal(cmd) {
readme, err := api.RepositoryReadme(apiClient, toView)
if err != nil {
return err
}

out := cmd.OutOrStdout()
fmt.Fprintf(out, "name:\t%s\n", fullName)
fmt.Fprintf(out, "description:\t%s\n", repo.Description)
fmt.Fprintln(out, "--")
fmt.Fprintf(out, readme.Content)

return nil
}

repoTmpl := `
{{.FullName}}
{{.Description}}

{{.Readme}}

{{.View}}
`

tmpl, err := template.New("repo").Parse(repoTmpl)
if err != nil {
return err
}

readme, err := api.RepositoryReadme(apiClient, toView)
var notFound *api.NotFoundError
if err != nil && !errors.As(err, &notFound) {
return err
}

var readmeContent string
if readme == nil {
readmeContent = utils.Gray("This repository does not have a README")
} else if isMarkdownFile(readme.Filename) {
var err error
readmeContent, err = utils.RenderMarkdown(readme.Content)
if err != nil {
return fmt.Errorf("error rendering markdown: %w", err)
}
} else {
readmeContent = readme.Content
}

description := repo.Description
if description == "" {
description = utils.Gray("No description provided")
}

repoData := struct {
FullName string
Description string
Readme string
View string
}{
FullName: utils.Bold(fullName),
Description: description,
Readme: readmeContent,
View: utils.Gray(fmt.Sprintf("View this repository on GitHub: %s", openURL)),
}

out := colorableOut(cmd)

err = tmpl.Execute(out, repoData)
if err != nil {
return err
}

return nil
}

func repoCredits(cmd *cobra.Command, args []string) error {
return credits(cmd, args)
}

func isMarkdownFile(filename string) bool {
// kind of gross, but i'm assuming that 90% of the time the suffix will just be .md. it didn't
// seem worth executing a regex for this given that assumption.
return strings.HasSuffix(filename, ".md") ||
strings.HasSuffix(filename, ".markdown") ||
strings.HasSuffix(filename, ".mdown") ||
strings.HasSuffix(filename, ".mkdown")
}
Loading