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

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/signal"
     7  	"syscall"
     8  
     9  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/flaghelpers"
    10  	"github.com/pf-qiu/concourse/v6/fly/eventstream"
    11  	"github.com/pf-qiu/concourse/v6/fly/rc"
    12  	"github.com/pf-qiu/concourse/v6/fly/ui"
    13  )
    14  
    15  type RerunBuildCommand struct {
    16  	Job   flaghelpers.JobFlag `short:"j" long:"job" required:"true" value-name:"PIPELINE/JOB" description:"Name of the job that you want to rerun a build for"`
    17  	Build string              `short:"b" long:"build" required:"true" description:"The number of the build to rerun"`
    18  	Watch bool                `short:"w" long:"watch" description:"Start watching the rerun build output"`
    19  }
    20  
    21  func (command *RerunBuildCommand) Execute(args []string) error {
    22  	jobName, buildName := command.Job.JobName, command.Build
    23  	pipelineRef := command.Job.PipelineRef
    24  
    25  	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
    26  	if err != nil {
    27  		return err
    28  	}
    29  
    30  	err = target.Validate()
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	build, err := target.Team().RerunJobBuild(pipelineRef, jobName, buildName)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	fmt.Printf("started %s/%s #%s\n", pipelineRef.String(), jobName, build.Name)
    40  
    41  	if command.Watch {
    42  		terminate := make(chan os.Signal, 1)
    43  
    44  		go func(terminate <-chan os.Signal) {
    45  			<-terminate
    46  			fmt.Fprintf(ui.Stderr, "\ndetached, build is still running...\n")
    47  			fmt.Fprintf(ui.Stderr, "re-attach to it with:\n\n")
    48  			fmt.Fprintf(ui.Stderr, "    "+ui.Embolden(fmt.Sprintf("fly -t %s watch -j %s/%s -b %s\n\n", Fly.Target, pipelineRef.QueryParams(), jobName, build.Name)))
    49  			os.Exit(2)
    50  		}(terminate)
    51  
    52  		signal.Notify(terminate, syscall.SIGINT, syscall.SIGTERM)
    53  
    54  		fmt.Println("")
    55  		eventSource, err := target.Client().BuildEvents(fmt.Sprintf("%d", build.ID))
    56  		if err != nil {
    57  			return err
    58  		}
    59  
    60  		renderOptions := eventstream.RenderOptions{}
    61  
    62  		exitCode := eventstream.Render(os.Stdout, eventSource, renderOptions)
    63  
    64  		eventSource.Close()
    65  
    66  		os.Exit(exitCode)
    67  	}
    68  
    69  	return nil
    70  }