-
Notifications
You must be signed in to change notification settings - Fork 787
/
repository.go
84 lines (71 loc) · 1.89 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package cmd
import (
"fmt"
"io"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/browser"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
)
type RepoOptions struct {
CommonOptions
Dir string
OnlyViewURL bool
}
var (
repoLong = templates.LongDesc(`
Opens the web page for the current Git repository in a browser
You can use the '--url' argument to just display the URL without opening it`)
repoExample = templates.Examples(`
# Open the Git repository in a browser
jx repo
# Print the URL of the Git repository
jx repo -u
`)
)
func NewCmdRepo(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &RepoOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "repository",
Aliases: []string{"repo"},
Short: "Opens the web page for the current Git repository in a browser",
Long: repoLong,
Example: repoExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addCommonFlags(cmd)
cmd.Flags().BoolVarP(&options.OnlyViewURL, "url", "u", false, "Only displays and the URL and does not open the browser")
return cmd
}
func (o *RepoOptions) Run() error {
gitInfo, provider, _, err := o.createGitProvider(o.Dir)
if err != nil {
return err
}
if provider == nil {
return fmt.Errorf("No Git provider could be found. Are you in a directory containing a `.git/config` file?")
}
fullURL := gitInfo.HttpsURL()
if fullURL == "" {
return fmt.Errorf("Could not find URL from Git repository %s", gitInfo.URL)
}
log.Infof("repository: %s\n", util.ColorInfo(fullURL))
if !o.OnlyViewURL {
browser.OpenURL(fullURL)
}
return nil
}