github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/commands/trigger_job.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/atc" 10 "github.com/pf-qiu/concourse/v6/fly/commands/internal/flaghelpers" 11 "github.com/pf-qiu/concourse/v6/fly/eventstream" 12 "github.com/pf-qiu/concourse/v6/fly/rc" 13 "github.com/pf-qiu/concourse/v6/fly/ui" 14 "github.com/pf-qiu/concourse/v6/go-concourse/concourse" 15 ) 16 17 type TriggerJobCommand struct { 18 Job flaghelpers.JobFlag `short:"j" long:"job" required:"true" value-name:"PIPELINE/JOB" description:"Name of a job to trigger"` 19 Watch bool `short:"w" long:"watch" description:"Start watching the build output"` 20 Team string `long:"team" description:"Name of the team to which the job belongs, if different from the target default"` 21 } 22 23 func (command *TriggerJobCommand) Execute(args []string) error { 24 jobName := command.Job.JobName 25 pipelineRef := command.Job.PipelineRef 26 27 target, err := rc.LoadTarget(Fly.Target, Fly.Verbose) 28 if err != nil { 29 return err 30 } 31 32 err = target.Validate() 33 if err != nil { 34 return err 35 } 36 37 var ( 38 build atc.Build 39 team concourse.Team 40 ) 41 if command.Team != "" { 42 team, err = target.FindTeam(command.Team) 43 if err != nil { 44 return err 45 } 46 } else { 47 team = target.Team() 48 } 49 50 build, err = team.CreateJobBuild(pipelineRef, jobName) 51 if err != nil { 52 return err 53 } else { 54 fmt.Printf("started %s/%s #%s\n", pipelineRef.String(), jobName, build.Name) 55 } 56 57 if command.Watch { 58 terminate := make(chan os.Signal, 1) 59 60 go func(terminate <-chan os.Signal) { 61 <-terminate 62 fmt.Fprintf(ui.Stderr, "\ndetached, build is still running...\n") 63 fmt.Fprintf(ui.Stderr, "re-attach to it with:\n\n") 64 fmt.Fprintf(ui.Stderr, " "+ui.Embolden(fmt.Sprintf("fly -t %s watch -j %s/%s -b %s\n\n", Fly.Target, pipelineRef.String(), jobName, build.Name))) 65 os.Exit(2) 66 }(terminate) 67 68 signal.Notify(terminate, syscall.SIGINT, syscall.SIGTERM) 69 70 fmt.Println("") 71 eventSource, err := target.Client().BuildEvents(fmt.Sprintf("%d", build.ID)) 72 if err != nil { 73 return err 74 } 75 76 renderOptions := eventstream.RenderOptions{} 77 78 exitCode := eventstream.Render(os.Stdout, eventSource, renderOptions) 79 80 eventSource.Close() 81 82 os.Exit(exitCode) 83 } 84 85 return nil 86 }