提交 fd9f82fc 编写于 作者: LinuxSuRen's avatar LinuxSuRen

Add select sub-cmd to select one jenkins as current

上级 2a1c5fd1
package cmd
import (
"errors"
"fmt"
"io/ioutil"
"log"
......@@ -35,6 +36,12 @@ var configCmd = &cobra.Command{
Use: "config",
Short: "Manage the config of jcli",
Long: `Manage the config of jcli`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("requires at least one argument")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
current := getCurrentJenkins()
if configOptions.Show {
......@@ -63,24 +70,10 @@ var configCmd = &cobra.Command{
}
if configOptions.Current != "" {
found := false
for _, jenkins := range getConfig().JenkinsServers {
if jenkins.Name == configOptions.Current {
found = true
break
}
}
if found {
config.Current = configOptions.Current
if err := saveConfig(); err != nil {
log.Fatal(err)
}
} else {
log.Fatalf("Cannot found Jenkins by name %s", configOptions.Current)
}
setCurrentJenkins(configOptions.Current)
}
},
Example: "jcli config -l",
}
// JenkinsServer holds the configuration of your Jenkins
......@@ -98,6 +91,25 @@ type Config struct {
JenkinsServers []JenkinsServer `yaml:"jenkins_servers"`
}
func setCurrentJenkins(name string) {
found := false
for _, jenkins := range getConfig().JenkinsServers {
if jenkins.Name == name {
found = true
break
}
}
if found {
config.Current = name
if err := saveConfig(); err != nil {
log.Fatal(err)
}
} else {
log.Fatalf("Cannot found Jenkins by name %s", name)
}
}
func generateSampleConfig() ([]byte, error) {
sampleConfig := Config{
Current: "yourServer",
......@@ -119,6 +131,15 @@ func getConfig() Config {
return config
}
func getJenkinsNames() []string {
config := getConfig()
names := make([]string, 0)
for _, j := range config.JenkinsServers {
names = append(names, j.Name)
}
return names
}
func getCurrentJenkins() (jenkinsServer *JenkinsServer) {
config := getConfig()
current := config.Current
......
package cmd
import (
"github.com/AlecAivazis/survey"
"github.com/spf13/cobra"
)
func init() {
configCmd.AddCommand(configSelectCmd)
}
var configSelectCmd = &cobra.Command{
Use: "select",
Short: "Select one config as current Jenkins",
Long: `Select one config as current Jenkins`,
Run: func(cmd *cobra.Command, args []string) {
target := ""
if currentJenkins := getCurrentJenkins(); currentJenkins != nil {
target = currentJenkins.Name
}
prompt := &survey.Select{
Message: "Choose a Jenkins as the current one:",
Options: getJenkinsNames(),
Default: target,
}
survey.AskOne(prompt, &target)
if target != "" {
setCurrentJenkins(target)
}
},
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册