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

     1  package commands
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/pf-qiu/concourse/v6/atc"
     7  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/displayhelpers"
     8  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/flaghelpers"
     9  	"github.com/pf-qiu/concourse/v6/fly/rc"
    10  	"github.com/pf-qiu/concourse/v6/fly/ui"
    11  	"github.com/pf-qiu/concourse/v6/go-concourse/concourse"
    12  	"github.com/fatih/color"
    13  )
    14  
    15  type JobsCommand struct {
    16  	Pipeline flaghelpers.PipelineFlag `short:"p" long:"pipeline" required:"true" description:"Get jobs in this pipeline"`
    17  	Json     bool                     `long:"json" description:"Print command result as JSON"`
    18  	Team     string                   `long:"team" description:"Name of the team to which the pipeline belongs, if different from the target default"`
    19  }
    20  
    21  func (command *JobsCommand) Execute([]string) error {
    22  	var (
    23  		headers []string
    24  		team    concourse.Team
    25  	)
    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(command.Pipeline.Ref())
    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 = ui.BuildStatusCell(p.FinishedBuild.Status)
    83  		} else {
    84  			statusColumn.Contents = "n/a"
    85  		}
    86  		row = append(row, statusColumn)
    87  
    88  		var nextColumn ui.TableCell
    89  		if p.NextBuild != nil {
    90  			nextColumn = ui.BuildStatusCell(p.NextBuild.Status)
    91  		} else {
    92  			nextColumn.Contents = "n/a"
    93  		}
    94  		row = append(row, nextColumn)
    95  
    96  		table.Data = append(table.Data, row)
    97  	}
    98  
    99  	return table.Render(os.Stdout, Fly.PrintTableHeaders)
   100  }