2017-03-16 13:39:18 -04:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/csv"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
swarmtypes "github.com/docker/docker/api/types/swarm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ConfigOpt is a Value type for parsing configs
|
|
|
|
type ConfigOpt struct {
|
|
|
|
values []*swarmtypes.ConfigReference
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a new config value
|
|
|
|
func (o *ConfigOpt) Set(value string) error {
|
|
|
|
csvReader := csv.NewReader(strings.NewReader(value))
|
|
|
|
fields, err := csvReader.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
options := &swarmtypes.ConfigReference{
|
|
|
|
File: &swarmtypes.ConfigReferenceFileTarget{
|
|
|
|
UID: "0",
|
|
|
|
GID: "0",
|
2022-09-30 13:13:22 -04:00
|
|
|
Mode: 0o444,
|
2017-03-16 13:39:18 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// support a simple syntax of --config foo
|
2020-01-07 06:28:28 -05:00
|
|
|
if len(fields) == 1 && !strings.Contains(fields[0], "=") {
|
2017-03-16 13:39:18 -04:00
|
|
|
options.File.Name = fields[0]
|
|
|
|
options.ConfigName = fields[0]
|
|
|
|
o.values = append(o.values, options)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, field := range fields {
|
2022-12-27 10:24:23 -05:00
|
|
|
key, val, ok := strings.Cut(field, "=")
|
|
|
|
if !ok || key == "" {
|
2017-03-16 13:39:18 -04:00
|
|
|
return fmt.Errorf("invalid field '%s' must be a key=value pair", field)
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:24:23 -05:00
|
|
|
// TODO(thaJeztah): these options should not be case-insensitive.
|
|
|
|
switch strings.ToLower(key) {
|
2017-03-16 13:39:18 -04:00
|
|
|
case "source", "src":
|
2022-12-27 10:24:23 -05:00
|
|
|
options.ConfigName = val
|
2017-03-16 13:39:18 -04:00
|
|
|
case "target":
|
2022-12-27 10:24:23 -05:00
|
|
|
options.File.Name = val
|
2017-03-16 13:39:18 -04:00
|
|
|
case "uid":
|
2022-12-27 10:24:23 -05:00
|
|
|
options.File.UID = val
|
2017-03-16 13:39:18 -04:00
|
|
|
case "gid":
|
2022-12-27 10:24:23 -05:00
|
|
|
options.File.GID = val
|
2017-03-16 13:39:18 -04:00
|
|
|
case "mode":
|
2022-12-27 10:24:23 -05:00
|
|
|
m, err := strconv.ParseUint(val, 0, 32)
|
2017-03-16 13:39:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid mode specified: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
options.File.Mode = os.FileMode(m)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid field in config request: %s", key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.ConfigName == "" {
|
|
|
|
return fmt.Errorf("source is required")
|
|
|
|
}
|
2020-01-07 08:11:53 -05:00
|
|
|
if options.File.Name == "" {
|
|
|
|
options.File.Name = options.ConfigName
|
|
|
|
}
|
2017-03-16 13:39:18 -04:00
|
|
|
|
|
|
|
o.values = append(o.values, options)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the type of this option
|
|
|
|
func (o *ConfigOpt) Type() string {
|
|
|
|
return "config"
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a string repr of this option
|
|
|
|
func (o *ConfigOpt) String() string {
|
|
|
|
configs := []string{}
|
|
|
|
for _, config := range o.values {
|
|
|
|
repr := fmt.Sprintf("%s -> %s", config.ConfigName, config.File.Name)
|
|
|
|
configs = append(configs, repr)
|
|
|
|
}
|
|
|
|
return strings.Join(configs, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the config requests
|
|
|
|
func (o *ConfigOpt) Value() []*swarmtypes.ConfigReference {
|
|
|
|
return o.values
|
|
|
|
}
|