forked from vmware-archive/fly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigger_job.go
66 lines (51 loc) · 1.62 KB
/
trigger_job.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package commands
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/concourse/fly/commands/internal/flaghelpers"
"github.com/concourse/fly/eventstream"
"github.com/concourse/fly/rc"
"github.com/concourse/fly/ui"
)
type TriggerJobCommand struct {
Job flaghelpers.JobFlag `short:"j" long:"job" required:"true" value-name:"PIPELINE/JOB" description:"Name of a job to trigger"`
Watch bool `short:"w" long:"watch" description:"Start watching the build output"`
}
func (command *TriggerJobCommand) Execute(args []string) error {
pipelineName, jobName := command.Job.PipelineName, command.Job.JobName
target, err := rc.LoadTarget(Fly.Target)
if err != nil {
return err
}
err = target.Validate()
if err != nil {
return err
}
build, err := target.Team().CreateJobBuild(pipelineName, jobName)
if err != nil {
return err
}
fmt.Printf("started %s/%s #%s\n", pipelineName, jobName, build.Name)
if command.Watch {
terminate := make(chan os.Signal, 1)
go func(terminate <-chan os.Signal) {
<-terminate
fmt.Fprintf(ui.Stderr, "\ndetached, build is still running...\n")
fmt.Fprintf(ui.Stderr, "re-attach to it with:\n\n")
fmt.Fprintf(ui.Stderr, " "+ui.Embolden(fmt.Sprintf("fly -t %s watch -j %s/%s -b %s\n\n", Fly.Target, pipelineName, jobName, build.Name)))
os.Exit(2)
}(terminate)
signal.Notify(terminate, syscall.SIGINT, syscall.SIGTERM)
fmt.Println("")
eventSource, err := target.Client().BuildEvents(fmt.Sprintf("%d", build.ID))
if err != nil {
return err
}
exitCode := eventstream.Render(os.Stdout, eventSource)
eventSource.Close()
os.Exit(exitCode)
}
return nil
}