root.go 3.3 KB
Newer Older
LinuxSuRen's avatar
LinuxSuRen 已提交
1 2 3 4
package cmd

import (
	"fmt"
LinuxSuRen's avatar
LinuxSuRen 已提交
5
	"io"
6
	"log"
LinuxSuRen's avatar
LinuxSuRen 已提交
7
	"os"
LinuxSuRen's avatar
LinuxSuRen 已提交
8 9
	"os/exec"
	"strings"
LinuxSuRen's avatar
LinuxSuRen 已提交
10

11
	"github.com/jenkins-zh/jenkins-cli/app"
LinuxSuRen's avatar
LinuxSuRen 已提交
12 13 14
	"github.com/spf13/cobra"
)

LinuxSuRen's avatar
LinuxSuRen 已提交
15
// RootOptions is a global option for whole cli
16
type RootOptions struct {
17 18 19 20
	ConfigFile string
	Jenkins    string
	Version    bool
	Debug      bool
21 22
}

LinuxSuRen's avatar
LinuxSuRen 已提交
23
var rootCmd = &cobra.Command{
24 25 26 27 28
	Use:   "jcli",
	Short: "jcli is a tool which could help you with your multiple Jenkins",
	Long: `jcli is Jenkins CLI which could help with your multiple Jenkins,
				  Manage your Jenkins and your pipelines
				  More information could found at https://jenkins-zh.cn`,
yJunS's avatar
yJunS 已提交
29
	Run: func(_ *cobra.Command, _ []string) {
30 31
		fmt.Println("Jenkins CLI (jcli) manage your Jenkins")

LinuxSuRen's avatar
LinuxSuRen 已提交
32
		current := getCurrentJenkinsFromOptionsOrDie()
33 34 35 36 37 38
		if current != nil {
			fmt.Println("Current Jenkins is:", current.Name)
		} else {
			fmt.Println("Cannot found the configuration")
		}

39
		if rootOptions.Version {
LinuxSuRen's avatar
LinuxSuRen 已提交
40 41
			fmt.Printf("Version: %s\n", app.GetVersion())
			fmt.Printf("Commit: %s\n", app.GetCommit())
42
		}
LinuxSuRen's avatar
LinuxSuRen 已提交
43 44 45 46 47 48 49 50 51 52
	},
}

func Execute() {
	if err := rootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

53
var rootOptions RootOptions
LinuxSuRen's avatar
LinuxSuRen 已提交
54 55 56

func init() {
	cobra.OnInitialize(initConfig)
57
	rootCmd.PersistentFlags().StringVarP(&rootOptions.ConfigFile, "configFile", "", "", "An alternative config file")
LinuxSuRen's avatar
LinuxSuRen 已提交
58
	rootCmd.PersistentFlags().StringVarP(&rootOptions.Jenkins, "jenkins", "j", "", "Select a Jenkins server for this time")
59
	rootCmd.PersistentFlags().BoolVarP(&rootOptions.Version, "version", "v", false, "Print the version of Jenkins CLI")
60
	rootCmd.PersistentFlags().BoolVarP(&rootOptions.Debug, "debug", "", false, "Print the output into debug.html")
LinuxSuRen's avatar
LinuxSuRen 已提交
61 62 63
}

func initConfig() {
64 65 66
	if rootOptions.ConfigFile == "" {
		if err := loadDefaultConfig(); err != nil {
			configLoadErrorHandle(err)
67
		}
68 69 70 71 72 73
	} else {
		if err := loadConfig(rootOptions.ConfigFile); err != nil {
			configLoadErrorHandle(err)
		}
	}
}
74

75 76 77 78
func configLoadErrorHandle(err error) {
	if os.IsNotExist(err) {
		log.Printf("No config file found.")
		return
79
	}
80 81

	log.Fatalf("Config file is invalid: %v", err)
LinuxSuRen's avatar
LinuxSuRen 已提交
82
}
LinuxSuRen's avatar
LinuxSuRen 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

func getCurrentJenkinsFromOptions() (jenkinsServer *JenkinsServer) {
	jenkinsOpt := rootOptions.Jenkins
	if jenkinsOpt == "" {
		jenkinsServer = getCurrentJenkins()
	} else {
		jenkinsServer = findJenkinsByName(jenkinsOpt)
	}
	return
}

func getCurrentJenkinsFromOptionsOrDie() (jenkinsServer *JenkinsServer) {
	if jenkinsServer = getCurrentJenkinsFromOptions(); jenkinsServer == nil {
		log.Fatal("Cannot found Jenkins by", rootOptions.Jenkins) // TODO not accurate
	}
	return
}
LinuxSuRen's avatar
LinuxSuRen 已提交
100

LinuxSuRen's avatar
LinuxSuRen 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114
func getCmdPath(cmd *cobra.Command) string {
	current := cmd.Use
	if cmd.HasParent() {
		parentName := getCmdPath(cmd.Parent())
		if parentName == "" {
			return current
		}

		return fmt.Sprintf("%s.%s", parentName, current)
	}
	// don't need the name of root cmd
	return ""
}

LinuxSuRen's avatar
LinuxSuRen 已提交
115
func executePreCmd(cmd *cobra.Command, _ []string, writer io.Writer) (err error) {
LinuxSuRen's avatar
LinuxSuRen 已提交
116 117
	config := getConfig()
	if config == nil {
LinuxSuRen's avatar
LinuxSuRen 已提交
118
		err = fmt.Errorf("Cannot find config file")
LinuxSuRen's avatar
LinuxSuRen 已提交
119 120 121 122 123 124 125 126 127
		return
	}

	path := getCmdPath(cmd)
	for _, preHook := range config.PreHooks {
		if path != preHook.Path {
			continue
		}

LinuxSuRen's avatar
LinuxSuRen 已提交
128 129 130
		if err = execute(preHook.Command, writer); err != nil {
			return
		}
LinuxSuRen's avatar
LinuxSuRen 已提交
131
	}
LinuxSuRen's avatar
LinuxSuRen 已提交
132
	return
LinuxSuRen's avatar
LinuxSuRen 已提交
133 134
}

LinuxSuRen's avatar
LinuxSuRen 已提交
135
func execute(command string, writer io.Writer) (err error) {
LinuxSuRen's avatar
LinuxSuRen 已提交
136 137
	array := strings.Split(command, " ")
	cmd := exec.Command(array[0], array[1:]...)
LinuxSuRen's avatar
LinuxSuRen 已提交
138 139 140
	cmd.Stdout = writer
	err = cmd.Run()
	return
LinuxSuRen's avatar
LinuxSuRen 已提交
141
}