root.go 3.0 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 {
LinuxSuRen's avatar
LinuxSuRen 已提交
17
	Jenkins string
18
	Version bool
19
	Debug   bool
20 21
}

LinuxSuRen's avatar
LinuxSuRen 已提交
22
var rootCmd = &cobra.Command{
23 24 25 26 27
	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 已提交
28
	Run: func(_ *cobra.Command, _ []string) {
29 30
		fmt.Println("Jenkins CLI (jcli) manage your Jenkins")

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

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

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

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

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

func initConfig() {
62 63 64 65 66 67 68 69
	if err := loadDefaultConfig(); err != nil {
		if os.IsNotExist(err) {
			log.Printf("No config file found.")
			return
		}

		log.Fatalf("Config file is invalid: %v", err)
	}
LinuxSuRen's avatar
LinuxSuRen 已提交
70
}
LinuxSuRen's avatar
LinuxSuRen 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

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 已提交
88

LinuxSuRen's avatar
LinuxSuRen 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102
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 已提交
103
func executePreCmd(cmd *cobra.Command, _ []string, writer io.Writer) (err error) {
LinuxSuRen's avatar
LinuxSuRen 已提交
104 105
	config := getConfig()
	if config == nil {
LinuxSuRen's avatar
LinuxSuRen 已提交
106
		err = fmt.Errorf("Cannot find config file")
LinuxSuRen's avatar
LinuxSuRen 已提交
107 108 109 110 111 112 113 114 115
		return
	}

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

LinuxSuRen's avatar
LinuxSuRen 已提交
116 117 118
		if err = execute(preHook.Command, writer); err != nil {
			return
		}
LinuxSuRen's avatar
LinuxSuRen 已提交
119
	}
LinuxSuRen's avatar
LinuxSuRen 已提交
120
	return
LinuxSuRen's avatar
LinuxSuRen 已提交
121 122
}

LinuxSuRen's avatar
LinuxSuRen 已提交
123
func execute(command string, writer io.Writer) (err error) {
LinuxSuRen's avatar
LinuxSuRen 已提交
124 125
	array := strings.Split(command, " ")
	cmd := exec.Command(array[0], array[1:]...)
LinuxSuRen's avatar
LinuxSuRen 已提交
126 127 128
	cmd.Stdout = writer
	err = cmd.Run()
	return
LinuxSuRen's avatar
LinuxSuRen 已提交
129
}