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

     1  package commands
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/chenbh/concourse/v6/atc"
     7  	"github.com/chenbh/concourse/v6/fly/commands/internal/displayhelpers"
     8  	"github.com/chenbh/concourse/v6/fly/rc"
     9  	"github.com/chenbh/concourse/v6/fly/ui"
    10  	"github.com/chenbh/concourse/v6/go-concourse/concourse"
    11  	"github.com/fatih/color"
    12  )
    13  
    14  type JobsCommand struct {
    15  	Pipeline string `short:"p" long:"pipeline" required:"true" description:"Get jobs in this pipeline"`
    16  	Json     bool   `long:"json" description:"Print command result as JSON"`
    17  	Team     string `long:"team" description:"Name of the team to which the pipeline belongs, if different from the target default"`
    18  }
    19  
    20  func (command *JobsCommand) Execute([]string) error {
    21  	var (
    22  		headers []string
    23  		team    concourse.Team
    24  	)
    25  	pipelineName := command.Pipeline
    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  	if command.Team != "" {
    38  		team, err = target.FindTeam(command.Team)
    39  		if err != nil {
    40  			return err
    41  		}
    42  	} else {
    43  		team = target.Team()
    44  	}
    45  
    46  	var jobs []atc.Job
    47  	jobs, err = team.ListJobs(pipelineName)
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	if command.Json {
    53  		err = displayhelpers.JsonPrint(jobs)
    54  		if err != nil {
    55  			return err
    56  		}
    57  		return nil
    58  	}
    59  
    60  	headers = []string{"name", "paused", "status", "next"}
    61  	table := ui.Table{Headers: ui.TableRow{}}
    62  	for _, h := range headers {
    63  		table.Headers = append(table.Headers, ui.TableCell{Contents: h, Color: color.New(color.Bold)})
    64  	}
    65  
    66  	for _, p := range jobs {
    67  		var pausedColumn ui.TableCell
    68  		if p.Paused {
    69  			pausedColumn.Contents = "yes"
    70  			pausedColumn.Color = color.New(color.FgCyan)
    71  		} else {
    72  			pausedColumn.Contents = "no"
    73  		}
    74  
    75  		row := ui.TableRow{}
    76  		row = append(row, ui.TableCell{Contents: p.Name})
    77  
    78  		row = append(row, pausedColumn)
    79  
    80  		var statusColumn ui.TableCell
    81  		if p.FinishedBuild != nil {
    82  			statusColumn.Contents = p.FinishedBuild.Status
    83  			switch p.FinishedBuild.Status {
    84  			case "pending":
    85  				statusColumn.Color = ui.PendingColor
    86  			case "started":
    87  				statusColumn.Color = ui.StartedColor
    88  			case "succeeded":
    89  				statusColumn.Color = ui.SucceededColor
    90  			case "failed":
    91  				statusColumn.Color = ui.FailedColor
    92  			case "errored":
    93  				statusColumn.Color = ui.ErroredColor
    94  			case "aborted":
    95  				statusColumn.Color = ui.AbortedColor
    96  			case "paused":
    97  				statusColumn.Color = ui.PausedColor
    98  			}
    99  		} else {
   100  			statusColumn.Contents = "n/a"
   101  		}
   102  		row = append(row, statusColumn)
   103  
   104  		var nextColumn ui.TableCell
   105  		if p.NextBuild != nil {
   106  			nextColumn.Contents = p.NextBuild.Status
   107  			switch p.NextBuild.Status {
   108  			case "pending:":
   109  				nextColumn.Color = ui.PendingColor
   110  			case "started":
   111  				nextColumn.Color = ui.StartedColor
   112  			}
   113  		} else {
   114  			nextColumn.Contents = "n/a"
   115  		}
   116  		row = append(row, nextColumn)
   117  
   118  		table.Data = append(table.Data, row)
   119  	}
   120  
   121  	return table.Render(os.Stdout, Fly.PrintTableHeaders)
   122  }