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

import (
4
	"fmt"
5
	"github.com/jenkins-zh/jenkins-cli/app/cmd/common"
6
	"github.com/mitchellh/go-homedir"
7
	"go.uber.org/zap"
8
	"gopkg.in/yaml.v2"
9 10 11
	"io/ioutil"
	"os"

12
	"github.com/atotto/clipboard"
13
	. "github.com/jenkins-zh/jenkins-cli/app/config"
14
	"github.com/jenkins-zh/jenkins-cli/app/i18n"
LinuxSuRen's avatar
LinuxSuRen 已提交
15 16 17
	"github.com/spf13/cobra"
)

LinuxSuRen's avatar
LinuxSuRen 已提交
18
// ConfigGenerateOption is the config generate cmd option
19
type ConfigGenerateOption struct {
20 21 22
	common.InteractiveOption
	common.CommonOption
	common.BatchOption
23

24 25 26 27 28
	Copy bool
}

var configGenerateOption ConfigGenerateOption

LinuxSuRen's avatar
LinuxSuRen 已提交
29 30
func init() {
	configCmd.AddCommand(configGenerateCmd)
31 32 33 34
	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"))
35 36
	configGenerateOption.CommonOption.Stdio = common.GetSystemStdio()
	configGenerateOption.BatchOption.Stdio = common.GetSystemStdio()
LinuxSuRen's avatar
LinuxSuRen 已提交
37 38 39
}

var configGenerateCmd = &cobra.Command{
40 41
	Use:     "generate",
	Aliases: []string{"gen"},
42 43
	Short:   i18n.T("Generate a sample config file for you"),
	Long:    i18n.T("Generate a sample config file for you"),
44 45
	RunE: func(cmd *cobra.Command, _ []string) (err error) {
		var data []byte
LinuxSuRen's avatar
LinuxSuRen 已提交
46
		data, err = GenerateSampleConfig()
47 48
		if err == nil {
			if configGenerateOption.Interactive {
49
				err = configGenerateOption.InteractiveWithConfig(cmd, data)
50 51 52
			} else {
				printCfg(cmd, data)
			}
53

54 55
			if configGenerateOption.Copy {
				err = clipboard.WriteAll(string(data))
56
			}
57
		}
58
		return
59 60
	},
}
61

62
// InteractiveWithConfig be friendly for a newer
63
func (o *ConfigGenerateOption) InteractiveWithConfig(cmd *cobra.Command, data []byte) (err error) {
64
	configPath := configOptions.ConfigFileLocation
65 66 67 68 69 70 71 72
	if configPath == "" {
		configPath, err = getDefaultConfigPath()
	}

	if err == nil {
		_, err = os.Stat(configPath)
	}

73
	if err != nil && os.IsNotExist(err) {
74 75 76 77 78
		confirm := o.Confirm("Cannot found your config file, do you want to edit it?")
		if confirm {
			var content string
			content, err = o.Editor(string(data), "Edit your config file")
			if err == nil {
79
				logger.Debug("write generated config file", zap.String("path", configPath))
80
				err = ioutil.WriteFile(configPath, []byte(content), 0644)
81
			}
LinuxSuRen's avatar
LinuxSuRen 已提交
82
		}
83 84
	}
	return
LinuxSuRen's avatar
LinuxSuRen 已提交
85 86
}

87 88 89 90
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.")
91 92
}

93 94
func getSampleConfig() (sampleConfig Config) {
	sampleConfig = Config{
LinuxSuRen's avatar
LinuxSuRen 已提交
95 96 97
		Current: "yourServer",
		JenkinsServers: []JenkinsServer{
			{
98 99 100 101 102
				Name:               "yourServer",
				URL:                "http://localhost:8080/jenkins",
				UserName:           "admin",
				Token:              "111e3a2f0231198855dceaff96f20540a9",
				InsecureSkipVerify: true,
LinuxSuRen's avatar
LinuxSuRen 已提交
103 104
			},
		},
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
	return
}

LinuxSuRen's avatar
LinuxSuRen 已提交
127 128
// GenerateSampleConfig returns a sample config
func GenerateSampleConfig() ([]byte, error) {
129
	sampleConfig := getSampleConfig()
LinuxSuRen's avatar
LinuxSuRen 已提交
130 131
	return yaml.Marshal(&sampleConfig)
}
132 133 134 135 136 137 138 139 140

// GetConfigFromHome returns the config file path from user home dir
func GetConfigFromHome() (configPath string, homeErr error) {
	userHome, homeErr := homedir.Dir()
	if homeErr == nil {
		configPath = fmt.Sprintf("%s/.jenkins-cli.yaml", userHome)
	}
	return
}