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

     1  package flaghelpers
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"strings"
     7  )
     8  
     9  type InputPairFlag struct {
    10  	Name string
    11  	Path string
    12  }
    13  
    14  func (pair *InputPairFlag) UnmarshalFlag(value string) error {
    15  	name, path, ok := parseKeyValuePair(value)
    16  	if !ok {
    17  		return fmt.Errorf("invalid input pair '%s' (must be name=path)", value)
    18  	}
    19  
    20  	matches, err := filepath.Glob(path)
    21  	if err != nil {
    22  		return fmt.Errorf("failed to expand path '%s': %s", path, err)
    23  	}
    24  
    25  	if len(matches) == 0 {
    26  		return fmt.Errorf("path '%s' does not exist", path)
    27  	}
    28  
    29  	if len(matches) > 1 {
    30  		return fmt.Errorf("path '%s' resolves to multiple entries: %s", path, strings.Join(matches, ", "))
    31  	}
    32  
    33  	pair.Name = name
    34  	pair.Path = matches[0]
    35  
    36  	return nil
    37  }