Skip to content

Commit

Permalink
update empty artifacts message
Browse files Browse the repository at this point in the history
Signed-off-by: Jeeva Kandasamy <[email protected]>
  • Loading branch information
jkandasa committed Jun 21, 2021
1 parent ae21a63 commit 26393cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
8 changes: 5 additions & 3 deletions cmd/command/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
}
12 changes: 10 additions & 2 deletions pkg/jenkins/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,23 @@ 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 {
return directoryFinal, err
}

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])
Expand Down

0 comments on commit 26393cf

Please sign in to comment.