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

import (
	"fmt"
5
	"log"
LinuxSuRen's avatar
LinuxSuRen 已提交
6 7
	"os"

8
	"github.com/linuxsuren/jenkins-cli/app"
LinuxSuRen's avatar
LinuxSuRen 已提交
9 10 11
	"github.com/spf13/cobra"
)

12 13 14 15
type RootOptions struct {
	Version bool
}

LinuxSuRen's avatar
LinuxSuRen 已提交
16
var rootCmd = &cobra.Command{
17 18 19 20 21
	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`,
LinuxSuRen's avatar
LinuxSuRen 已提交
22
	Run: func(cmd *cobra.Command, args []string) {
23 24
		fmt.Println("Jenkins CLI (jcli) manage your Jenkins")

25 26 27 28 29 30 31
		current := getCurrentJenkins()
		if current != nil {
			fmt.Println("Current Jenkins is:", current.Name)
		} else {
			fmt.Println("Cannot found the configuration")
		}

32 33 34 35 36
		if rootOptions.Version {
			fmt.Printf("Version: v%.2f.%d%s", app.CurrentVersion.Number,
				app.CurrentVersion.PatchLevel,
				app.CurrentVersion.Suffix)
		}
LinuxSuRen's avatar
LinuxSuRen 已提交
37 38 39 40 41 42 43 44 45 46
	},
}

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

47
var rootOptions RootOptions
LinuxSuRen's avatar
LinuxSuRen 已提交
48 49 50

func init() {
	cobra.OnInitialize(initConfig)
51
	rootCmd.PersistentFlags().BoolVarP(&rootOptions.Version, "version", "v", false, "Print the version of Jenkins CLI")
LinuxSuRen's avatar
LinuxSuRen 已提交
52 53 54
}

func initConfig() {
55 56 57 58 59 60 61 62
	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 已提交
63
}