github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/commands/abort_build.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/pf-qiu/concourse/v6/atc"
     8  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/flaghelpers"
     9  	"github.com/pf-qiu/concourse/v6/fly/rc"
    10  )
    11  
    12  type AbortBuildCommand struct {
    13  	Job   flaghelpers.JobFlag `short:"j" long:"job" value-name:"PIPELINE/JOB"   description:"Name of a job to cancel"`
    14  	Build string              `short:"b" long:"build" required:"true" description:"If job is specified: build number to cancel. If job not specified: build id"`
    15  }
    16  
    17  func (command *AbortBuildCommand) Execute([]string) error {
    18  	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
    19  	if err != nil {
    20  		return err
    21  	}
    22  
    23  	err = target.Validate()
    24  	if err != nil {
    25  		return err
    26  	}
    27  
    28  	var build atc.Build
    29  	var exists bool
    30  	if command.Job.PipelineRef.Name == "" && command.Job.JobName == "" {
    31  		build, exists, err = target.Client().Build(command.Build)
    32  	} else {
    33  		build, exists, err = target.Team().JobBuild(command.Job.PipelineRef, command.Job.JobName, command.Build)
    34  	}
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	if !exists {
    40  		return fmt.Errorf("build does not exist")
    41  	}
    42  
    43  	if err := target.Client().AbortBuild(strconv.Itoa(build.ID)); err != nil {
    44  		return err
    45  	}
    46  
    47  	fmt.Println("build successfully aborted")
    48  	return nil
    49  }