summaryrefslogtreecommitdiff
path: root/pkg/domain/infra/abi/parse/parse.go
blob: 5a75e12167912a3a9bc70e751e5f85dd0dbb3b15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package parse

import (
	"strconv"
	"strings"

	"github.com/containers/podman/v3/libpod"
	"github.com/containers/podman/v3/libpod/define"
	units "github.com/docker/go-units"
	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"
)

// Handle volume options from CLI.
// Parse "o" option to find UID, GID, Size.
func VolumeOptions(opts map[string]string) ([]libpod.VolumeCreateOption, error) {
	libpodOptions := []libpod.VolumeCreateOption{}
	volumeOptions := make(map[string]string)

	for key, value := range opts {
		switch key {
		case "o":
			// o has special handling to parse out UID, GID.
			// These are separate Libpod options.
			splitVal := strings.Split(value, ",")
			finalVal := []string{}
			for _, o := range splitVal {
				// Options will be formatted as either "opt" or
				// "opt=value"
				splitO := strings.SplitN(o, "=", 2)
				switch strings.ToLower(splitO[0]) {
				case "size":
					size, err := units.FromHumanSize(splitO[1])
					if err != nil {
						return nil, errors.Wrapf(err, "cannot convert size %s to integer", splitO[1])
					}
					libpodOptions = append(libpodOptions, libpod.WithVolumeSize(uint64(size)))
					finalVal = append(finalVal, o)
					// set option "SIZE": "$size"
					volumeOptions["SIZE"] = splitO[1]
				case "inodes":
					inodes, err := strconv.ParseUint(splitO[1], 10, 64)
					if err != nil {
						return nil, errors.Wrapf(err, "cannot convert inodes %s to integer", splitO[1])
					}
					libpodOptions = append(libpodOptions, libpod.WithVolumeInodes(uint64(inodes)))
					finalVal = append(finalVal, o)
					// set option "INODES": "$size"
					volumeOptions["INODES"] = splitO[1]
				case "uid":
					if len(splitO) != 2 {
						return nil, errors.Wrapf(define.ErrInvalidArg, "uid option must provide a UID")
					}
					intUID, err := strconv.Atoi(splitO[1])
					if err != nil {
						return nil, errors.Wrapf(err, "cannot convert UID %s to integer", splitO[1])
					}
					logrus.Debugf("Removing uid= from options and adding WithVolumeUID for UID %d", intUID)
					libpodOptions = append(libpodOptions, libpod.WithVolumeUID(intUID), libpod.WithVolumeNoChown())
					finalVal = append(finalVal, o)
					// set option "UID": "$uid"
					volumeOptions["UID"] = splitO[1]
				case "gid":
					if len(splitO) != 2 {
						return nil, errors.Wrapf(define.ErrInvalidArg, "gid option must provide a GID")
					}
					intGID, err := strconv.Atoi(splitO[1])
					if err != nil {
						return nil, errors.Wrapf(err, "cannot convert GID %s to integer", splitO[1])
					}
					logrus.Debugf("Removing gid= from options and adding WithVolumeGID for GID %d", intGID)
					libpodOptions = append(libpodOptions, libpod.WithVolumeGID(intGID), libpod.WithVolumeNoChown())
					finalVal = append(finalVal, o)
					// set option "GID": "$gid"
					volumeOptions["GID"] = splitO[1]
				default:
					finalVal = append(finalVal, o)
				}
			}
			if len(finalVal) > 0 {
				volumeOptions[key] = strings.Join(finalVal, ",")
			}
		default:
			volumeOptions[key] = value
		}
	}

	if len(volumeOptions) > 0 {
		libpodOptions = append(libpodOptions, libpod.WithVolumeOptions(volumeOptions))
	}

	return libpodOptions, nil
}