From 9bb6febe715f77993fdff36572d8c2b471b94c86 Mon Sep 17 00:00:00 2001 From: Zhao Xiaojie Date: Tue, 2 Jul 2019 15:29:13 +0800 Subject: [PATCH] Add batchOption for job build --- app/cmd/common.go | 27 ++++++++++++++++++++++----- app/cmd/job_build.go | 13 +++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/app/cmd/common.go b/app/cmd/common.go index 0bedde9..167b936 100644 --- a/app/cmd/common.go +++ b/app/cmd/common.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" + "github.com/AlecAivazis/survey" "gopkg.in/yaml.v2" ) @@ -12,11 +13,6 @@ type OutputOption struct { Format string } -// BatchOption represent the options for a batch operation -type BatchOption struct { - Batch bool -} - type FormatOutput interface { Output(obj interface{}, format string) (data []byte, err error) } @@ -47,3 +43,24 @@ func Format(obj interface{}, format string) (data []byte, err error) { return nil, fmt.Errorf("not support format %s", format) } + +// 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 +} diff --git a/app/cmd/job_build.go b/app/cmd/job_build.go index 0fc6468..c8ae9af 100644 --- a/app/cmd/job_build.go +++ b/app/cmd/job_build.go @@ -1,12 +1,21 @@ package cmd import ( + "fmt" + "github.com/linuxsuren/jenkins-cli/client" "github.com/spf13/cobra" ) +type JobBuildOption struct { + BatchOption +} + +var jobBuildOption JobBuildOption + func init() { jobCmd.AddCommand(jobBuildCmd) + jobBuildCmd.Flags().BoolVarP(&jobBuildOption.Batch, "batch", "b", false, "Batch mode, no need confirm") } var jobBuildCmd = &cobra.Command{ @@ -19,6 +28,10 @@ var jobBuildCmd = &cobra.Command{ return } + if !jobBuildOption.Confirm(fmt.Sprintf("Are you sure to build job %s", jobOption.Name)) { + return + } + jenkins := getCurrentJenkins() jclient := &client.JobClient{} jclient.URL = jenkins.URL -- GitLab