83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/csv"
|
|
"fmt"
|
|
"strings"
|
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
|
)
|
|
|
|
// parseMountFlag parses a mount string in the form "type=foo,source=/path,destination=/target,options=rbind:rw"
|
|
func parseMountFlag(m string) (specs.Mount, error) {
|
|
mount := specs.Mount{}
|
|
r := csv.NewReader(strings.NewReader(m))
|
|
|
|
fields, err := r.Read()
|
|
if err != nil {
|
|
return mount, err
|
|
}
|
|
|
|
for _, field := range fields {
|
|
v := strings.Split(field, "=")
|
|
if len(v) != 2 {
|
|
return mount, fmt.Errorf("invalid mount specification: expected key=val")
|
|
}
|
|
|
|
key := v[0]
|
|
val := v[1]
|
|
switch key {
|
|
case "type":
|
|
mount.Type = val
|
|
case "source", "src":
|
|
mount.Source = val
|
|
case "destination", "dst":
|
|
mount.Destination = val
|
|
case "options":
|
|
mount.Options = strings.Split(val, ":")
|
|
default:
|
|
return mount, fmt.Errorf("mount option %q not supported", key)
|
|
}
|
|
}
|
|
|
|
return mount, nil
|
|
}
|
|
|
|
// replaceOrAppendEnvValues returns the defaults with the overrides either
|
|
// replaced by env key or appended to the list
|
|
func replaceOrAppendEnvValues(defaults, overrides []string) []string {
|
|
cache := make(map[string]int, len(defaults))
|
|
for i, e := range defaults {
|
|
parts := strings.SplitN(e, "=", 2)
|
|
cache[parts[0]] = i
|
|
}
|
|
|
|
for _, value := range overrides {
|
|
// Values w/o = means they want this env to be removed/unset.
|
|
if !strings.Contains(value, "=") {
|
|
if i, exists := cache[value]; exists {
|
|
defaults[i] = "" // Used to indicate it should be removed
|
|
}
|
|
continue
|
|
}
|
|
|
|
// Just do a normal set/update
|
|
parts := strings.SplitN(value, "=", 2)
|
|
if i, exists := cache[parts[0]]; exists {
|
|
defaults[i] = value
|
|
} else {
|
|
defaults = append(defaults, value)
|
|
}
|
|
}
|
|
|
|
// Now remove all entries that we want to "unset"
|
|
for i := 0; i < len(defaults); i++ {
|
|
if defaults[i] == "" {
|
|
defaults = append(defaults[:i], defaults[i+1:]...)
|
|
i--
|
|
}
|
|
}
|
|
|
|
return defaults
|
|
}
|