github.com/docker/docker-ce@v17.12.1-ce-rc2+incompatible/components/cli/opts/config.go (about) 1 package opts 2 3 import ( 4 "encoding/csv" 5 "fmt" 6 "os" 7 "strconv" 8 "strings" 9 10 swarmtypes "github.com/docker/docker/api/types/swarm" 11 ) 12 13 // ConfigOpt is a Value type for parsing configs 14 type ConfigOpt struct { 15 values []*swarmtypes.ConfigReference 16 } 17 18 // Set a new config value 19 func (o *ConfigOpt) Set(value string) error { 20 csvReader := csv.NewReader(strings.NewReader(value)) 21 fields, err := csvReader.Read() 22 if err != nil { 23 return err 24 } 25 26 options := &swarmtypes.ConfigReference{ 27 File: &swarmtypes.ConfigReferenceFileTarget{ 28 UID: "0", 29 GID: "0", 30 Mode: 0444, 31 }, 32 } 33 34 // support a simple syntax of --config foo 35 if len(fields) == 1 { 36 options.File.Name = fields[0] 37 options.ConfigName = fields[0] 38 o.values = append(o.values, options) 39 return nil 40 } 41 42 for _, field := range fields { 43 parts := strings.SplitN(field, "=", 2) 44 key := strings.ToLower(parts[0]) 45 46 if len(parts) != 2 { 47 return fmt.Errorf("invalid field '%s' must be a key=value pair", field) 48 } 49 50 value := parts[1] 51 switch key { 52 case "source", "src": 53 options.ConfigName = value 54 case "target": 55 options.File.Name = value 56 case "uid": 57 options.File.UID = value 58 case "gid": 59 options.File.GID = value 60 case "mode": 61 m, err := strconv.ParseUint(value, 0, 32) 62 if err != nil { 63 return fmt.Errorf("invalid mode specified: %v", err) 64 } 65 66 options.File.Mode = os.FileMode(m) 67 default: 68 return fmt.Errorf("invalid field in config request: %s", key) 69 } 70 } 71 72 if options.ConfigName == "" { 73 return fmt.Errorf("source is required") 74 } 75 76 o.values = append(o.values, options) 77 return nil 78 } 79 80 // Type returns the type of this option 81 func (o *ConfigOpt) Type() string { 82 return "config" 83 } 84 85 // String returns a string repr of this option 86 func (o *ConfigOpt) String() string { 87 configs := []string{} 88 for _, config := range o.values { 89 repr := fmt.Sprintf("%s -> %s", config.ConfigName, config.File.Name) 90 configs = append(configs, repr) 91 } 92 return strings.Join(configs, ", ") 93 } 94 95 // Value returns the config requests 96 func (o *ConfigOpt) Value() []*swarmtypes.ConfigReference { 97 return o.values 98 }