forked from jenkins-zh/jenkins-cli
/
open.go
78 lines (66 loc) · 1.65 KB
/
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
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
package cmd
import (
"fmt"
"log"
"os/exec"
"runtime"
"github.com/AlecAivazis/survey"
"github.com/spf13/cobra"
)
type OpenOption struct {
InteractiveOption
Name string
Config bool
}
var openOption OpenOption
func init() {
rootCmd.AddCommand(openCmd)
openCmd.PersistentFlags().StringVarP(&openOption.Name, "name", "n", "", "Open a specific Jenkins by name")
openCmd.PersistentFlags().BoolVarP(&openOption.Config, "config", "c", false, "Open the configuration page of Jenkins")
openOption.SetFlag(openCmd)
}
var openCmd = &cobra.Command{
Use: "open",
Short: "Open your Jenkins with a browse",
Long: `Open your Jenkins with a browse`,
Run: func(cmd *cobra.Command, args []string) {
var jenkins *JenkinsServer
if openOption.Name == "" && openOption.Interactive {
jenkinsNames := getJenkinsNames()
prompt := &survey.Select{
Message: "Choose a Jenkins that you want to open:",
Options: jenkinsNames,
}
survey.AskOne(prompt, &(openOption.Name))
}
if openOption.Name != "" {
jenkins = findJenkinsByName(openOption.Name)
} else {
jenkins = getCurrentJenkins()
}
if jenkins != nil && jenkins.URL != "" {
url := jenkins.URL
if openOption.Config {
url = fmt.Sprintf("%s/configure", url)
}
open(url)
} else {
log.Fatalf("No URL found with Jenkins %s", openOption.Name)
}
},
}
func open(url string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
}
args = append(args, url)
return exec.Command(cmd, args...).Start()
}