github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/repository.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 6 "github.com/jenkins-x/jx/v2/pkg/cmd/helper" 7 8 "github.com/jenkins-x/jx-logging/pkg/log" 9 "github.com/jenkins-x/jx/v2/pkg/util" 10 "github.com/pkg/browser" 11 "github.com/spf13/cobra" 12 13 "github.com/jenkins-x/jx/v2/pkg/cmd/opts" 14 "github.com/jenkins-x/jx/v2/pkg/cmd/templates" 15 ) 16 17 type RepoOptions struct { 18 *opts.CommonOptions 19 20 Dir string 21 OnlyViewURL bool 22 Quiet bool 23 } 24 25 var ( 26 repoLong = templates.LongDesc(` 27 Opens the web page for the current Git repository in a browser 28 29 You can use the '--url' argument to just display the URL without opening it`) 30 31 repoExample = templates.Examples(` 32 # Open the Git repository in a browser 33 jx repo 34 35 # Print the URL of the Git repository 36 jx repo -u 37 38 # Use the git URL in a script/pipeline 39 export URL="$(jx repo -q -b)" 40 `) 41 ) 42 43 func NewCmdRepo(commonOpts *opts.CommonOptions) *cobra.Command { 44 options := &RepoOptions{ 45 CommonOptions: commonOpts, 46 } 47 cmd := &cobra.Command{ 48 Use: "repository", 49 Aliases: []string{"repo"}, 50 Short: "Opens the web page for the current Git repository in a browser", 51 Long: repoLong, 52 Example: repoExample, 53 Run: func(cmd *cobra.Command, args []string) { 54 options.Cmd = cmd 55 options.Args = args 56 err := options.Run() 57 helper.CheckErr(err) 58 }, 59 } 60 cmd.Flags().BoolVarP(&options.OnlyViewURL, "url", "u", false, "Only displays and the URL and does not open the browser") 61 cmd.Flags().BoolVarP(&options.Quiet, "quiet", "q", false, "Quiet mode just displays the git URL only for use in scripts") 62 return cmd 63 } 64 65 func (o *RepoOptions) Run() error { 66 gitInfo, provider, _, err := o.CreateGitProvider(o.Dir) 67 if err != nil { 68 return err 69 } 70 if provider == nil { 71 return fmt.Errorf("No Git provider could be found. Are you in a directory containing a `.git/config` file?") 72 } 73 74 fullURL := gitInfo.HttpsURL() 75 if fullURL == "" { 76 return fmt.Errorf("Could not find URL from Git repository %s", gitInfo.URL) 77 } 78 if o.Quiet { 79 _, err = fmt.Fprintln(o.Out, fullURL) 80 if err != nil { 81 return err 82 } 83 return nil 84 } 85 log.Logger().Infof("repository: %s", util.ColorInfo(fullURL)) 86 if !o.OnlyViewURL { 87 err = browser.OpenURL(fullURL) 88 if err != nil { 89 return err 90 } 91 } 92 return nil 93 }