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

     1  package commands
     2  
     3  import (
     4  	"os"
     5  	"time"
     6  
     7  	"github.com/pf-qiu/concourse/v6/atc"
     8  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/displayhelpers"
     9  	"github.com/pf-qiu/concourse/v6/fly/rc"
    10  	"github.com/pf-qiu/concourse/v6/fly/ui"
    11  	"github.com/fatih/color"
    12  )
    13  
    14  type PipelinesCommand struct {
    15  	All             bool `short:"a"  long:"all" description:"Show pipelines across all teams"`
    16  	IncludeArchived bool `long:"include-archived" description:"Show archived pipelines"`
    17  	Json            bool `long:"json" description:"Print command result as JSON"`
    18  }
    19  
    20  func (command *PipelinesCommand) Execute([]string) error {
    21  	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
    22  	if err != nil {
    23  		return err
    24  	}
    25  
    26  	err = target.Validate()
    27  	if err != nil {
    28  		return err
    29  	}
    30  
    31  	var unfilteredPipelines []atc.Pipeline
    32  
    33  	if command.All {
    34  		unfilteredPipelines, err = target.Client().ListPipelines()
    35  	} else {
    36  		unfilteredPipelines, err = target.Team().ListPipelines()
    37  	}
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	headers := command.buildHeader()
    43  	pipelines := command.filterPipelines(unfilteredPipelines)
    44  
    45  	if command.Json {
    46  		err = displayhelpers.JsonPrint(pipelines)
    47  		if err != nil {
    48  			return err
    49  		}
    50  		return nil
    51  	}
    52  
    53  	table := ui.Table{Headers: ui.TableRow{}}
    54  	for _, h := range headers {
    55  		table.Headers = append(table.Headers, ui.TableCell{Contents: h, Color: color.New(color.Bold)})
    56  	}
    57  
    58  	for _, p := range pipelines {
    59  		var pausedColumn ui.TableCell
    60  		if p.Paused {
    61  			pausedColumn.Contents = "yes"
    62  			pausedColumn.Color = ui.OnColor
    63  		} else {
    64  			pausedColumn.Contents = "no"
    65  		}
    66  
    67  		var publicColumn ui.TableCell
    68  		if p.Public {
    69  			publicColumn.Contents = "yes"
    70  			publicColumn.Color = ui.OnColor
    71  		} else {
    72  			publicColumn.Contents = "no"
    73  		}
    74  
    75  		var archivedColumn ui.TableCell
    76  		if command.IncludeArchived {
    77  			if p.Archived {
    78  				archivedColumn.Contents = "yes"
    79  				archivedColumn.Color = ui.OnColor
    80  			} else {
    81  				archivedColumn.Contents = "no"
    82  			}
    83  		}
    84  
    85  		row := ui.TableRow{}
    86  		row = append(row, ui.TableCell{Contents: p.Ref().String()})
    87  		if command.All {
    88  			row = append(row, ui.TableCell{Contents: p.TeamName})
    89  		}
    90  		row = append(row, pausedColumn)
    91  		row = append(row, publicColumn)
    92  		if command.IncludeArchived {
    93  			row = append(row, archivedColumn)
    94  		}
    95  		row = append(row, ui.TableCell{Contents: time.Unix(p.LastUpdated, 0).String()})
    96  
    97  		table.Data = append(table.Data, row)
    98  	}
    99  
   100  	return table.Render(os.Stdout, Fly.PrintTableHeaders)
   101  }
   102  
   103  func (command *PipelinesCommand) buildHeader() []string {
   104  	var headers []string
   105  	if command.All {
   106  		headers = []string{"name", "team", "paused", "public"}
   107  	} else {
   108  		headers = []string{"name", "paused", "public"}
   109  	}
   110  
   111  	if command.IncludeArchived {
   112  		headers = append(headers, "archived")
   113  	}
   114  	headers = append(headers, "last updated")
   115  
   116  	return headers
   117  }
   118  
   119  func (command *PipelinesCommand) filterPipelines(unfilteredPipelines []atc.Pipeline) []atc.Pipeline {
   120  	pipelines := make([]atc.Pipeline, 0)
   121  
   122  	if !command.IncludeArchived {
   123  		for _, p := range unfilteredPipelines {
   124  			if !p.Archived {
   125  				pipelines = append(pipelines, p)
   126  			}
   127  		}
   128  	} else {
   129  		pipelines = unfilteredPipelines
   130  	}
   131  
   132  	return pipelines
   133  }