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

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

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

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

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

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

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

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

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

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

func initConfig() {
61 62 63 64 65 66 67 68
	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 已提交
69
}
LinuxSuRen's avatar
LinuxSuRen 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

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

LinuxSuRen's avatar
LinuxSuRen 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
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 ""
}

func executePreCmd(cmd *cobra.Command, _ []string) {
	config := getConfig()
	if config == nil {
		log.Fatal("Cannot find config file")
		return
	}

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

		execute(preHook.Command)
	}
}

func execute(command string) {
	array := strings.Split(command, " ")
	cmd := exec.Command(array[0], array[1:]...)
	cmd.Stdout = os.Stdout
	if err := cmd.Run(); err != nil {
		log.Fatal(err)
	}
}