From 26393cf2705dafd2ef73bd8b0fa092fdd4bb9b50 Mon Sep 17 00:00:00 2001 From: Jeeva Kandasamy Date: Mon, 21 Jun 2021 23:41:06 +0530 Subject: [PATCH] update empty artifacts message Signed-off-by: Jeeva Kandasamy --- cmd/command/download.go | 8 +++++--- pkg/jenkins/client.go | 12 ++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/cmd/command/download.go b/cmd/command/download.go index f22dab4..0e08b3f 100644 --- a/cmd/command/download.go +++ b/cmd/command/download.go @@ -33,18 +33,20 @@ var downloadArtifacts = &cobra.Command{ Use: "artifact", Aliases: []string{"artifacts"}, Short: "Download artifact of a build", - Example: ` jenkinsctl download artifact 2101 --to-dir /tmp/artifacts`, + Example: ` jenkinsctl download artifact --build-number 2101 --to-dir /tmp/artifacts`, Run: func(cmd *cobra.Command, args []string) { client := jenkins.NewClient(CONFIG) if client == nil { return } - savedLocation, err := client.DownloadArtifacts(CONFIG.JobContext, buildNumber, artifactSaveLocation) + savedLocation, err := client.DownloadArtifacts(ioStreams.Out, CONFIG.JobContext, buildNumber, artifactSaveLocation) if err != nil { fmt.Fprintf(ioStreams.ErrOut, "error on downloading artifacts. error:[%s]\n", err) return } - fmt.Fprintf(ioStreams.Out, "artifacts are downloaded on the directory: %s\n", savedLocation) + if savedLocation != "" { + fmt.Fprintf(ioStreams.Out, "artifacts are downloaded on the directory: %s\n", savedLocation) + } }, } diff --git a/pkg/jenkins/client.go b/pkg/jenkins/client.go index b0576ad..dd5b0b0 100644 --- a/pkg/jenkins/client.go +++ b/pkg/jenkins/client.go @@ -186,7 +186,7 @@ func (jc *Client) ListParameters(jobName string) ([]gojenkins.ParameterDefinitio } // DownloadArtifacts of a build -func (jc *Client) DownloadArtifacts(jobName string, buildNumber int, toDirectory string) (string, error) { +func (jc *Client) DownloadArtifacts(out io.Writer, jobName string, buildNumber int, toDirectory string) (string, error) { directoryFinal := filepath.Join(toDirectory, jobName, strconv.Itoa(buildNumber)) build, err := jc.api.GetBuild(jc.ctx, jobName, int64(buildNumber)) if err != nil { @@ -194,7 +194,15 @@ func (jc *Client) DownloadArtifacts(jobName string, buildNumber int, toDirectory } dirSplitter := fmt.Sprintf("%d/artifact/", buildNumber) - for _, a := range build.GetArtifacts() { + + artifacts := build.GetArtifacts() + + if len(artifacts) == 0 { + fmt.Fprintln(out, "no artifacts found") + return "", nil + } + + for _, a := range artifacts { subDir := "" if dirs := strings.SplitAfterN(a.Path, dirSplitter, 2); len(dirs) > 1 { subDir = filepath.Dir(dirs[1])