-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopen.go
44 lines (37 loc) · 1019 Bytes
/
open.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
package cmd
import (
"errors"
"fmt"
"os"
appCmd "github.com/jenkins-zh/jenkins-cli/app/cmd"
"github.com/jenkins-zh/jenkins-cli/util"
"github.com/spf13/cobra"
)
func NewOpenCmd() (cmd *cobra.Command) {
opt := &CASCOpenOption{}
cmd = &cobra.Command{
Use: "open",
Short: "Open Configuration as Code page in browser",
Long: "Open Configuration as Code page in browser",
PreRun: opt.PreRun,
RunE: opt.RunE,
}
cmd.Flags().StringVarP(&opt.Browser, "browser", "b", "",
"Open Jenkins with a specific browser")
return
}
func (c *CASCOpenOption) PreRun(_ *cobra.Command, _ []string) {
if c.Browser == "" {
c.Browser = os.Getenv("JCLI_BROWSER")
}
}
func (c *CASCOpenOption) RunE(_ *cobra.Command, _ []string) (err error) {
jenkins := appCmd.GetCurrentJenkinsFromOptions()
if jenkins == nil {
err = errors.New("cannot found Jenkins by --jenkins")
return
}
browser := c.Browser
err = util.Open(fmt.Sprintf("%s/configuration-as-code", jenkins.URL), browser, c.ExecContext)
return
}