X Tutup
Skip to content

Commit a27c8a9

Browse files
committed
isolate repo view cmd
1 parent ac1333a commit a27c8a9

File tree

10 files changed

+751
-407
lines changed

10 files changed

+751
-407
lines changed

api/queries_pr.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ type NotFoundError struct {
134134
error
135135
}
136136

137+
func NewNotFoundError(err error) *NotFoundError {
138+
return &NotFoundError{err}
139+
}
140+
137141
func (err *NotFoundError) Unwrap() error {
138142
return err.error
139143
}

api/queries_repo.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package api
33
import (
44
"bytes"
55
"context"
6-
"encoding/base64"
76
"encoding/json"
87
"errors"
98
"fmt"
@@ -405,37 +404,6 @@ func RepoCreate(client *Client, input RepoCreateInput) (*Repository, error) {
405404
return initRepoHostname(&response.CreateRepository.Repository, "github.com"), nil
406405
}
407406

408-
type RepoReadme struct {
409-
Filename string
410-
Content string
411-
}
412-
413-
func RepositoryReadme(client *Client, repo ghrepo.Interface) (*RepoReadme, error) {
414-
var response struct {
415-
Name string
416-
Content string
417-
}
418-
419-
err := client.REST("GET", fmt.Sprintf("repos/%s/readme", ghrepo.FullName(repo)), nil, &response)
420-
if err != nil {
421-
var httpError HTTPError
422-
if errors.As(err, &httpError) && httpError.StatusCode == 404 {
423-
return nil, &NotFoundError{err}
424-
}
425-
return nil, err
426-
}
427-
428-
decoded, err := base64.StdEncoding.DecodeString(response.Content)
429-
if err != nil {
430-
return nil, fmt.Errorf("failed to decode readme: %w", err)
431-
}
432-
433-
return &RepoReadme{
434-
Filename: response.Name,
435-
Content: string(decoded),
436-
}, nil
437-
}
438-
439407
type RepoMetadataResult struct {
440408
AssignableUsers []RepoAssignee
441409
Labels []RepoLabel

command/repo.go

Lines changed: 0 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88
"path"
99
"strings"
10-
"text/template"
1110
"time"
1211

1312
"github.com/AlecAivazis/survey/v2"
@@ -21,7 +20,6 @@ import (
2120
)
2221

2322
func init() {
24-
RootCmd.AddCommand(repoCmd)
2523
repoCmd.AddCommand(repoCloneCmd)
2624

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

41-
repoCmd.AddCommand(repoViewCmd)
42-
repoViewCmd.Flags().BoolP("web", "w", false, "Open a repository in the browser")
43-
4439
repoCmd.AddCommand(repoCreditsCmd)
4540
repoCreditsCmd.Flags().BoolP("static", "s", false, "Print a static version of the credits")
4641
}
@@ -104,17 +99,6 @@ With no argument, creates a fork of the current repository. Otherwise, forks the
10499
RunE: repoFork,
105100
}
106101

107-
var repoViewCmd = &cobra.Command{
108-
Use: "view [<repository>]",
109-
Short: "View a repository",
110-
Long: `Display the description and the README of a GitHub repository.
111-
112-
With no argument, the repository for the current directory is displayed.
113-
114-
With '--web', open the repository in a web browser instead.`,
115-
RunE: repoView,
116-
}
117-
118102
var repoCreditsCmd = &cobra.Command{
119103
Use: "credits [<repository>]",
120104
Short: "View credits for a repository",
@@ -576,145 +560,6 @@ var Confirm = func(prompt string, result *bool) error {
576560
return survey.AskOne(p, result)
577561
}
578562

579-
func repoView(cmd *cobra.Command, args []string) error {
580-
ctx := contextForCommand(cmd)
581-
apiClient, err := apiClientForContext(ctx)
582-
if err != nil {
583-
return err
584-
}
585-
586-
var toView ghrepo.Interface
587-
if len(args) == 0 {
588-
var err error
589-
toView, err = determineBaseRepo(apiClient, cmd, ctx)
590-
if err != nil {
591-
return err
592-
}
593-
} else {
594-
repoArg := args[0]
595-
if isURL(repoArg) {
596-
parsedURL, err := url.Parse(repoArg)
597-
if err != nil {
598-
return fmt.Errorf("did not understand argument: %w", err)
599-
}
600-
601-
toView, err = ghrepo.FromURL(parsedURL)
602-
if err != nil {
603-
return fmt.Errorf("did not understand argument: %w", err)
604-
}
605-
} else {
606-
var err error
607-
toView, err = ghrepo.FromFullName(repoArg)
608-
if err != nil {
609-
return fmt.Errorf("argument error: %w", err)
610-
}
611-
}
612-
}
613-
614-
repo, err := api.GitHubRepo(apiClient, toView)
615-
if err != nil {
616-
return err
617-
}
618-
619-
web, err := cmd.Flags().GetBool("web")
620-
if err != nil {
621-
return err
622-
}
623-
624-
openURL := generateRepoURL(toView, "")
625-
if web {
626-
if connectedToTerminal(cmd) {
627-
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL))
628-
}
629-
return utils.OpenInBrowser(openURL)
630-
}
631-
632-
fullName := ghrepo.FullName(toView)
633-
634-
if !connectedToTerminal(cmd) {
635-
readme, err := api.RepositoryReadme(apiClient, toView)
636-
if err != nil {
637-
return err
638-
}
639-
640-
out := cmd.OutOrStdout()
641-
fmt.Fprintf(out, "name:\t%s\n", fullName)
642-
fmt.Fprintf(out, "description:\t%s\n", repo.Description)
643-
fmt.Fprintln(out, "--")
644-
fmt.Fprintf(out, readme.Content)
645-
646-
return nil
647-
}
648-
649-
repoTmpl := `
650-
{{.FullName}}
651-
{{.Description}}
652-
653-
{{.Readme}}
654-
655-
{{.View}}
656-
`
657-
658-
tmpl, err := template.New("repo").Parse(repoTmpl)
659-
if err != nil {
660-
return err
661-
}
662-
663-
readme, err := api.RepositoryReadme(apiClient, toView)
664-
var notFound *api.NotFoundError
665-
if err != nil && !errors.As(err, &notFound) {
666-
return err
667-
}
668-
669-
var readmeContent string
670-
if readme == nil {
671-
readmeContent = utils.Gray("This repository does not have a README")
672-
} else if isMarkdownFile(readme.Filename) {
673-
var err error
674-
readmeContent, err = utils.RenderMarkdown(readme.Content)
675-
if err != nil {
676-
return fmt.Errorf("error rendering markdown: %w", err)
677-
}
678-
} else {
679-
readmeContent = readme.Content
680-
}
681-
682-
description := repo.Description
683-
if description == "" {
684-
description = utils.Gray("No description provided")
685-
}
686-
687-
repoData := struct {
688-
FullName string
689-
Description string
690-
Readme string
691-
View string
692-
}{
693-
FullName: utils.Bold(fullName),
694-
Description: description,
695-
Readme: readmeContent,
696-
View: utils.Gray(fmt.Sprintf("View this repository on GitHub: %s", openURL)),
697-
}
698-
699-
out := colorableOut(cmd)
700-
701-
err = tmpl.Execute(out, repoData)
702-
if err != nil {
703-
return err
704-
}
705-
706-
return nil
707-
}
708-
709563
func repoCredits(cmd *cobra.Command, args []string) error {
710564
return credits(cmd, args)
711565
}
712-
713-
func isMarkdownFile(filename string) bool {
714-
// kind of gross, but i'm assuming that 90% of the time the suffix will just be .md. it didn't
715-
// seem worth executing a regex for this given that assumption.
716-
return strings.HasSuffix(filename, ".md") ||
717-
strings.HasSuffix(filename, ".markdown") ||
718-
strings.HasSuffix(filename, ".mdown") ||
719-
strings.HasSuffix(filename, ".mkdown")
720-
}

0 commit comments

Comments
 (0)
X Tutup