github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/domain/infra/abi/parse/parse.go (about)

     1  package parse
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/containers/podman/v2/libpod"
     8  	"github.com/containers/podman/v2/libpod/define"
     9  	"github.com/pkg/errors"
    10  	"github.com/sirupsen/logrus"
    11  )
    12  
    13  // Handle volume options from CLI.
    14  // Parse "o" option to find UID, GID.
    15  func VolumeOptions(opts map[string]string) ([]libpod.VolumeCreateOption, error) {
    16  	libpodOptions := []libpod.VolumeCreateOption{}
    17  	volumeOptions := make(map[string]string)
    18  
    19  	for key, value := range opts {
    20  		switch key {
    21  		case "o":
    22  			// o has special handling to parse out UID, GID.
    23  			// These are separate Libpod options.
    24  			splitVal := strings.Split(value, ",")
    25  			finalVal := []string{}
    26  			for _, o := range splitVal {
    27  				// Options will be formatted as either "opt" or
    28  				// "opt=value"
    29  				splitO := strings.SplitN(o, "=", 2)
    30  				switch strings.ToLower(splitO[0]) {
    31  				case "uid":
    32  					if len(splitO) != 2 {
    33  						return nil, errors.Wrapf(define.ErrInvalidArg, "uid option must provide a UID")
    34  					}
    35  					intUID, err := strconv.Atoi(splitO[1])
    36  					if err != nil {
    37  						return nil, errors.Wrapf(err, "cannot convert UID %s to integer", splitO[1])
    38  					}
    39  					logrus.Debugf("Removing uid= from options and adding WithVolumeUID for UID %d", intUID)
    40  					libpodOptions = append(libpodOptions, libpod.WithVolumeUID(intUID))
    41  				case "gid":
    42  					if len(splitO) != 2 {
    43  						return nil, errors.Wrapf(define.ErrInvalidArg, "gid option must provide a GID")
    44  					}
    45  					intGID, err := strconv.Atoi(splitO[1])
    46  					if err != nil {
    47  						return nil, errors.Wrapf(err, "cannot convert GID %s to integer", splitO[1])
    48  					}
    49  					logrus.Debugf("Removing gid= from options and adding WithVolumeGID for GID %d", intGID)
    50  					libpodOptions = append(libpodOptions, libpod.WithVolumeGID(intGID))
    51  				default:
    52  					finalVal = append(finalVal, o)
    53  				}
    54  			}
    55  			if len(finalVal) > 0 {
    56  				volumeOptions[key] = strings.Join(finalVal, ",")
    57  			}
    58  		default:
    59  			volumeOptions[key] = value
    60  		}
    61  	}
    62  
    63  	if len(volumeOptions) > 0 {
    64  		libpodOptions = append(libpodOptions, libpod.WithVolumeOptions(volumeOptions))
    65  	}
    66  
    67  	return libpodOptions, nil
    68  }