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

import (
4 5 6
	"io/ioutil"
	"os"

LinuxSuRen's avatar
LinuxSuRen 已提交
7
	"github.com/AlecAivazis/survey/v2"
8
	"github.com/atotto/clipboard"
9
	"github.com/jenkins-zh/jenkins-cli/app/helper"
10
	"github.com/jenkins-zh/jenkins-cli/app/i18n"
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
type ConfigGenerateOption struct {
17
	InteractiveOption
18 19 20 21 22
	Copy bool
}

var configGenerateOption ConfigGenerateOption

LinuxSuRen's avatar
LinuxSuRen 已提交
23 24
func init() {
	configCmd.AddCommand(configGenerateCmd)
25 26 27 28
	configGenerateCmd.Flags().BoolVarP(&configGenerateOption.Interactive, "interactive", "i", true,
		i18n.T("Interactive mode"))
	configGenerateCmd.Flags().BoolVarP(&configGenerateOption.Copy, "copy", "c", false,
		i18n.T("Copy the output into clipboard"))
LinuxSuRen's avatar
LinuxSuRen 已提交
29 30 31
}

var configGenerateCmd = &cobra.Command{
32 33
	Use:     "generate",
	Aliases: []string{"gen"},
34 35
	Short:   i18n.T("Generate a sample config file for you"),
	Long:    i18n.T("Generate a sample config file for you"),
36 37 38 39 40 41 42 43
	Run: func(cmd *cobra.Command, _ []string) {
		data, err := generateSampleConfig()
		if err == nil {
			if configGenerateOption.Interactive {
				err = InteractiveWithConfig(cmd, data)
			} else {
				printCfg(cmd, data)
			}
44

45 46
			if configGenerateOption.Copy {
				err = clipboard.WriteAll(string(data))
47
			}
48 49 50 51
		}
		helper.CheckErr(cmd, err)
	},
}
52

53 54 55
// InteractiveWithConfig be friendly for a newer
func InteractiveWithConfig(cmd *cobra.Command, data []byte) (err error) {
	configPath := configOptions.ConfigFileLocation
56

57 58 59 60 61
	if configPath == "" { // config file isn't exists
		if configPath, err = GetConfigFromHome(); err != nil {
			return
		}
	}
62

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	_, 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?",
		}
		err = survey.AskOne(prompt, &confirm)
		if err == nil && confirm {
			prompt := &survey.Editor{
				Message:       "Edit your config file",
				FileName:      "*.yaml",
				Default:       string(data),
				HideDefault:   true,
				AppendDefault: true,
			}
78

79 80 81
			var configContext string
			if err = survey.AskOne(prompt, &configContext); err == nil {
				err = ioutil.WriteFile(configPath, []byte(configContext), 0644)
82
			}
LinuxSuRen's avatar
LinuxSuRen 已提交
83
		}
84 85
	}
	return
LinuxSuRen's avatar
LinuxSuRen 已提交
86 87
}

88 89 90 91
func printCfg(cmd *cobra.Command, data []byte) {
	cmd.Print(string(data))
	cmd.Println("# Language context is accept-language for HTTP header, It contains zh-CN/zh-TW/en/en-US/ja and so on")
	cmd.Println("# Goto 'http://localhost:8080/jenkins/me/configure', then you can generate your token.")
92 93
}

94 95
func getSampleConfig() (sampleConfig Config) {
	sampleConfig = Config{
LinuxSuRen's avatar
LinuxSuRen 已提交
96 97 98 99 100 101 102 103 104
		Current: "yourServer",
		JenkinsServers: []JenkinsServer{
			{
				Name:     "yourServer",
				URL:      "http://localhost:8080/jenkins",
				UserName: "admin",
				Token:    "111e3a2f0231198855dceaff96f20540a9",
			},
		},
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
		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 已提交
123
	}
124 125 126 127 128
	return
}

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