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
2322func 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-
118102var 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-
709563func 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