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

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

LinuxSuRen's avatar
LinuxSuRen 已提交
9
	"github.com/AlecAivazis/survey/v2"
10
	"github.com/atotto/clipboard"
LinuxSuRen's avatar
LinuxSuRen 已提交
11 12 13 14
	"github.com/spf13/cobra"
	"gopkg.in/yaml.v2"
)

LinuxSuRen's avatar
LinuxSuRen 已提交
15
// ConfigGenerateOption is the config generate cmd option
16 17 18 19 20 21
type ConfigGenerateOption struct {
	Copy bool
}

var configGenerateOption ConfigGenerateOption

LinuxSuRen's avatar
LinuxSuRen 已提交
22 23
func init() {
	configCmd.AddCommand(configGenerateCmd)
24
	configGenerateCmd.Flags().BoolVarP(&configGenerateOption.Copy, "copy", "c", false, "Copy the output into clipboard")
LinuxSuRen's avatar
LinuxSuRen 已提交
25 26 27
}

var configGenerateCmd = &cobra.Command{
28 29 30 31
	Use:     "generate",
	Aliases: []string{"gen"},
	Short:   "Generate a sample config file for you",
	Long:    `Generate a sample config file for you`,
yJunS's avatar
yJunS 已提交
32
	Run: func(_ *cobra.Command, _ []string) {
LinuxSuRen's avatar
LinuxSuRen 已提交
33
		if data, err := generateSampleConfig(); err == nil {
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
			configPath := configOptions.ConfigFileLocation

			if configPath == "" { // config file isn't exists
				userHome := userHomeDir()
				configPath = fmt.Sprintf("%s/.jenkins-cli.yaml", userHome)
			}

			_, err := os.Stat(configPath)
			if err != nil && os.IsNotExist(err) {
				confirm := false
				prompt := &survey.Confirm{
					Message: "Cannot found your config file, do you want to edit it?",
				}
				survey.AskOne(prompt, &confirm)
				if confirm {
					prompt := &survey.Editor{
						Message:       "Edit your config file",
						FileName:      "*.yaml",
						Default:       string(data),
						HideDefault:   true,
						AppendDefault: true,
					}

					var configContext string
					if err = survey.AskOne(prompt, &configContext); err != nil {
						log.Fatal(err)
					} else {
						if err = ioutil.WriteFile(configPath, []byte(configContext), 0644); err != nil {
							log.Fatal(err)
						}
					}
					return
				}
			}

			printCfg(data)
70 71 72 73

			if configGenerateOption.Copy {
				clipboard.WriteAll(string(data))
			}
LinuxSuRen's avatar
LinuxSuRen 已提交
74 75 76 77 78 79
		} else {
			log.Fatal(err)
		}
	},
}

80 81
func printCfg(data []byte) {
	fmt.Print(string(data))
82
	fmt.Println("# Language context is accept-language for HTTP header, It contains zh-CN/zh-TW/en/en-US/ja and so on")
83 84 85
	fmt.Println("# Goto 'http://localhost:8080/jenkins/me/configure', then you can generate your token.")
}

86 87
func getSampleConfig() (sampleConfig Config) {
	sampleConfig = Config{
LinuxSuRen's avatar
LinuxSuRen 已提交
88 89 90 91 92 93 94 95 96
		Current: "yourServer",
		JenkinsServers: []JenkinsServer{
			{
				Name:     "yourServer",
				URL:      "http://localhost:8080/jenkins",
				UserName: "admin",
				Token:    "111e3a2f0231198855dceaff96f20540a9",
			},
		},
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
		Mirrors: []JenkinsMirror{
			{
				Name: "default",
				URL:  "http://mirrors.jenkins.io/",
			},
			{
				Name: "tsinghua",
				URL:  "https://mirrors.tuna.tsinghua.edu.cn/jenkins/",
			},
			{
				Name: "huawei",
				URL:  "https://mirrors.huaweicloud.com/jenkins/",
			},
			{
				Name: "tencent",
				URL:  "https://mirrors.cloud.tencent.com/jenkins/",
			},
		},
LinuxSuRen's avatar
LinuxSuRen 已提交
115
	}
116 117 118 119 120
	return
}

func generateSampleConfig() ([]byte, error) {
	sampleConfig := getSampleConfig()
LinuxSuRen's avatar
LinuxSuRen 已提交
121 122
	return yaml.Marshal(&sampleConfig)
}