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

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  
     7  	"github.com/pf-qiu/concourse/v6/atc"
     8  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/flaghelpers"
     9  	"github.com/pf-qiu/concourse/v6/fly/rc"
    10  )
    11  
    12  type ChecklistCommand struct {
    13  	Pipeline flaghelpers.PipelineFlag `short:"p" long:"pipeline" required:"true" description:"The pipeline from which to generate the Checkfile"`
    14  }
    15  
    16  func (command *ChecklistCommand) Validate() error {
    17  	_, err := command.Pipeline.Validate()
    18  	return err
    19  }
    20  
    21  func (command *ChecklistCommand) Execute([]string) error {
    22  	err := command.Validate()
    23  	if err != nil {
    24  		return err
    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  	pipelineRef := command.Pipeline.Ref()
    38  	config, _, _, err := target.Team().PipelineConfig(pipelineRef)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	printCheckfile(target.Team().Name(), pipelineRef.String(), config, target.Client().URL())
    44  
    45  	return nil
    46  }
    47  
    48  func printCheckfile(teamName, pipelineName string, config atc.Config, url string) {
    49  	orphanHeaderName := "misc"
    50  	if len(config.Groups) == 0 {
    51  		orphanHeaderName = pipelineName
    52  	}
    53  
    54  	for _, group := range config.Groups {
    55  		printGroup(teamName, pipelineName, group, url)
    56  	}
    57  
    58  	miscJobs := orphanedJobs(config)
    59  	if len(miscJobs) > 0 {
    60  		printGroup(teamName, pipelineName, atc.GroupConfig{Name: orphanHeaderName, Jobs: miscJobs}, url)
    61  	}
    62  }
    63  
    64  func printGroup(teamName, pipelineName string, group atc.GroupConfig, url string) {
    65  	fmt.Printf("#- %s\n", group.Name)
    66  	for _, job := range group.Jobs {
    67  		fmt.Printf("%s: concourse.check %s %s %s %s\n", job, url, teamName, pipelineName, job)
    68  	}
    69  	fmt.Println("")
    70  }
    71  
    72  func orphanedJobs(config atc.Config) []string {
    73  	allJobNames := map[string]struct{}{}
    74  	for _, jobConfig := range config.Jobs {
    75  		allJobNames[jobConfig.Name] = struct{}{}
    76  	}
    77  
    78  	for _, group := range config.Groups {
    79  		for _, job := range group.Jobs {
    80  			delete(allJobNames, job)
    81  		}
    82  	}
    83  
    84  	result := make([]string, 0, len(config.Jobs))
    85  	for job := range allJobNames {
    86  		result = append(result, job)
    87  	}
    88  
    89  	sort.Strings(result)
    90  	return result
    91  }