github.com/pdmccormick/importable-docker-buildx@v0.0.0-20240426161518-e47091289030/util/buildflags/printfunc.go (about)

     1  package buildflags
     2  
     3  import (
     4  	"encoding/csv"
     5  	"strconv"
     6  	"strings"
     7  
     8  	controllerapi "github.com/docker/buildx/controller/pb"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  func ParsePrintFunc(str string) (*controllerapi.PrintFunc, error) {
    13  	if str == "" {
    14  		return nil, nil
    15  	}
    16  	csvReader := csv.NewReader(strings.NewReader(str))
    17  	fields, err := csvReader.Read()
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	f := &controllerapi.PrintFunc{}
    22  	for _, field := range fields {
    23  		parts := strings.SplitN(field, "=", 2)
    24  		if len(parts) == 2 {
    25  			switch parts[0] {
    26  			case "format":
    27  				f.Format = parts[1]
    28  			case "ignorestatus":
    29  				v, err := strconv.ParseBool(parts[1])
    30  				if err != nil {
    31  					return nil, errors.Wrapf(err, "invalid ignorestatus print value: %s", parts[1])
    32  				}
    33  				f.IgnoreStatus = v
    34  			default:
    35  				return nil, errors.Errorf("invalid print field: %s", field)
    36  			}
    37  		} else {
    38  			if f.Name != "" {
    39  				return nil, errors.Errorf("invalid print value: %s", str)
    40  			}
    41  			f.Name = field
    42  		}
    43  	}
    44  	return f, nil
    45  }