github.1485827954.workers.dev/nektos/act@v0.2.63/cmd/list.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  
     8  	"github.com/nektos/act/pkg/model"
     9  )
    10  
    11  func printList(plan *model.Plan) error {
    12  	type lineInfoDef struct {
    13  		jobID   string
    14  		jobName string
    15  		stage   string
    16  		wfName  string
    17  		wfFile  string
    18  		events  string
    19  	}
    20  	lineInfos := []lineInfoDef{}
    21  
    22  	header := lineInfoDef{
    23  		jobID:   "Job ID",
    24  		jobName: "Job name",
    25  		stage:   "Stage",
    26  		wfName:  "Workflow name",
    27  		wfFile:  "Workflow file",
    28  		events:  "Events",
    29  	}
    30  
    31  	jobs := map[string]bool{}
    32  	duplicateJobIDs := false
    33  
    34  	jobIDMaxWidth := len(header.jobID)
    35  	jobNameMaxWidth := len(header.jobName)
    36  	stageMaxWidth := len(header.stage)
    37  	wfNameMaxWidth := len(header.wfName)
    38  	wfFileMaxWidth := len(header.wfFile)
    39  	eventsMaxWidth := len(header.events)
    40  
    41  	for i, stage := range plan.Stages {
    42  		for _, r := range stage.Runs {
    43  			jobID := r.JobID
    44  			line := lineInfoDef{
    45  				jobID:   jobID,
    46  				jobName: r.String(),
    47  				stage:   strconv.Itoa(i),
    48  				wfName:  r.Workflow.Name,
    49  				wfFile:  r.Workflow.File,
    50  				events:  strings.Join(r.Workflow.On(), `,`),
    51  			}
    52  			if _, ok := jobs[jobID]; ok {
    53  				duplicateJobIDs = true
    54  			} else {
    55  				jobs[jobID] = true
    56  			}
    57  			lineInfos = append(lineInfos, line)
    58  			if jobIDMaxWidth < len(line.jobID) {
    59  				jobIDMaxWidth = len(line.jobID)
    60  			}
    61  			if jobNameMaxWidth < len(line.jobName) {
    62  				jobNameMaxWidth = len(line.jobName)
    63  			}
    64  			if stageMaxWidth < len(line.stage) {
    65  				stageMaxWidth = len(line.stage)
    66  			}
    67  			if wfNameMaxWidth < len(line.wfName) {
    68  				wfNameMaxWidth = len(line.wfName)
    69  			}
    70  			if wfFileMaxWidth < len(line.wfFile) {
    71  				wfFileMaxWidth = len(line.wfFile)
    72  			}
    73  			if eventsMaxWidth < len(line.events) {
    74  				eventsMaxWidth = len(line.events)
    75  			}
    76  		}
    77  	}
    78  
    79  	jobIDMaxWidth += 2
    80  	jobNameMaxWidth += 2
    81  	stageMaxWidth += 2
    82  	wfNameMaxWidth += 2
    83  	wfFileMaxWidth += 2
    84  
    85  	fmt.Printf("%*s%*s%*s%*s%*s%*s\n",
    86  		-stageMaxWidth, header.stage,
    87  		-jobIDMaxWidth, header.jobID,
    88  		-jobNameMaxWidth, header.jobName,
    89  		-wfNameMaxWidth, header.wfName,
    90  		-wfFileMaxWidth, header.wfFile,
    91  		-eventsMaxWidth, header.events,
    92  	)
    93  	for _, line := range lineInfos {
    94  		fmt.Printf("%*s%*s%*s%*s%*s%*s\n",
    95  			-stageMaxWidth, line.stage,
    96  			-jobIDMaxWidth, line.jobID,
    97  			-jobNameMaxWidth, line.jobName,
    98  			-wfNameMaxWidth, line.wfName,
    99  			-wfFileMaxWidth, line.wfFile,
   100  			-eventsMaxWidth, line.events,
   101  		)
   102  	}
   103  	if duplicateJobIDs {
   104  		fmt.Print("\nDetected multiple jobs with the same job name, use `-W` to specify the path to the specific workflow.\n")
   105  	}
   106  	return nil
   107  }