From db37cceefe3882d855c3fcaac775d2747765f966 Mon Sep 17 00:00:00 2001 From: Zhao Xiaojie Date: Tue, 5 May 2020 16:04:39 +0800 Subject: [PATCH] Add shutdown Jenkins support (#346) * Add shutdown Jenkins support * Fix the packge missing issue * Using sonarqube to analzy the code lines * Rename the sonarqube actions * Remove travis * Fix the duplicates * Add unit test cases for shutdown client api * Add e2e tests for shutdown command * Log the duration of command executing * Exclusions setting for bindata.go * Fix the code lines duplicates * Add test cases for shutdown cmd * Adjust the test cases --- .github/workflows/sonarqube.yaml | 21 +++++++ .travis.yml | 30 --------- Makefile | 23 +++---- app/cmd/casc.go | 2 +- app/cmd/casc_apply.go | 2 +- app/cmd/casc_apply_test.go | 2 +- app/cmd/casc_export.go | 2 +- app/cmd/casc_export_test.go | 2 +- app/cmd/casc_open.go | 2 +- app/cmd/casc_open_test.go | 2 +- app/cmd/casc_reload.go | 2 +- app/cmd/casc_reload_test.go | 2 +- app/cmd/casc_schema.go | 2 +- app/cmd/casc_schema_test.go | 2 +- app/cmd/center_download_test.go | 2 +- app/cmd/center_identity_test.go | 2 +- app/cmd/center_mirror_test.go | 4 +- app/cmd/center_start_test.go | 2 +- app/cmd/center_test.go | 2 +- app/cmd/center_upgrade_test.go | 2 +- app/cmd/center_watch_test.go | 2 +- app/cmd/{ => common}/common_test.go | 15 +++-- app/cmd/common/setup_test.go | 16 +++++ app/cmd/common/version_since.go | 8 +++ app/cmd/completion_test.go | 2 +- app/cmd/computer_create.go | 2 +- app/cmd/computer_create_test.go | 2 +- app/cmd/computer_delete.go | 2 +- app/cmd/computer_delete_test.go | 2 +- app/cmd/computer_launch_test.go | 2 +- app/cmd/computer_list_test.go | 2 +- app/cmd/computer_log_test.go | 2 +- app/cmd/config_add_test.go | 2 +- app/cmd/config_edit_test.go | 2 +- app/cmd/config_generate.go | 5 +- app/cmd/config_list_test.go | 2 +- app/cmd/config_plugin.go | 8 +-- app/cmd/config_plugin/root.go | 8 +-- app/cmd/config_plugin/uninstall_plugin.go | 2 +- app/cmd/config_select_test.go | 2 +- app/cmd/config_test.go | 2 +- app/cmd/credential.go | 2 +- app/cmd/credential_create.go | 2 +- app/cmd/credential_create_test.go | 2 +- app/cmd/credential_delete.go | 2 +- app/cmd/credential_delete_test.go | 6 +- app/cmd/credential_list.go | 2 +- app/cmd/credential_list_test.go | 2 +- app/cmd/crumbissuer_test.go | 2 +- app/cmd/doc_test.go | 2 +- app/cmd/job_artifact_download_test.go | 6 +- app/cmd/job_artifact_test.go | 6 +- app/cmd/job_build_test.go | 6 +- app/cmd/job_create_test.go | 2 +- app/cmd/job_delete_test.go | 4 +- app/cmd/job_disable_test.go | 2 +- app/cmd/job_edit_test.go | 6 +- app/cmd/job_enable_test.go | 2 +- app/cmd/job_history_test.go | 2 +- app/cmd/job_input_test.go | 6 +- app/cmd/job_log_test.go | 2 +- app/cmd/job_param_test.go | 6 +- app/cmd/job_search_test.go | 4 +- app/cmd/job_stop_test.go | 4 +- app/cmd/job_type_test.go | 8 +-- app/cmd/open_test.go | 4 +- app/cmd/plugin_checkout_test.go | 2 +- app/cmd/plugin_create_test.go | 2 +- app/cmd/plugin_download_test.go | 5 +- app/cmd/plugin_install_test.go | 4 +- app/cmd/plugin_list_test.go | 12 ++-- app/cmd/plugin_open_test.go | 2 +- app/cmd/plugin_release.go | 2 +- app/cmd/plugin_release_test.go | 2 +- app/cmd/plugin_search_test.go | 8 +-- app/cmd/plugin_trend_test.go | 2 +- app/cmd/plugin_uninstall_test.go | 4 +- app/cmd/plugin_upgrade_test.go | 6 +- app/cmd/plugin_upload_test.go | 2 +- app/cmd/queue_cancel_test.go | 4 +- app/cmd/queue_list_test.go | 4 +- app/cmd/queue_test.go | 2 +- app/cmd/restart_test.go | 2 +- app/cmd/root.go | 31 ++++++++- app/cmd/root_test.go | 2 +- app/cmd/shell_test.go | 2 +- app/cmd/shutdown.go | 77 +++++++++++++++++++++++ app/cmd/shutdown_test.go | 61 ++++++++++++++++++ app/cmd/user_create_test.go | 4 +- app/cmd/user_delete_test.go | 4 +- app/cmd/user_edit_test.go | 2 +- app/cmd/user_test.go | 4 +- app/cmd/user_token_test.go | 2 +- app/cmd/version_test.go | 2 +- client/core.go | 20 ++++++ client/core_test.go | 60 ++++++++++++++++++ client/core_test_common.go | 24 +++++++ client/pluginApi_test.go | 6 +- e2e/shutdown_test.go | 14 +++++ sonar-project.properties | 5 +- 100 files changed, 485 insertions(+), 192 deletions(-) create mode 100644 .github/workflows/sonarqube.yaml delete mode 100644 .travis.yml rename app/cmd/{ => common}/common_test.go (89%) create mode 100644 app/cmd/common/setup_test.go create mode 100644 app/cmd/common/version_since.go create mode 100644 app/cmd/shutdown.go create mode 100644 app/cmd/shutdown_test.go create mode 100644 e2e/shutdown_test.go diff --git a/.github/workflows/sonarqube.yaml b/.github/workflows/sonarqube.yaml new file mode 100644 index 0000000..9978c47 --- /dev/null +++ b/.github/workflows/sonarqube.yaml @@ -0,0 +1,21 @@ +on: + push: + branches: + - master + pull_request: + types: [opened, synchronize, reopened] +name: Sonarqube +jobs: + sonarcloud: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Test + run: | + export PATH=$PATH:${PWD}/bin:$GOPATH/bin:/home/runner/go/bin + make test + - name: SonarCloud Scan + uses: sonarsource/sonarcloud-github-action@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: 3caad1285eb0edf2b4f65ee07b3cd8edde6c5176 diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index ff717ca..0000000 --- a/.travis.yml +++ /dev/null @@ -1,30 +0,0 @@ -language: go -dist: trusty - -go: -- 1.12.x - -env: - global: - - GO111MODULE=on - -addons: - sonarcloud: - organization: "jenkins-zh" - token: - secure: 3caad1285eb0edf2b4f65ee07b3cd8edde6c5176 # encrypted value of your token - -script: - # Execute some tests - - export PATH=${PWD}/bin:$PATH - - make clean gen-data-linux tools init build-all test - # And finally run the SonarQube analysis - read the "sonar-project.properties" - # file to see the specific configuration - - curl -LsS https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.0.0.1744-linux.zip > sonar-scanner-cli-4.0.0.1744-linux.zip - - unzip sonar-scanner-cli-4.0.0.1744-linux.zip - - export branch=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then echo $TRAVIS_BRANCH; else echo pr-$TRAVIS_PULL_REQUEST; fi) - - branch=$(echo $branch | sed 's/\//-/g') - - sonar-scanner-4.0.0.1744-linux/bin/sonar-scanner -Dsonar.branch.name=$branch -Dsonar.projectKey=jenkins-zh_jenkins-cli -Dsonar.organization=jenkins-zh -Dsonar.sources=. -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=674e187e300edc0ad56a05705bd0b21cbe18bd52 - # it's bad, but no better solution for now -# - sleep 30 -# - if [ 'OK' != $(curl -s "https://sonarcloud.io/api/qualitygates/project_status?branch=$branch&projectKey=jenkins-zh_jenkins-cli" | python -c "import sys, json; print(json.load(sys.stdin)['projectStatus']['status'])") ]; then exit -1; fi diff --git a/Makefile b/Makefile index 38aad12..696e1f2 100644 --- a/Makefile +++ b/Makefile @@ -116,13 +116,15 @@ test-slow: test: mkdir -p bin - go test ./util -v -count=1 - go test ./client -v -count=1 -coverprofile coverage.out - go test ./app -v -count=1 - go test ./app/health -v -count=1 - go test ./app/helper -v -count=1 - go test ./app/i18n -v -count=1 + go test ./util ./client ./app/ ./app/health ./app/helper ./app/i18n ./app/cmd/common -v -count=1 -coverprofile coverage.out go test ./app/cmd -v -count=1 +# go test ./util -v -count=1 +# go test ./client -v -count=1 -coverprofile coverage.out +# go test ./app -v -count=1 +# go test ./app/health -v -count=1 +# go test ./app/helper -v -count=1 +# go test ./app/i18n -v -count=1 +# go test ./app/cmd -v -count=1 dep: go get github.com/AlecAivazis/survey/v2 @@ -141,12 +143,3 @@ gen-data: image: docker build . -t jenkinszh/jcli - -image-win: - docker build . -t jenkinszh/jcli:win -f Dockerfile-win - -image-darwin: - docker build . -t jenkinszh/jcli:darwin -f Dockerfile-darwin - -image-dev: - docker build . -t jenkinszh/jcli:dev -f Docker-dev diff --git a/app/cmd/casc.go b/app/cmd/casc.go index 90cd762..6726e69 100644 --- a/app/cmd/casc.go +++ b/app/cmd/casc.go @@ -33,6 +33,6 @@ var cascCmd = &cobra.Command{ Short: i18n.T("Configuration as Code"), Long: i18n.T("Configuration as Code"), Annotations: map[string]string{ - common.Since: "v0.0.24", + common.Since: common.VersionSince0024, }, } diff --git a/app/cmd/casc_apply.go b/app/cmd/casc_apply.go index a4bc0c9..d2788e1 100644 --- a/app/cmd/casc_apply.go +++ b/app/cmd/casc_apply.go @@ -36,6 +36,6 @@ var cascApplyCmd = &cobra.Command{ return jClient.Apply() }, Annotations: map[string]string{ - common.Since: "v0.0.24", + common.Since: common.VersionSince0024, }, } diff --git a/app/cmd/casc_apply_test.go b/app/cmd/casc_apply_test.go index c73867a..dbe36fc 100644 --- a/app/cmd/casc_apply_test.go +++ b/app/cmd/casc_apply_test.go @@ -42,7 +42,7 @@ var _ = Describe("casc apply command", func() { Context("basic cases", func() { It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/casc_export.go b/app/cmd/casc_export.go index 6c0fbd8..195cace 100644 --- a/app/cmd/casc_export.go +++ b/app/cmd/casc_export.go @@ -41,6 +41,6 @@ var cascExportCmd = &cobra.Command{ return }, Annotations: map[string]string{ - common.Since: "v0.0.24", + common.Since: common.VersionSince0024, }, } diff --git a/app/cmd/casc_export_test.go b/app/cmd/casc_export_test.go index ffe07bb..8a3cfc4 100644 --- a/app/cmd/casc_export_test.go +++ b/app/cmd/casc_export_test.go @@ -42,7 +42,7 @@ var _ = Describe("casc apply command", func() { Context("basic cases", func() { It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/casc_open.go b/app/cmd/casc_open.go index 8bfb0c8..7ed37e2 100644 --- a/app/cmd/casc_open.go +++ b/app/cmd/casc_open.go @@ -47,6 +47,6 @@ var cascOpenCmd = &cobra.Command{ return }, Annotations: map[string]string{ - common.Since: "v0.0.24", + common.Since: common.VersionSince0024, }, } diff --git a/app/cmd/casc_open_test.go b/app/cmd/casc_open_test.go index 64dd5e5..811e4e4 100644 --- a/app/cmd/casc_open_test.go +++ b/app/cmd/casc_open_test.go @@ -16,7 +16,7 @@ var _ = Describe("casc open test", func() { BeforeEach(func() { cascOpenOption.ExecContext = util.FakeExecCommandSuccess - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) tempFile, err = ioutil.TempFile(".", "test.yaml") diff --git a/app/cmd/casc_reload.go b/app/cmd/casc_reload.go index 5e25f6d..a7765a8 100644 --- a/app/cmd/casc_reload.go +++ b/app/cmd/casc_reload.go @@ -36,6 +36,6 @@ var cascReloadCmd = &cobra.Command{ return jClient.Reload() }, Annotations: map[string]string{ - common.Since: "v0.0.24", + common.Since: common.VersionSince0024, }, } diff --git a/app/cmd/casc_reload_test.go b/app/cmd/casc_reload_test.go index ff6463f..c3bf7e5 100644 --- a/app/cmd/casc_reload_test.go +++ b/app/cmd/casc_reload_test.go @@ -42,7 +42,7 @@ var _ = Describe("casc reload command", func() { Context("basic cases", func() { It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/casc_schema.go b/app/cmd/casc_schema.go index 2bd1a44..4698f62 100644 --- a/app/cmd/casc_schema.go +++ b/app/cmd/casc_schema.go @@ -41,6 +41,6 @@ var cascSchemaCmd = &cobra.Command{ return }, Annotations: map[string]string{ - common.Since: "v0.0.24", + common.Since: common.VersionSince0024, }, } diff --git a/app/cmd/casc_schema_test.go b/app/cmd/casc_schema_test.go index 4b0554f..fdeea09 100644 --- a/app/cmd/casc_schema_test.go +++ b/app/cmd/casc_schema_test.go @@ -42,7 +42,7 @@ var _ = Describe("casc apply command", func() { Context("basic cases", func() { It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/center_download_test.go b/app/cmd/center_download_test.go index 5740926..0536b2e 100644 --- a/app/cmd/center_download_test.go +++ b/app/cmd/center_download_test.go @@ -49,7 +49,7 @@ var _ = Describe("center download command", func() { Context("basic cases", func() { BeforeEach(func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/center_identity_test.go b/app/cmd/center_identity_test.go index e8f2f80..e58ffa4 100644 --- a/app/cmd/center_identity_test.go +++ b/app/cmd/center_identity_test.go @@ -40,7 +40,7 @@ var _ = Describe("center identity command", func() { Context("basic cases", func() { It("should not error", func() { var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/center_mirror_test.go b/app/cmd/center_mirror_test.go index 5c317d4..afcbf13 100644 --- a/app/cmd/center_mirror_test.go +++ b/app/cmd/center_mirror_test.go @@ -42,7 +42,7 @@ var _ = Describe("center mirror command", func() { Context("basic cases", func() { It("enable mirror site", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -57,7 +57,7 @@ var _ = Describe("center mirror command", func() { }) It("disable mirror site", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/center_start_test.go b/app/cmd/center_start_test.go index 9d269a8..5493b6b 100644 --- a/app/cmd/center_start_test.go +++ b/app/cmd/center_start_test.go @@ -17,7 +17,7 @@ var _ = Describe("center start command", func() { Expect(err).NotTo(HaveOccurred()) configFile = file.Name() - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(configFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/center_test.go b/app/cmd/center_test.go index f6fa19f..2c2a038 100644 --- a/app/cmd/center_test.go +++ b/app/cmd/center_test.go @@ -39,7 +39,7 @@ var _ = Describe("center command", func() { Context("basic cases", func() { It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/center_upgrade_test.go b/app/cmd/center_upgrade_test.go index 6ca3261..c1af8cb 100644 --- a/app/cmd/center_upgrade_test.go +++ b/app/cmd/center_upgrade_test.go @@ -38,7 +38,7 @@ var _ = Describe("center upgrade command", func() { Context("basic cases", func() { It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/center_watch_test.go b/app/cmd/center_watch_test.go index 12f918a..c15317e 100644 --- a/app/cmd/center_watch_test.go +++ b/app/cmd/center_watch_test.go @@ -41,7 +41,7 @@ var _ = Describe("center watch command", func() { Context("basic cases", func() { It("should success, center watch command", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/common_test.go b/app/cmd/common/common_test.go similarity index 89% rename from app/cmd/common_test.go rename to app/cmd/common/common_test.go index 13dd634..6742f1b 100644 --- a/app/cmd/common_test.go +++ b/app/cmd/common/common_test.go @@ -1,8 +1,7 @@ -package cmd_test +package common import ( "bytes" - "github.com/jenkins-zh/jenkins-cli/app/cmd/common" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "reflect" @@ -10,12 +9,12 @@ import ( var _ = Describe("test OutputOption", func() { var ( - outputOption common.OutputOption + outputOption OutputOption fakeFoos []FakeFoo ) BeforeEach(func() { - outputOption = common.OutputOption{} + outputOption = OutputOption{} fakeFoos = []FakeFoo{{ Name: "fake", @@ -39,7 +38,7 @@ var _ = Describe("test OutputOption", func() { Context("with filter", func() { BeforeEach(func() { - outputOption = common.OutputOption{ + outputOption = OutputOption{ Filter: []string{"Name=fake"}, } }) @@ -95,7 +94,7 @@ foo-1 Context("with json format", func() { BeforeEach(func() { - outputOption.Format = common.JSONOutputFormat + outputOption.Format = JSONOutputFormat }) It("should get a json text", func() { @@ -112,7 +111,7 @@ foo-1 Context("with yaml format", func() { BeforeEach(func() { - outputOption.Format = common.YAMLOutputFormat + outputOption.Format = YAMLOutputFormat }) It("should get a yaml text", func() { @@ -150,7 +149,7 @@ foo-1 Context("ignore invalid filter", func() { BeforeEach(func() { - outputOption = common.OutputOption{ + outputOption = OutputOption{ Filter: []string{"Name"}, } }) diff --git a/app/cmd/common/setup_test.go b/app/cmd/common/setup_test.go new file mode 100644 index 0000000..7cea338 --- /dev/null +++ b/app/cmd/common/setup_test.go @@ -0,0 +1,16 @@ +package common + +import ( + "testing" + + "github.com/onsi/ginkgo/reporters" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestCmd(t *testing.T) { + RegisterFailHandler(Fail) + junitReporter := reporters.NewJUnitReporter("test-app-cmd-common.xml") + RunSpecsWithDefaultAndCustomReporters(t, "app/cmd/common", []Reporter{junitReporter}) +} diff --git a/app/cmd/common/version_since.go b/app/cmd/common/version_since.go new file mode 100644 index 0000000..5aa176b --- /dev/null +++ b/app/cmd/common/version_since.go @@ -0,0 +1,8 @@ +package common + +const ( + // VersionSince0028 represents v0.0.28 + VersionSince0028 = "v0.0.28" + // VersionSince0024 represents v0.0.24 + VersionSince0024 = "v0.0.24" +) diff --git a/app/cmd/completion_test.go b/app/cmd/completion_test.go index 5451110..79df729 100644 --- a/app/cmd/completion_test.go +++ b/app/cmd/completion_test.go @@ -25,7 +25,7 @@ var _ = Describe("completion command", func() { rootOptions.ConfigFile = "test.yaml" var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/computer_create.go b/app/cmd/computer_create.go index 4a9e3d7..4d5c992 100644 --- a/app/cmd/computer_create.go +++ b/app/cmd/computer_create.go @@ -31,6 +31,6 @@ It can only create a JNLP agent.`), return jClient.Create(args[0]) }, Annotations: map[string]string{ - common.Since: "v0.0.24", + common.Since: common.VersionSince0024, }, } diff --git a/app/cmd/computer_create_test.go b/app/cmd/computer_create_test.go index 72da4f1..0c0aea3 100644 --- a/app/cmd/computer_create_test.go +++ b/app/cmd/computer_create_test.go @@ -48,7 +48,7 @@ var _ = Describe("create list command", func() { BeforeEach(func() { var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/computer_delete.go b/app/cmd/computer_delete.go index fee40d2..87357ab 100644 --- a/app/cmd/computer_delete.go +++ b/app/cmd/computer_delete.go @@ -30,6 +30,6 @@ var computerDeleteCmd = &cobra.Command{ return jClient.Delete(args[0]) }, Annotations: map[string]string{ - common.Since: "v0.0.24", + common.Since: common.VersionSince0024, }, } diff --git a/app/cmd/computer_delete_test.go b/app/cmd/computer_delete_test.go index d57ffbc..04a047f 100644 --- a/app/cmd/computer_delete_test.go +++ b/app/cmd/computer_delete_test.go @@ -48,7 +48,7 @@ var _ = Describe("create delete command", func() { BeforeEach(func() { var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/computer_launch_test.go b/app/cmd/computer_launch_test.go index 4ebcd06..8986fc7 100644 --- a/app/cmd/computer_launch_test.go +++ b/app/cmd/computer_launch_test.go @@ -39,7 +39,7 @@ var _ = Describe("computer launch command", func() { name = "fake" var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/computer_list_test.go b/app/cmd/computer_list_test.go index 6340ad9..e3ae3b4 100644 --- a/app/cmd/computer_list_test.go +++ b/app/cmd/computer_list_test.go @@ -47,7 +47,7 @@ var _ = Describe("computer list command", func() { BeforeEach(func() { var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/computer_log_test.go b/app/cmd/computer_log_test.go index 123bd5b..9fb4ca1 100644 --- a/app/cmd/computer_log_test.go +++ b/app/cmd/computer_log_test.go @@ -47,7 +47,7 @@ var _ = Describe("computer log command", func() { BeforeEach(func() { var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/config_add_test.go b/app/cmd/config_add_test.go index 5954b62..d311f50 100644 --- a/app/cmd/config_add_test.go +++ b/app/cmd/config_add_test.go @@ -30,7 +30,7 @@ var _ = Describe("config add command", func() { configPath = path.Join(os.TempDir(), "fake.yaml") var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(configPath, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/config_edit_test.go b/app/cmd/config_edit_test.go index 6a9c00e..8f14b41 100644 --- a/app/cmd/config_edit_test.go +++ b/app/cmd/config_edit_test.go @@ -12,7 +12,7 @@ package cmd // }, // Test: func(stdio terminal.Stdio) (err error) { // rootOptions.ConfigFile = "test.yaml" -// data, err := generateSampleConfig() +// data, err := GenerateSampleConfig() // err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) // // rootCmd.SetArgs([]string{"config", "edit"}) diff --git a/app/cmd/config_generate.go b/app/cmd/config_generate.go index 8be4751..3045894 100644 --- a/app/cmd/config_generate.go +++ b/app/cmd/config_generate.go @@ -43,7 +43,7 @@ var configGenerateCmd = &cobra.Command{ Long: i18n.T("Generate a sample config file for you"), RunE: func(cmd *cobra.Command, _ []string) (err error) { var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() if err == nil { if configGenerateOption.Interactive { err = configGenerateOption.InteractiveWithConfig(cmd, data) @@ -124,7 +124,8 @@ func getSampleConfig() (sampleConfig Config) { return } -func generateSampleConfig() ([]byte, error) { +// GenerateSampleConfig returns a sample config +func GenerateSampleConfig() ([]byte, error) { sampleConfig := getSampleConfig() return yaml.Marshal(&sampleConfig) } diff --git a/app/cmd/config_list_test.go b/app/cmd/config_list_test.go index 3aba7ab..18bb555 100644 --- a/app/cmd/config_list_test.go +++ b/app/cmd/config_list_test.go @@ -25,7 +25,7 @@ var _ = Describe("config list command", func() { rootOptions.ConfigFile = "test.yaml" var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/config_plugin.go b/app/cmd/config_plugin.go index 6754e31..6898a10 100644 --- a/app/cmd/config_plugin.go +++ b/app/cmd/config_plugin.go @@ -46,7 +46,7 @@ var configPluginCmd = &cobra.Command{ If you want to submit a plugin for jcli, please see also the following project. https://github.com/jenkins-zh/jcli-plugins`), Annotations: map[string]string{ - common.Since: "v0.0.28", + common.Since: common.VersionSince0028, }, } @@ -66,7 +66,7 @@ func NewConfigPluginListCmd() (cmd *cobra.Command) { Long: "list all installed plugins", RunE: configPluginListCmd.RunE, Annotations: map[string]string{ - common.Since: "v0.0.28", + common.Since: common.VersionSince0028, }, } @@ -93,7 +93,7 @@ The official metadata git repository is https://github.com/jenkins-zh/jcli-plugi but you can change it by giving a command parameter.`, RunE: pluginFetchCmd.Run, Annotations: map[string]string{ - common.Since: "v0.0.28", + common.Since: common.VersionSince0028, }, } @@ -125,7 +125,7 @@ func NewConfigPluginInstallCmd() (cmd *cobra.Command) { Args: cobra.MinimumNArgs(1), RunE: pluginInstallCmd.Run, Annotations: map[string]string{ - common.Since: "v0.0.28", + common.Since: common.VersionSince0028, }, } diff --git a/app/cmd/config_plugin/root.go b/app/cmd/config_plugin/root.go index 042bc9c..e6691f6 100644 --- a/app/cmd/config_plugin/root.go +++ b/app/cmd/config_plugin/root.go @@ -39,7 +39,7 @@ func NewConfigPluginCmd(opt *common.CommonOption) (cmd *cobra.Command) { If you want to submit a plugin for jcli, please see also the following project. https://github.com/jenkins-zh/jcli-plugins`), Annotations: map[string]string{ - common.Since: "v0.0.28", + common.Since: common.VersionSince0028, }, } @@ -62,7 +62,7 @@ func NewConfigPluginListCmd(opt *common.CommonOption) (cmd *cobra.Command) { Long: "list all installed plugins", RunE: configPluginListCmd.RunE, Annotations: map[string]string{ - common.Since: "v0.0.28", + common.Since: common.VersionSince0028, }, } @@ -91,7 +91,7 @@ The official metadata git repository is https://github.com/jenkins-zh/jcli-plugi but you can change it by giving a command parameter.`, RunE: pluginFetchCmd.Run, Annotations: map[string]string{ - common.Since: "v0.0.28", + common.Since: common.VersionSince0028, }, } @@ -125,7 +125,7 @@ func NewConfigPluginInstallCmd(opt *common.CommonOption) (cmd *cobra.Command) { Args: cobra.MinimumNArgs(1), RunE: pluginInstallCmd.Run, Annotations: map[string]string{ - common.Since: "v0.0.28", + common.Since: common.VersionSince0028, }, } diff --git a/app/cmd/config_plugin/uninstall_plugin.go b/app/cmd/config_plugin/uninstall_plugin.go index 14a1ded..7773e96 100644 --- a/app/cmd/config_plugin/uninstall_plugin.go +++ b/app/cmd/config_plugin/uninstall_plugin.go @@ -23,7 +23,7 @@ func NewConfigPluginUninstallCmd(opt *common.CommonOption) (cmd *cobra.Command) Args: cobra.MinimumNArgs(1), RunE: jcliPluginUninstallCmd.RunE, Annotations: map[string]string{ - common.Since: "v0.0.28", + common.Since: common.VersionSince0028, }, } return diff --git a/app/cmd/config_select_test.go b/app/cmd/config_select_test.go index 07c7b75..4a46d85 100644 --- a/app/cmd/config_select_test.go +++ b/app/cmd/config_select_test.go @@ -74,7 +74,7 @@ var _ = Describe("config select command", func() { // defer os.Remove(configFile) // // var data []byte -// data, err = generateSampleConfig() +// data, err = GenerateSampleConfig() // err = ioutil.WriteFile(configFile, data, 0664) // // configSelectOptions.CommonOption.Stdio = stdio diff --git a/app/cmd/config_test.go b/app/cmd/config_test.go index de575da..ecefa64 100644 --- a/app/cmd/config_test.go +++ b/app/cmd/config_test.go @@ -84,7 +84,7 @@ var _ = Describe("Table util test", func() { rootOptions.Jenkins = "" rootOptions.ConfigFile = "test.yaml" - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/credential.go b/app/cmd/credential.go index 0336c29..d769029 100644 --- a/app/cmd/credential.go +++ b/app/cmd/credential.go @@ -16,6 +16,6 @@ var credentialCmd = &cobra.Command{ Short: i18n.T("Manage the credentials of your Jenkins"), Long: i18n.T(`Manage the credentials of your Jenkins`), Annotations: map[string]string{ - common.Since: "v0.0.24", + common.Since: common.VersionSince0024, }, } diff --git a/app/cmd/credential_create.go b/app/cmd/credential_create.go index 56376c6..7e031d1 100644 --- a/app/cmd/credential_create.go +++ b/app/cmd/credential_create.go @@ -98,6 +98,6 @@ var credentialCreateCmd = &cobra.Command{ return }, Annotations: map[string]string{ - common.Since: "v0.0.24", + common.Since: common.VersionSince0024, }, } diff --git a/app/cmd/credential_create_test.go b/app/cmd/credential_create_test.go index c3c5762..437331e 100644 --- a/app/cmd/credential_create_test.go +++ b/app/cmd/credential_create_test.go @@ -51,7 +51,7 @@ var _ = Describe("credential create command", func() { BeforeEach(func() { var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/credential_delete.go b/app/cmd/credential_delete.go index 0c1ba9d..f57ccf8 100644 --- a/app/cmd/credential_delete.go +++ b/app/cmd/credential_delete.go @@ -67,6 +67,6 @@ var credentialDeleteCmd = &cobra.Command{ return }, Annotations: map[string]string{ - common.Since: "v0.0.24", + common.Since: common.VersionSince0024, }, } diff --git a/app/cmd/credential_delete_test.go b/app/cmd/credential_delete_test.go index 0e2ce7c..425b75a 100644 --- a/app/cmd/credential_delete_test.go +++ b/app/cmd/credential_delete_test.go @@ -2,8 +2,8 @@ package cmd import ( "bytes" - _ "github.com/Netflix/go-expect" - _ "github.com/jenkins-zh/jenkins-cli/app/cmd/common" + //"github.com/Netflix/go-expect" + //"github.com/jenkins-zh/jenkins-cli/app/cmd/common" "io/ioutil" "os" @@ -54,7 +54,7 @@ var _ = Describe("credential delete command", func() { BeforeEach(func() { var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/credential_list.go b/app/cmd/credential_list.go index 3976db1..aa4dae7 100644 --- a/app/cmd/credential_list.go +++ b/app/cmd/credential_list.go @@ -49,6 +49,6 @@ var credentialListCmd = &cobra.Command{ return }, Annotations: map[string]string{ - common.Since: "v0.0.24", + common.Since: common.VersionSince0024, }, } diff --git a/app/cmd/credential_list_test.go b/app/cmd/credential_list_test.go index 7e56054..a47cb09 100644 --- a/app/cmd/credential_list_test.go +++ b/app/cmd/credential_list_test.go @@ -50,7 +50,7 @@ var _ = Describe("credential list command", func() { BeforeEach(func() { var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/crumbissuer_test.go b/app/cmd/crumbissuer_test.go index 824cb82..3bd7c72 100644 --- a/app/cmd/crumbissuer_test.go +++ b/app/cmd/crumbissuer_test.go @@ -45,7 +45,7 @@ var _ = Describe("crumb command", func() { Context("basic cases", func() { It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/doc_test.go b/app/cmd/doc_test.go index 154dd9f..7da554f 100644 --- a/app/cmd/doc_test.go +++ b/app/cmd/doc_test.go @@ -22,7 +22,7 @@ var _ = Describe("doc command test", func() { rootOptions.ConfigFile = "test.yaml" - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/job_artifact_download_test.go b/app/cmd/job_artifact_download_test.go index b0f9ac4..adca545 100644 --- a/app/cmd/job_artifact_download_test.go +++ b/app/cmd/job_artifact_download_test.go @@ -44,7 +44,7 @@ var _ = Describe("job artifact download command", func() { Context("basic cases", func() { It("invalid build id", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -59,7 +59,7 @@ var _ = Describe("job artifact download command", func() { }) It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -96,7 +96,7 @@ var _ = Describe("job artifact download command", func() { }) It("should success, fake artifact id", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/job_artifact_test.go b/app/cmd/job_artifact_test.go index ccb5afe..1cc1125 100644 --- a/app/cmd/job_artifact_test.go +++ b/app/cmd/job_artifact_test.go @@ -52,7 +52,7 @@ var _ = Describe("job artifact command", func() { }) It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -72,7 +72,7 @@ a.log a.log 0 }) It("should success, zero build id", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -93,7 +93,7 @@ a.log a.log 0 }) It("should success, invalid build id", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/job_build_test.go b/app/cmd/job_build_test.go index b9b65e3..219dc5e 100644 --- a/app/cmd/job_build_test.go +++ b/app/cmd/job_build_test.go @@ -47,7 +47,7 @@ var _ = Describe("job build command", func() { Context("basic cases", func() { It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -90,7 +90,7 @@ var _ = Describe("job build command", func() { }) It("with --param-entry", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -126,7 +126,7 @@ var _ = Describe("job build command", func() { // Test: func(stdio terminal.Stdio) (err error) { // var data []byte // rootOptions.ConfigFile = "test.yaml" -// data, err = generateSampleConfig() +// data, err = GenerateSampleConfig() // err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) // // ctrl := gomock.NewController(t) diff --git a/app/cmd/job_create_test.go b/app/cmd/job_create_test.go index 19392ad..07aa1e6 100644 --- a/app/cmd/job_create_test.go +++ b/app/cmd/job_create_test.go @@ -54,7 +54,7 @@ var _ = Describe("job create command", func() { } var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/job_delete_test.go b/app/cmd/job_delete_test.go index 9f43f9d..2fb5365 100644 --- a/app/cmd/job_delete_test.go +++ b/app/cmd/job_delete_test.go @@ -51,7 +51,7 @@ var _ = Describe("job delete command", func() { }) It("should success, with batch mode", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -134,7 +134,7 @@ type EditorTest struct { //func RunPromptCommandTest(t *testing.T, test PromptCommandTest) { // RunTest(t, func(stdio terminal.Stdio) (err error) { // var data []byte -// data, err = generateSampleConfig() +// data, err = GenerateSampleConfig() // err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) // // test.BatchOption.Stdio = stdio diff --git a/app/cmd/job_disable_test.go b/app/cmd/job_disable_test.go index acb8ae7..5a746ed 100644 --- a/app/cmd/job_disable_test.go +++ b/app/cmd/job_disable_test.go @@ -26,7 +26,7 @@ var _ = Describe("job disable command", func() { rootOptions.Jenkins = "" rootOptions.ConfigFile = "test.yaml" - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/job_edit_test.go b/app/cmd/job_edit_test.go index 520f251..c04ba3c 100644 --- a/app/cmd/job_edit_test.go +++ b/app/cmd/job_edit_test.go @@ -48,7 +48,7 @@ var _ = Describe("job edit command", func() { Context("basic cases", func() { It("edit with script param", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -67,7 +67,7 @@ var _ = Describe("job edit command", func() { }) It("edit with file param", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -91,7 +91,7 @@ var _ = Describe("job edit command", func() { }) It("edit with url param", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/job_enable_test.go b/app/cmd/job_enable_test.go index 31eb30b..78a63ad 100644 --- a/app/cmd/job_enable_test.go +++ b/app/cmd/job_enable_test.go @@ -26,7 +26,7 @@ var _ = Describe("job enable command", func() { rootOptions.Jenkins = "" rootOptions.ConfigFile = "test.yaml" - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/job_history_test.go b/app/cmd/job_history_test.go index 2f852de..1355c36 100644 --- a/app/cmd/job_history_test.go +++ b/app/cmd/job_history_test.go @@ -50,7 +50,7 @@ var _ = Describe("job history command", func() { }) It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/job_input_test.go b/app/cmd/job_input_test.go index ded9149..68cf1d5 100644 --- a/app/cmd/job_input_test.go +++ b/app/cmd/job_input_test.go @@ -46,7 +46,7 @@ var _ = Describe("job input command", func() { Context("basic cases", func() { It("no params, will error", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -62,7 +62,7 @@ var _ = Describe("job input command", func() { }) It("should success, abort without inputs", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -102,7 +102,7 @@ var _ = Describe("job input command", func() { }) It("should success, process without inputs", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/job_log_test.go b/app/cmd/job_log_test.go index f011e9b..dbab851 100644 --- a/app/cmd/job_log_test.go +++ b/app/cmd/job_log_test.go @@ -44,7 +44,7 @@ var _ = Describe("job log command", func() { Context("basic cases, need RoundTripper", func() { It("output the last build log", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/job_param_test.go b/app/cmd/job_param_test.go index 16e8636..7836af3 100644 --- a/app/cmd/job_param_test.go +++ b/app/cmd/job_param_test.go @@ -44,7 +44,7 @@ var _ = Describe("job search command", func() { Context("basic cases, need RoundTripper", func() { It("without parameters", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -62,7 +62,7 @@ var _ = Describe("job search command", func() { }) It("with one parameter", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -80,7 +80,7 @@ var _ = Describe("job search command", func() { }) It("with one parameter, output with indent", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/job_search_test.go b/app/cmd/job_search_test.go index 7ff330d..6e2cfe6 100644 --- a/app/cmd/job_search_test.go +++ b/app/cmd/job_search_test.go @@ -41,7 +41,7 @@ var _ = Describe("job search command", func() { }) It("should success, search with one result item", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -65,7 +65,7 @@ fake fake WorkflowJob job/fake/ }) It("should success, search without keyword", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/job_stop_test.go b/app/cmd/job_stop_test.go index 87b8add..a14de04 100644 --- a/app/cmd/job_stop_test.go +++ b/app/cmd/job_stop_test.go @@ -30,7 +30,7 @@ var _ = Describe("job stop command", func() { rootOptions.ConfigFile = "test.yaml" var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -84,7 +84,7 @@ var _ = Describe("job stop command", func() { }) It("stop the last build, with batch mode", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/job_type_test.go b/app/cmd/job_type_test.go index 867684a..bfa5563 100644 --- a/app/cmd/job_type_test.go +++ b/app/cmd/job_type_test.go @@ -40,7 +40,7 @@ var _ = Describe("job type command", func() { Context("basic cases", func() { It("GetCategories", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -79,7 +79,7 @@ var _ = Describe("job type command", func() { }) It("should success, empty list", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -106,7 +106,7 @@ var _ = Describe("job type command", func() { }) It("should success, empty list", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -133,7 +133,7 @@ var _ = Describe("job type command", func() { }) It("should success, one item", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/open_test.go b/app/cmd/open_test.go index 0358983..0c34759 100644 --- a/app/cmd/open_test.go +++ b/app/cmd/open_test.go @@ -22,7 +22,7 @@ var _ = Describe("test open", func() { BeforeEach(func() { configFile = path.Join(os.TempDir(), "fake.yaml") - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(configFile, data, 0664) Expect(err).To(BeNil()) @@ -93,7 +93,7 @@ var _ = Describe("test open", func() { // defer os.Remove(configFile) // // var data []byte -// data, err = generateSampleConfig() +// data, err = GenerateSampleConfig() // err = ioutil.WriteFile(configFile, data, 0664) // // openOption.ExecContext = util.FakeExecCommandSuccess diff --git a/app/cmd/plugin_checkout_test.go b/app/cmd/plugin_checkout_test.go index 19fec07..fbc909a 100644 --- a/app/cmd/plugin_checkout_test.go +++ b/app/cmd/plugin_checkout_test.go @@ -39,7 +39,7 @@ var _ = Describe("plugin checkout command", func() { It("should success", func() { var err error var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/plugin_create_test.go b/app/cmd/plugin_create_test.go index bc2073c..7d8075e 100644 --- a/app/cmd/plugin_create_test.go +++ b/app/cmd/plugin_create_test.go @@ -16,7 +16,7 @@ var _ = Describe("plugin create test", func() { BeforeEach(func() { pluginCreateOptions.SystemCallExec = util.FakeSystemCallExecSuccess pluginCreateOptions.LookPathContext = util.FakeLookPath - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) rootOptions.ConfigFile = "test.yaml" err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) diff --git a/app/cmd/plugin_download_test.go b/app/cmd/plugin_download_test.go index 86e1d75..317723e 100644 --- a/app/cmd/plugin_download_test.go +++ b/app/cmd/plugin_download_test.go @@ -39,7 +39,7 @@ var _ = Describe("plugin download command", func() { It("should success", func() { var err error var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -56,9 +56,8 @@ var _ = Describe("plugin download command", func() { Expect(buf.String()).To(Equal("")) _, err = os.Stat("fake.hpi") - Expect(err).To(BeNil()) - defer os.Remove("fake.hpi") + Expect(err).To(BeNil()) }) }) }) diff --git a/app/cmd/plugin_install_test.go b/app/cmd/plugin_install_test.go index 6034621..43b574e 100644 --- a/app/cmd/plugin_install_test.go +++ b/app/cmd/plugin_install_test.go @@ -48,7 +48,7 @@ var _ = Describe("plugin install command", func() { Context("basic cases", func() { It("install one plugin", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -66,7 +66,7 @@ var _ = Describe("plugin install command", func() { }) It("unknow suite", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/plugin_list_test.go b/app/cmd/plugin_list_test.go index 00a5c16..69b924d 100644 --- a/app/cmd/plugin_list_test.go +++ b/app/cmd/plugin_list_test.go @@ -37,7 +37,7 @@ var _ = Describe("plugin list command", func() { Context("basic cases", func() { It("no plugin in the list", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -56,7 +56,7 @@ var _ = Describe("plugin list command", func() { }) It("one plugin in the list", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -77,7 +77,7 @@ fake 1.0 true }) It("one plugin in the list without headers", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -97,7 +97,7 @@ fake 1.0 true }) It("one plugin output with json format", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -117,7 +117,7 @@ fake 1.0 true }) It("one plugin output with yaml format", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -137,7 +137,7 @@ fake 1.0 true }) It("one plugin output with not support format", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/plugin_open_test.go b/app/cmd/plugin_open_test.go index ef46853..1fa10c3 100644 --- a/app/cmd/plugin_open_test.go +++ b/app/cmd/plugin_open_test.go @@ -17,7 +17,7 @@ var _ = Describe("plugin open test", func() { BeforeEach(func() { pluginOpenOption.ExecContext = util.FakeExecCommandSuccess - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) rootOptions.ConfigFile = "test.yaml" err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) diff --git a/app/cmd/plugin_release.go b/app/cmd/plugin_release.go index f1d8fbc..0515945 100644 --- a/app/cmd/plugin_release.go +++ b/app/cmd/plugin_release.go @@ -73,6 +73,6 @@ var pluginReleaseCmd = &cobra.Command{ return }, Annotations: map[string]string{ - common.Since: "v0.0.24", + common.Since: common.VersionSince0024, }, } diff --git a/app/cmd/plugin_release_test.go b/app/cmd/plugin_release_test.go index 7bc524a..5176156 100644 --- a/app/cmd/plugin_release_test.go +++ b/app/cmd/plugin_release_test.go @@ -16,7 +16,7 @@ var _ = Describe("plugin release test", func() { BeforeEach(func() { pluginReleaseOptions.SystemCallExec = util.FakeSystemCallExecSuccess pluginReleaseOptions.LookPathContext = util.FakeLookPath - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) rootOptions.ConfigFile = "test.yaml" err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) diff --git a/app/cmd/plugin_search_test.go b/app/cmd/plugin_search_test.go index 131c0f6..fcedab9 100644 --- a/app/cmd/plugin_search_test.go +++ b/app/cmd/plugin_search_test.go @@ -37,7 +37,7 @@ var _ = Describe("plugin search command", func() { Context("basic cases", func() { It("should success, empty list", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -55,7 +55,7 @@ var _ = Describe("plugin search command", func() { }) It("many plugins in the list", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -84,7 +84,7 @@ var _ = Describe("plugin search command", func() { }) It("should success, empty updateCenter list", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -112,7 +112,7 @@ var _ = Describe("plugin search command", func() { }) It("should success, null updateCenter and 500 installed list", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/plugin_trend_test.go b/app/cmd/plugin_trend_test.go index 5ee96ec..ff5b993 100644 --- a/app/cmd/plugin_trend_test.go +++ b/app/cmd/plugin_trend_test.go @@ -39,7 +39,7 @@ var _ = Describe("plugin trend command", func() { Context("basic cases", func() { It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/plugin_uninstall_test.go b/app/cmd/plugin_uninstall_test.go index fb75d11..ff0f4f0 100644 --- a/app/cmd/plugin_uninstall_test.go +++ b/app/cmd/plugin_uninstall_test.go @@ -39,7 +39,7 @@ var _ = Describe("plugin uninstall command", func() { Context("basic cases", func() { It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -59,7 +59,7 @@ var _ = Describe("plugin uninstall command", func() { }) It("with error", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/plugin_upgrade_test.go b/app/cmd/plugin_upgrade_test.go index 5c9494a..a424b64 100644 --- a/app/cmd/plugin_upgrade_test.go +++ b/app/cmd/plugin_upgrade_test.go @@ -39,7 +39,7 @@ var _ = Describe("plugin upgrade command", func() { Context("basic cases", func() { It("given plugin name, should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -57,7 +57,7 @@ var _ = Describe("plugin upgrade command", func() { }) It("findUpgradeablePlugins", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -86,7 +86,7 @@ var _ = Describe("plugin upgrade command", func() { }) It("upgrade all plugin, should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/plugin_upload_test.go b/app/cmd/plugin_upload_test.go index 88e8766..83bba14 100644 --- a/app/cmd/plugin_upload_test.go +++ b/app/cmd/plugin_upload_test.go @@ -37,7 +37,7 @@ var _ = Describe("plugin upload command", func() { Context("basic cases", func() { It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/queue_cancel_test.go b/app/cmd/queue_cancel_test.go index 7ecb7d5..6068c16 100644 --- a/app/cmd/queue_cancel_test.go +++ b/app/cmd/queue_cancel_test.go @@ -39,7 +39,7 @@ var _ = Describe("queue cancel command", func() { }) It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -57,7 +57,7 @@ var _ = Describe("queue cancel command", func() { }) It("should have error with invalid number", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/queue_list_test.go b/app/cmd/queue_list_test.go index 54cb2f7..a62929f 100644 --- a/app/cmd/queue_list_test.go +++ b/app/cmd/queue_list_test.go @@ -40,7 +40,7 @@ var _ = Describe("queue list command", func() { }) It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -71,7 +71,7 @@ var _ = Describe("queue list command", func() { }) It("output with table format", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/queue_test.go b/app/cmd/queue_test.go index a12a894..91ff6a0 100644 --- a/app/cmd/queue_test.go +++ b/app/cmd/queue_test.go @@ -31,7 +31,7 @@ var _ = Describe("queue command", func() { Context("without http requests", func() { It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/restart_test.go b/app/cmd/restart_test.go index 1fa5d2a..681833a 100644 --- a/app/cmd/restart_test.go +++ b/app/cmd/restart_test.go @@ -29,7 +29,7 @@ var _ = Describe("restart command", func() { rootOptions.ConfigFile = path.Join(os.TempDir(), "fake.yaml") var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/root.go b/app/cmd/root.go index 3632987..9421e19 100644 --- a/app/cmd/root.go +++ b/app/cmd/root.go @@ -2,12 +2,14 @@ package cmd import ( "fmt" + "github.com/jenkins-zh/jenkins-cli/app/cmd/common" "io" "log" "os" "os/exec" "regexp" "strings" + "time" . "github.com/jenkins-zh/jenkins-cli/app/config" "github.com/jenkins-zh/jenkins-cli/app/health" @@ -38,7 +40,11 @@ type RootOptions struct { ProxyAuth string ProxyDisable bool - Doctor bool + Doctor bool + StartTime time.Time + EndTime time.Time + + CommonOption *common.CommonOption LoggerLevel string } @@ -54,6 +60,7 @@ var rootCmd = &cobra.Command{ We'd love to hear your feedback at https://github.com/jenkins-zh/jenkins-cli/issues`, PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) { + rootOptions.StartTime = time.Now() if logger, err = util.InitLogger(rootOptions.LoggerLevel); err == nil { (&configOptions).Logger = logger client.SetLogger(logger) @@ -88,6 +95,15 @@ We'd love to hear your feedback at https://github.com/jenkins-zh/jenkins-cli/iss } return }, + PersistentPostRun: func(cmd *cobra.Command, args []string) { + cmdPath := getCmdPath(cmd) + + // calculate the time + rootOptions.EndTime = time.Now() + + logger.Debug("done with command", zap.String("command", cmdPath), + zap.Float64("duration", rootOptions.EndTime.Sub(rootOptions.StartTime).Seconds())) + }, BashCompletionFunction: jcliBashCompletionFunc, } @@ -175,6 +191,19 @@ func init() { rootCmd.SetOut(os.Stdout) loadPlugins(rootCmd) + + // add sub-commands + NewShutdownCmd(&rootOptions) +} + +// GetRootOptions returns the root options +func GetRootOptions() *RootOptions { + return &rootOptions +} + +// GetRootCommand returns the root cmd +func GetRootCommand() *cobra.Command { + return rootCmd } func getCurrentJenkinsFromOptions() (jenkinsServer *JenkinsServer) { diff --git a/app/cmd/root_test.go b/app/cmd/root_test.go index 7c2f5ed..e1842f4 100644 --- a/app/cmd/root_test.go +++ b/app/cmd/root_test.go @@ -270,7 +270,7 @@ var _ = Describe("Root cmd test", func() { defer os.Remove(configFile.Name()) - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(configFile.Name(), data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/shell_test.go b/app/cmd/shell_test.go index 597b6e7..116644b 100644 --- a/app/cmd/shell_test.go +++ b/app/cmd/shell_test.go @@ -36,7 +36,7 @@ var _ = Describe("shell command", func() { //Context("basic test", func() { // It("should success", func() { - // data, err := generateSampleConfig() + // data, err := GenerateSampleConfig() // Expect(err).To(BeNil()) // err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) // Expect(err).To(BeNil()) diff --git a/app/cmd/shutdown.go b/app/cmd/shutdown.go new file mode 100644 index 0000000..28849ff --- /dev/null +++ b/app/cmd/shutdown.go @@ -0,0 +1,77 @@ +package cmd + +import ( + "fmt" + "github.com/jenkins-zh/jenkins-cli/app/cmd/common" + "github.com/jenkins-zh/jenkins-cli/app/i18n" + "github.com/jenkins-zh/jenkins-cli/client" + "github.com/spf13/cobra" +) + +// ShutdownOption holds the options for shutdown cmd +type ShutdownOption struct { + common.BatchOption + common.CommonOption + RootOptions *RootOptions + + Safe bool + Prepare bool + CancelPrepare bool +} + +const ( + // SafeShutdown the text about shutdown safely + SafeShutdown = "Puts Jenkins into the quiet mode, wait for existing builds to be completed, and then shut down Jenkins" +) + +// NewShutdownCmd create the shutdown command +func NewShutdownCmd(rootOpt *RootOptions) (cmd *cobra.Command) { + shutdownOption := &ShutdownOption{ + RootOptions: rootOpt, + } + cmd = &cobra.Command{ + Use: "shutdown", + Short: i18n.T(SafeShutdown), + Long: i18n.T(SafeShutdown), + RunE: shutdownOption.runE, + } + shutdownOption.init(cmd) + return +} + +func (o *ShutdownOption) runE(cmd *cobra.Command, _ []string) (err error) { + jenkins := getCurrentJenkinsFromOptions() + if !o.Confirm(fmt.Sprintf("Are you sure to shutdown Jenkins %s?", jenkins.URL)) { + return + } + + jClient := &client.CoreClient{ + JenkinsCore: client.JenkinsCore{ + RoundTripper: o.RootOptions.CommonOption.RoundTripper, + Debug: o.RootOptions.Debug, + }, + } + getCurrentJenkinsAndClient(&(jClient.JenkinsCore)) + + if o.CancelPrepare { + err = jClient.PrepareShutdown(true) + } else if o.Prepare { + err = jClient.PrepareShutdown(false) + } else { + err = jClient.Shutdown(o.Safe) + } + return +} + +func (o *ShutdownOption) init(shutdownCmd *cobra.Command) { + rootCmd.AddCommand(shutdownCmd) + o.SetFlag(shutdownCmd) + shutdownCmd.Flags().BoolVarP(&o.Safe, "safe", "s", true, + i18n.T(SafeShutdown)) + shutdownCmd.Flags().BoolVarP(&o.Prepare, "prepare", "", false, + i18n.T("Put Jenkins in a Quiet mode, in preparation for a restart. In that mode Jenkins don’t start any build")) + shutdownCmd.Flags().BoolVarP(&o.CancelPrepare, "prepare-cancel", "", false, + i18n.T(" Cancel the effect of the “quiet-down” command")) + o.BatchOption.Stdio = common.GetSystemStdio() + o.CommonOption.Stdio = common.GetSystemStdio() +} diff --git a/app/cmd/shutdown_test.go b/app/cmd/shutdown_test.go new file mode 100644 index 0000000..87e4d9e --- /dev/null +++ b/app/cmd/shutdown_test.go @@ -0,0 +1,61 @@ +package cmd + +import ( + "bytes" + "github.com/jenkins-zh/jenkins-cli/app/cmd/common" + "github.com/jenkins-zh/jenkins-cli/client" + "github.com/jenkins-zh/jenkins-cli/mock/mhttp" + "io/ioutil" + "os" + "path" + + "github.com/golang/mock/gomock" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("shutdown command", func() { + var ( + ctrl *gomock.Controller + roundTripper *mhttp.MockRoundTripper + configFile string + err error + ) + + BeforeEach(func() { + rootOptions := GetRootOptions() + ctrl = gomock.NewController(GinkgoT()) + roundTripper = mhttp.NewMockRoundTripper(ctrl) + rootOptions.CommonOption = &common.CommonOption{ + RoundTripper: roundTripper, + } + rootOptions.Jenkins = "" + configFile = path.Join(os.TempDir(), "fake.yaml") + rootOptions.ConfigFile = configFile + + var data []byte + data, err = GenerateSampleConfig() + Expect(err).To(BeNil()) + err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) + Expect(err).To(BeNil()) + }) + + AfterEach(func() { + os.Remove(configFile) + GetRootOptions().ConfigFile = "" + ctrl.Finish() + }) + + Context("with batch mode", func() { + It("should success", func() { + client.PrepareForShutdown(roundTripper, "http://localhost:8080/jenkins", "admin", "111e3a2f0231198855dceaff96f20540a9", true) + + GetRootCommand().SetArgs([]string{"shutdown", "-b"}) + + buf := new(bytes.Buffer) + GetRootCommand().SetOutput(buf) + _, err = GetRootCommand().ExecuteC() + Expect(err).To(BeNil()) + }) + }) +}) diff --git a/app/cmd/user_create_test.go b/app/cmd/user_create_test.go index 2908fe7..27c35e2 100644 --- a/app/cmd/user_create_test.go +++ b/app/cmd/user_create_test.go @@ -39,7 +39,7 @@ var _ = Describe("user create command", func() { }) It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -58,7 +58,7 @@ var _ = Describe("user create command", func() { }) It("with status code 500", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/user_delete_test.go b/app/cmd/user_delete_test.go index aae76fb..597486f 100644 --- a/app/cmd/user_delete_test.go +++ b/app/cmd/user_delete_test.go @@ -38,7 +38,7 @@ var _ = Describe("user delete command", func() { }) It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -57,7 +57,7 @@ var _ = Describe("user delete command", func() { }) It("with status code 500", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/user_edit_test.go b/app/cmd/user_edit_test.go index f15889b..6278a6a 100644 --- a/app/cmd/user_edit_test.go +++ b/app/cmd/user_edit_test.go @@ -14,7 +14,7 @@ package cmd // configFile := path.Join(os.TempDir(), "fake.yaml") // defer os.Remove(configFile) // -// data, err := generateSampleConfig() +// data, err := GenerateSampleConfig() // err = ioutil.WriteFile(configFile, data, 0664) // // var ( diff --git a/app/cmd/user_test.go b/app/cmd/user_test.go index fcc08b7..fade306 100644 --- a/app/cmd/user_test.go +++ b/app/cmd/user_test.go @@ -40,7 +40,7 @@ var _ = Describe("user command", func() { }) It("should success", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) @@ -59,7 +59,7 @@ var _ = Describe("user command", func() { }) It("with status code 500", func() { - data, err := generateSampleConfig() + data, err := GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/user_token_test.go b/app/cmd/user_token_test.go index 56c0a4a..883bb2c 100644 --- a/app/cmd/user_token_test.go +++ b/app/cmd/user_token_test.go @@ -30,7 +30,7 @@ var _ = Describe("user token command", func() { rootOptions.ConfigFile = "test.yaml" var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/app/cmd/version_test.go b/app/cmd/version_test.go index 0cbf2ab..461cdf0 100644 --- a/app/cmd/version_test.go +++ b/app/cmd/version_test.go @@ -33,7 +33,7 @@ var _ = Describe("version command", func() { rootCmd.SetOutput(buf) var data []byte - data, err = generateSampleConfig() + data, err = GenerateSampleConfig() Expect(err).To(BeNil()) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(err).To(BeNil()) diff --git a/client/core.go b/client/core.go index 533be47..911cf89 100644 --- a/client/core.go +++ b/client/core.go @@ -38,6 +38,26 @@ func (q *CoreClient) RestartDirectly() (err error) { return } +// Shutdown puts Jenkins into the quiet mode, wait for existing builds to be completed, and then shut down Jenkins +func (q *CoreClient) Shutdown(safe bool) (err error) { + if safe { + _, err = q.RequestWithoutData("POST", "/safeExit", nil, nil, 200) + } else { + _, err = q.RequestWithoutData("POST", "/exit", nil, nil, 200) + } + return +} + +// PrepareShutdown Put Jenkins in a Quiet mode, in preparation for a restart. In that mode Jenkins don’t start any build +func (q *CoreClient) PrepareShutdown(cancel bool) (err error) { + if cancel { + _, err = q.RequestWithoutData("POST", "/cancelQuietDown", nil, nil, 200) + } else { + _, err = q.RequestWithoutData("POST", "/quietDown", nil, nil, 200) + } + return +} + // JenkinsIdentity belongs to a Jenkins type JenkinsIdentity struct { Fingerprint string diff --git a/client/core_test.go b/client/core_test.go index 55d9b23..685a046 100644 --- a/client/core_test.go +++ b/client/core_test.go @@ -69,4 +69,64 @@ var _ = Describe("core test", func() { })) }) }) + + Context("shutdown", func() { + var ( + err error + safe bool + ) + + JustBeforeEach(func() { + PrepareForShutdown(roundTripper, coreClient.URL, username, password, safe) + err = coreClient.Shutdown(safe) + }) + + Context("shutdown safely", func() { + BeforeEach(func() { + safe = true + }) + It("should success", func() { + Expect(err).To(BeNil()) + }) + }) + + Context("shutdown not safely", func() { + BeforeEach(func() { + safe = false + }) + It("should success", func() { + Expect(err).To(BeNil()) + }) + }) + }) + + Context("prepare shutdown", func() { + var ( + err error + cancel bool + ) + + JustBeforeEach(func() { + PrepareForCancelShutdown(roundTripper, coreClient.URL, username, password, cancel) + err = coreClient.PrepareShutdown(cancel) + }) + + Context("cancelQuietDown", func() { + BeforeEach(func() { + cancel = true + }) + It("should success", func() { + Expect(err).To(BeNil()) + }) + }) + + Context("quietDown", func() { + BeforeEach(func() { + cancel = false + }) + It("should success", func() { + Expect(err).To(BeNil()) + }) + }) + }) }) diff --git a/client/core_test_common.go b/client/core_test_common.go index 6a2dca3..6af6e9d 100644 --- a/client/core_test_common.go +++ b/client/core_test_common.go @@ -24,6 +24,30 @@ func PrepareRestartDirectly(roundTripper *mhttp.MockRoundTripper, rootURL, user, return } +// PrepareForShutdown only for test +func PrepareForShutdown(roundTripper *mhttp.MockRoundTripper, rootURL, user, password string, safe bool) { + var request *http.Request + if safe { + request, _ = http.NewRequest("POST", fmt.Sprintf("%s/safeExit", rootURL), nil) + } else { + request, _ = http.NewRequest("POST", fmt.Sprintf("%s/exit", rootURL), nil) + } + PrepareCommonPost(request, "", roundTripper, user, password, rootURL) + return +} + +// PrepareForCancelShutdown only for test +func PrepareForCancelShutdown(roundTripper *mhttp.MockRoundTripper, rootURL, user, password string, cancel bool) { + var request *http.Request + if cancel { + request, _ = http.NewRequest("POST", fmt.Sprintf("%s/cancelQuietDown", rootURL), nil) + } else { + request, _ = http.NewRequest("POST", fmt.Sprintf("%s/quietDown", rootURL), nil) + } + PrepareCommonPost(request, "", roundTripper, user, password, rootURL) + return +} + // PrepareForGetIdentity only for test func PrepareForGetIdentity(roundTripper *mhttp.MockRoundTripper, rootURL, user, password string) { request, _ := http.NewRequest("GET", fmt.Sprintf("%s/instance", rootURL), nil) diff --git a/client/pluginApi_test.go b/client/pluginApi_test.go index 06dd04d..570479b 100644 --- a/client/pluginApi_test.go +++ b/client/pluginApi_test.go @@ -63,9 +63,8 @@ var _ = Describe("plugin api test", func() { pluginAPI.DownloadPlugins(names) _, err := os.Stat("fake.hpi") - Expect(err).To(BeNil()) - defer os.Remove("fake.hpi") + Expect(err).To(BeNil()) }) It("use mirror", func() { @@ -78,9 +77,8 @@ var _ = Describe("plugin api test", func() { pluginAPI.DownloadPlugins(names) _, err := os.Stat("fake.hpi") - Expect(err).To(BeNil()) - defer os.Remove("fake.hpi") + Expect(err).To(BeNil()) }) It("with dependency which is not optional", func() { diff --git a/e2e/shutdown_test.go b/e2e/shutdown_test.go new file mode 100644 index 0000000..f1269b6 --- /dev/null +++ b/e2e/shutdown_test.go @@ -0,0 +1,14 @@ +package e2e + +import ( + "fmt" + "github.com/stretchr/testify/assert" + "os/exec" + "testing" +) + +func TestShutdown(t *testing.T) { + cmd := exec.Command("jcli", "shutdown", "--url", GetJenkinsURL()) + data, err := cmd.CombinedOutput() + assert.Nil(t, err, fmt.Sprintf("failed in shutdown Jenkins, output is %s", string(data))) +} diff --git a/sonar-project.properties b/sonar-project.properties index 6c08401..f8fb31b 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,3 +1,4 @@ +sonar.organization=jenkins-zh sonar.projectKey=jenkins-zh_jenkins-cli # this is the name and version displayed in the SonarCloud UI. sonar.projectName=jenkins-cli @@ -10,6 +11,8 @@ sonar.sources=. # Encoding of the source code. Default is default system encoding #sonar.sourceEncoding=UTF-8 -sonar.go.exclusions=**/vendor/**,**/**/*_test.go,client/test_methods.go +sonar.go.exclusions=**/vendor/**,**/**/*_test.go,client/test_methods.go,app/i18n/bindata.go +sonar.exclusions=**/vendor/**,**/**/*_test.go,client/test_methods.go,app/i18n/bindata.go +sonar.coverage.exclusions=app/i18n/bindata.go sonar.go.coverage.reportPaths=coverage.out -- GitLab