common.go 2.8 KB
Newer Older
LinuxSuRen's avatar
LinuxSuRen 已提交
1 2 3 4 5 6
package cmd

import (
	"encoding/json"
	"fmt"

LinuxSuRen's avatar
LinuxSuRen 已提交
7
	"github.com/AlecAivazis/survey/v2"
8
	"github.com/jenkins-zh/jenkins-cli/client"
LinuxSuRen's avatar
LinuxSuRen 已提交
9
	"github.com/spf13/cobra"
LinuxSuRen's avatar
LinuxSuRen 已提交
10 11 12
	"gopkg.in/yaml.v2"
)

LinuxSuRen's avatar
LinuxSuRen 已提交
13
// OutputOption represent the format of output
LinuxSuRen's avatar
LinuxSuRen 已提交
14 15 16 17
type OutputOption struct {
	Format string
}

18 19 20 21
type FormatOutput interface {
	Output(obj interface{}, format string) (data []byte, err error)
}

LinuxSuRen's avatar
LinuxSuRen 已提交
22
const (
23 24 25
	JsonOutputFormat  string = "json"
	YAMLOutputFormat  string = "yaml"
	TableOutputFormat string = "table"
LinuxSuRen's avatar
LinuxSuRen 已提交
26 27
)

28 29 30 31 32 33 34 35 36 37 38
func (o *OutputOption) Output(obj interface{}) (data []byte, err error) {
	switch o.Format {
	case JsonOutputFormat:
		return json.MarshalIndent(obj, "", "  ")
	case YAMLOutputFormat:
		return yaml.Marshal(obj)
	}

	return nil, fmt.Errorf("not support format %s", o.Format)
}

39
// SetFlag set flag of output format
40
func (o *OutputOption) SetFlag(cmd *cobra.Command) {
41
	cmd.Flags().StringVarP(&o.Format, "output", "o", "table", "Format the output, supported formats: table, json, yaml")
42 43
}

LinuxSuRen's avatar
LinuxSuRen 已提交
44 45 46 47 48 49 50 51 52
func Format(obj interface{}, format string) (data []byte, err error) {
	if format == JsonOutputFormat {
		return json.MarshalIndent(obj, "", "  ")
	} else if format == YAMLOutputFormat {
		return yaml.Marshal(obj)
	}

	return nil, fmt.Errorf("not support format %s", format)
}
LinuxSuRen's avatar
LinuxSuRen 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

// BatchOption represent the options for a batch operation
type BatchOption struct {
	Batch bool
}

// Confirm prompte user if they really want to do this
func (b *BatchOption) Confirm(message string) bool {
	if !b.Batch {
		confirm := false
		prompt := &survey.Confirm{
			Message: message,
		}
		survey.AskOne(prompt, &confirm)
		if !confirm {
			return false
		}
	}

	return true
}
74

LinuxSuRen's avatar
LinuxSuRen 已提交
75 76 77 78
func (b *BatchOption) SetFlag(cmd *cobra.Command) {
	cmd.Flags().BoolVarP(&b.Batch, "batch", "b", false, "Batch mode, no need confirm")
}

79 80
// WatchOption for the resources which can be watched
type WatchOption struct {
LinuxSuRen's avatar
LinuxSuRen 已提交
81 82
	Watch    bool
	Interval int
83 84 85 86 87 88
	Count    int
}

// SetFlag for WatchOption
func (o *WatchOption) SetFlag(cmd *cobra.Command) {
	cmd.Flags().IntVarP(&o.Interval, "interval", "i", 1, "Interval of watch")
89
	cmd.Flags().IntVarP(&o.Count, "count", "", 9999, "Count of watch")
90
}
91 92 93 94 95 96 97 98 99 100

// InteractiveOption allow user to choose whether the mode is interactive
type InteractiveOption struct {
	Interactive bool
}

// SetFlag set the option flag to this cmd
func (b *InteractiveOption) SetFlag(cmd *cobra.Command) {
	cmd.Flags().BoolVarP(&b.Interactive, "interactive", "i", false, "Interactive mode")
}
101

LinuxSuRen's avatar
LinuxSuRen 已提交
102 103
// HookOption is the option whether skip command hook
type HookOption struct {
104
	SkipPreHook  bool
LinuxSuRen's avatar
LinuxSuRen 已提交
105 106 107
	SkipPostHook bool
}

108 109 110 111 112 113 114 115 116
func getCurrentJenkinsAndClient(jclient *client.JenkinsCore) (jenkins *JenkinsServer) {
	jenkins = getCurrentJenkinsFromOptionsOrDie()
	jclient.URL = jenkins.URL
	jclient.UserName = jenkins.UserName
	jclient.Token = jenkins.Token
	jclient.Proxy = jenkins.Proxy
	jclient.ProxyAuth = jenkins.ProxyAuth
	return
}