github.com/chenbh/concourse/v6@v6.4.2/fly/commands/watch.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"os"
     7  	"strconv"
     8  
     9  	"github.com/chenbh/concourse/v6/fly/commands/internal/flaghelpers"
    10  	"github.com/chenbh/concourse/v6/fly/eventstream"
    11  	"github.com/chenbh/concourse/v6/fly/rc"
    12  )
    13  
    14  type WatchCommand struct {
    15  	Job                      flaghelpers.JobFlag `short:"j" long:"job"         value-name:"PIPELINE/JOB"  description:"Watches builds of the given job"`
    16  	Build                    string              `short:"b" long:"build"                                  description:"Watches a specific build"`
    17  	Url                      string              `short:"u" long:"url"                                    description:"URL for the build or job to watch"`
    18  	Timestamp                bool                `short:"t" long:"timestamps"                             description:"Print with local timestamp"`
    19  	IgnoreEventParsingErrors bool                `long:"ignore-event-parsing-errors"                      description:"Ignore event parsing errors"`
    20  }
    21  
    22  func getBuildIDFromURL(target rc.Target, urlParam string) (int, error) {
    23  	var buildId int
    24  	client := target.Client()
    25  
    26  	u, err := url.Parse(urlParam)
    27  	if err != nil {
    28  		return 0, err
    29  	}
    30  
    31  	urlMap := parseUrlPath(u.Path)
    32  
    33  	parsedTargetUrl := url.URL{
    34  		Scheme: u.Scheme,
    35  		Host:   u.Host,
    36  	}
    37  
    38  	host := parsedTargetUrl.String()
    39  	if host != target.URL() {
    40  		err = fmt.Errorf("URL doesn't match target (%s, %s)", urlParam, target.URL())
    41  		return 0, err
    42  	}
    43  
    44  	team := urlMap["teams"]
    45  	if team != "" && team != target.Team().Name() {
    46  		err = fmt.Errorf("Team in URL doesn't match the current team of the target (%s, %s)", urlParam, team)
    47  		return 0, err
    48  	}
    49  
    50  	if urlMap["pipelines"] != "" && urlMap["jobs"] != "" {
    51  		build, err := GetBuild(client, target.Team(), urlMap["jobs"], urlMap["builds"], urlMap["pipelines"])
    52  
    53  		if err != nil {
    54  			return 0, err
    55  		}
    56  		buildId = build.ID
    57  	} else if urlMap["builds"] != "" {
    58  		buildId, err = strconv.Atoi(urlMap["builds"])
    59  
    60  		if err != nil {
    61  			return 0, err
    62  		}
    63  	} else {
    64  		return 0, fmt.Errorf("No build found in %s", urlParam)
    65  	}
    66  	return buildId, nil
    67  }
    68  
    69  func (command *WatchCommand) Execute(args []string) error {
    70  	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	err = target.Validate()
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	var buildId int
    81  	client := target.Client()
    82  	if command.Job.JobName != "" || command.Build == "" && command.Url == "" {
    83  		build, err := GetBuild(client, target.Team(), command.Job.JobName, command.Build, command.Job.PipelineName)
    84  		if err != nil {
    85  			return err
    86  		}
    87  		buildId = build.ID
    88  	} else if command.Build != "" {
    89  		buildId, err = strconv.Atoi(command.Build)
    90  
    91  		if err != nil {
    92  			return err
    93  		}
    94  	} else if command.Url != "" {
    95  		buildId, err = getBuildIDFromURL(target, command.Url)
    96  
    97  		if err != nil {
    98  			return err
    99  		}
   100  	}
   101  
   102  	eventSource, err := client.BuildEvents(fmt.Sprintf("%d", buildId))
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	renderOptions := eventstream.RenderOptions{
   108  		ShowTimestamp:            command.Timestamp,
   109  		IgnoreEventParsingErrors: command.IgnoreEventParsingErrors,
   110  	}
   111  
   112  	exitCode := eventstream.Render(os.Stdout, eventSource, renderOptions)
   113  
   114  	eventSource.Close()
   115  
   116  	os.Exit(exitCode)
   117  
   118  	return nil
   119  }