feat: improve list (#786)

This commit is contained in:
Ryan 2021-08-30 15:38:03 +00:00 committed by GitHub
parent de44a505da
commit 37aaec81f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 19 deletions

View file

@ -3,38 +3,51 @@ package cmd
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"strings"
"github.com/nektos/act/pkg/model" "github.com/nektos/act/pkg/model"
) )
func printList(plan *model.Plan) error { func printList(plan *model.Plan) error {
type lineInfoDef struct { type lineInfoDef struct {
id string jobID string
stage string jobName string
name string stage string
wfName string
wfFile string
events string
} }
lineInfos := []lineInfoDef{} lineInfos := []lineInfoDef{}
header := lineInfoDef{ header := lineInfoDef{
id: "ID", jobID: "Job ID",
stage: "Stage", jobName: "Job name",
name: "Name", stage: "Stage",
wfName: "Workflow name",
wfFile: "Workflow file",
events: "Events",
} }
jobs := map[string]bool{} jobs := map[string]bool{}
duplicateJobIDs := false duplicateJobIDs := false
idMaxWidth := len(header.id) jobIDMaxWidth := len(header.jobID)
jobNameMaxWidth := len(header.jobName)
stageMaxWidth := len(header.stage) stageMaxWidth := len(header.stage)
nameMaxWidth := len(header.name) wfNameMaxWidth := len(header.wfName)
wfFileMaxWidth := len(header.wfFile)
eventsMaxWidth := len(header.events)
for i, stage := range plan.Stages { for i, stage := range plan.Stages {
for _, r := range stage.Runs { for _, r := range stage.Runs {
jobID := r.JobID jobID := r.JobID
line := lineInfoDef{ line := lineInfoDef{
id: jobID, jobID: jobID,
stage: strconv.Itoa(i), jobName: r.String(),
name: r.String(), stage: strconv.Itoa(i),
wfName: r.Workflow.Name,
wfFile: r.Workflow.File,
events: strings.Join(r.Workflow.On(), `,`),
} }
if _, ok := jobs[jobID]; ok { if _, ok := jobs[jobID]; ok {
duplicateJobIDs = true duplicateJobIDs = true
@ -42,25 +55,50 @@ func printList(plan *model.Plan) error {
jobs[jobID] = true jobs[jobID] = true
} }
lineInfos = append(lineInfos, line) lineInfos = append(lineInfos, line)
if idMaxWidth < len(line.id) { if jobIDMaxWidth < len(line.jobID) {
idMaxWidth = len(line.id) jobIDMaxWidth = len(line.jobID)
}
if jobNameMaxWidth < len(line.jobName) {
jobNameMaxWidth = len(line.jobName)
} }
if stageMaxWidth < len(line.stage) { if stageMaxWidth < len(line.stage) {
stageMaxWidth = len(line.stage) stageMaxWidth = len(line.stage)
} }
if nameMaxWidth < len(line.name) { if wfNameMaxWidth < len(line.wfName) {
nameMaxWidth = len(line.name) wfNameMaxWidth = len(line.wfName)
}
if wfFileMaxWidth < len(line.wfFile) {
wfFileMaxWidth = len(line.wfFile)
}
if eventsMaxWidth < len(line.events) {
eventsMaxWidth = len(line.events)
} }
} }
} }
idMaxWidth += 2 jobIDMaxWidth += 2
jobNameMaxWidth += 2
stageMaxWidth += 2 stageMaxWidth += 2
nameMaxWidth += 2 wfNameMaxWidth += 2
wfFileMaxWidth += 2
fmt.Printf("%*s%*s%*s\n", -idMaxWidth, header.id, -stageMaxWidth, header.stage, -nameMaxWidth, header.name) fmt.Printf("%*s%*s%*s%*s%*s%*s\n",
-stageMaxWidth, header.stage,
-jobIDMaxWidth, header.jobID,
-jobNameMaxWidth, header.jobName,
-wfNameMaxWidth, header.wfName,
-wfFileMaxWidth, header.wfFile,
-eventsMaxWidth, header.events,
)
for _, line := range lineInfos { for _, line := range lineInfos {
fmt.Printf("%*s%*s%*s\n", -idMaxWidth, line.id, -stageMaxWidth, line.stage, -nameMaxWidth, line.name) fmt.Printf("%*s%*s%*s%*s%*s%*s\n",
-stageMaxWidth, line.stage,
-jobIDMaxWidth, line.jobID,
-jobNameMaxWidth, line.jobName,
-wfNameMaxWidth, line.wfName,
-wfFileMaxWidth, line.wfFile,
-eventsMaxWidth, line.events,
)
} }
if duplicateJobIDs { if duplicateJobIDs {
fmt.Print("\nDetected multiple jobs with the same job name, use `-W` to specify the path to the specific workflow.\n") fmt.Print("\nDetected multiple jobs with the same job name, use `-W` to specify the path to the specific workflow.\n")

View file

@ -186,6 +186,7 @@ func NewWorkflowPlanner(path string, noWorkflowRecurse bool) (WorkflowPlanner, e
return nil, err return nil, err
} }
workflow.File = wf.workflowFileInfo.Name()
if workflow.Name == "" { if workflow.Name == "" {
workflow.Name = wf.workflowFileInfo.Name() workflow.Name = wf.workflowFileInfo.Name()
} }

View file

@ -15,6 +15,7 @@ import (
// Workflow is the structure of the files in .github/workflows // Workflow is the structure of the files in .github/workflows
type Workflow struct { type Workflow struct {
File string
Name string `yaml:"name"` Name string `yaml:"name"`
RawOn yaml.Node `yaml:"on"` RawOn yaml.Node `yaml:"on"`
Env map[string]string `yaml:"env"` Env map[string]string `yaml:"env"`