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

     1  package flaghelpers
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/jessevdk/go-flags"
     9  
    10  	"github.com/pf-qiu/concourse/v6/atc"
    11  	"github.com/pf-qiu/concourse/v6/fly/rc"
    12  )
    13  
    14  type JobFlag struct {
    15  	PipelineRef atc.PipelineRef
    16  	JobName     string
    17  }
    18  
    19  func (flag JobFlag) String() string {
    20  	return fmt.Sprintf("%s/%s", flag.PipelineRef, flag.JobName)
    21  }
    22  
    23  func (flag *JobFlag) UnmarshalFlag(value string) error {
    24  	flag.PipelineRef = atc.PipelineRef{}
    25  
    26  	jobNameIdx := strings.LastIndex(value, "/")
    27  	if jobNameIdx == -1 {
    28  		return errors.New("argument format should be <pipeline>/<job>")
    29  	}
    30  
    31  	flag.JobName = value[jobNameIdx+1:]
    32  	if flag.JobName == "" {
    33  		return errors.New("argument format should be <pipeline>/<job>")
    34  	}
    35  
    36  	vs := strings.SplitN(value[:jobNameIdx], "/", 2)
    37  	flag.PipelineRef.Name = vs[0]
    38  	if len(vs) == 2 {
    39  		var err error
    40  		flag.PipelineRef.InstanceVars, err = unmarshalInstanceVars(vs[1])
    41  		if err != nil {
    42  			return errors.New(err.Error() + "/<job>")
    43  		}
    44  	}
    45  
    46  	return nil
    47  }
    48  
    49  func (flag *JobFlag) Complete(match string) []flags.Completion {
    50  	fly := parseFlags()
    51  
    52  	target, err := rc.LoadTarget(fly.Target, false)
    53  	if err != nil {
    54  		return []flags.Completion{}
    55  	}
    56  
    57  	err = target.Validate()
    58  	if err != nil {
    59  		return []flags.Completion{}
    60  	}
    61  
    62  	team := target.Team()
    63  	var comps []flags.Completion
    64  	vs := strings.SplitN(match, "/", 3)
    65  
    66  	if len(vs) == 1 {
    67  		pipelines, err := team.ListPipelines()
    68  		if err != nil {
    69  			return comps
    70  		}
    71  
    72  		for _, pipeline := range pipelines {
    73  			if strings.HasPrefix(pipeline.Name, vs[0]) {
    74  				comps = append(comps, flags.Completion{Item: pipeline.Name + "/"})
    75  			}
    76  		}
    77  	} else if len(vs) == 2 {
    78  		pipelines, err := team.ListPipelines()
    79  		if err != nil {
    80  			return comps
    81  		}
    82  
    83  		pipelineRef, err := parsePipelineRef(vs[0], vs[1])
    84  		if err == nil {
    85  			for _, pipeline := range pipelines {
    86  				if strings.HasPrefix(pipeline.Ref().String(), pipelineRef.String()) {
    87  					comps = append(comps, flags.Completion{Item: pipeline.Ref().String() + "/"})
    88  				}
    89  			}
    90  		} else {
    91  			pipelineRef := atc.PipelineRef{Name: vs[0]}
    92  			jobs, err := team.ListJobs(pipelineRef)
    93  			if err != nil {
    94  				return comps
    95  			}
    96  			for _, job := range jobs {
    97  				if strings.HasPrefix(job.Name, vs[1]) {
    98  					comps = append(comps, flags.Completion{Item: fmt.Sprintf("%s/%s", pipelineRef.String(), job.Name)})
    99  				}
   100  			}
   101  		}
   102  	} else if len(vs) == 3 {
   103  		pipelineRef, err := parsePipelineRef(vs[0], vs[1])
   104  		if err != nil {
   105  			return comps
   106  		}
   107  		jobs, err := team.ListJobs(pipelineRef)
   108  		if err != nil {
   109  			return comps
   110  		}
   111  		for _, job := range jobs {
   112  			if strings.HasPrefix(job.Name, vs[2]) {
   113  				comps = append(comps, flags.Completion{Item: fmt.Sprintf("%s/%s", pipelineRef.String(), job.Name)})
   114  			}
   115  		}
   116  	}
   117  
   118  	return comps
   119  }
   120  
   121  func parsePipelineRef(pipelineName, rawInstanceVars string) (atc.PipelineRef, error) {
   122  	instanceVars, err := unmarshalInstanceVars(rawInstanceVars)
   123  	if err != nil {
   124  		return atc.PipelineRef{}, err
   125  	}
   126  	return atc.PipelineRef{Name: pipelineName, InstanceVars: instanceVars}, nil
   127  }