Browse Source

Merge pull request #2099 from danielflira/mount-helper

fix parameter multiple values
pull/2104/head
Chris Lu 4 years ago
committed by GitHub
parent
commit
ae185b997f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 277
      weed/command/fuse.go

277
weed/command/fuse.go

@ -12,124 +12,178 @@ func init() {
cmdFuse.Run = runFuse // break init cycle cmdFuse.Run = runFuse // break init cycle
} }
func runFuse(cmd *Command, args []string) bool { type parameter struct {
argsLen := len(args) name string
options := []string{} value string
}
// at least target mount path should be passed func runFuse(cmd *Command, args []string) bool {
if argsLen < 1 { rawArgs := strings.Join(args, " ")
return false rawArgsLen := len(rawArgs)
option := strings.Builder{}
options := []parameter{}
// first parameter
i := 0
for i = 0; i < rawArgsLen && rawArgs[i] != ' '; i++ {
option.WriteByte(rawArgs[i])
} }
options = append(options, parameter{"arg0", option.String()})
option.Reset()
for i++; i < rawArgsLen; i++ {
// space separator check for filled option
if rawArgs[i] == ' ' {
if option.Len() > 0 {
options = append(options, parameter{option.String(), "true"})
option.Reset()
}
// dash separator read option until next space
} else if rawArgs[i] == '-' {
for i++; i < rawArgsLen && rawArgs[i] != ' '; i++ {
option.WriteByte(rawArgs[i])
}
options = append(options, parameter{option.String(), "true"})
option.Reset()
// equal separator start option with pending value
} else if rawArgs[i] == '=' {
name := option.String()
option.Reset()
for i++; i < rawArgsLen && rawArgs[i] != ','; i++ {
// double quote separator read option until next double quote
if rawArgs[i] == '"' {
for i++; i < rawArgsLen && rawArgs[i] != '"'; i++ {
option.WriteByte(rawArgs[i])
}
// single quote separator read option until next single quote
} else if rawArgs[i] == '\'' {
for i++; i < rawArgsLen && rawArgs[i] != '\''; i++ {
option.WriteByte(rawArgs[i])
}
// add chars before comma
} else if rawArgs[i] != ' ' {
option.WriteByte(rawArgs[i])
}
}
// first option is always target mount path options = append(options, parameter{name, option.String()})
mountOptions.dir = &args[0] option.Reset()
// scan parameters looking for one or more -o options // comma separator just read current option
// -o options receive parameters on format key=value[,key=value]... } else if rawArgs[i] == ',' {
for i := 0; i < argsLen; i++ { options = append(options, parameter{option.String(), "true"})
if args[i] == "-o" && i+1 <= argsLen { option.Reset()
options = strings.Split(args[i+1], ",") // what is not a separator fill option buffer
i++ } else {
option.WriteByte(rawArgs[i])
} }
} }
// for each option passed with -o // get residual option data
for _, option := range options { if option.Len() > 0 {
// split just first = character // add value to pending option
parts := strings.SplitN(option, "=", 2) options = append(options, parameter{option.String(), "true"})
option.Reset()
// if doesn't key and value skip }
if len(parts) != 2 {
continue
}
key, value := parts[0], parts[1] // scan each parameter
for i := 0; i < len(options); i++ {
// switch key keeping "weed mount" parameters parameter := options[i]
switch key { switch parameter.name {
case "filer": case "arg0":
mountOptions.filer = &value mountOptions.dir = &parameter.value
case "filer.path": case "filer":
mountOptions.filerMountRootPath = &value mountOptions.filer = &parameter.value
case "dirAutoCreate": case "filer.path":
if parsed, err := strconv.ParseBool(value); err != nil { mountOptions.filerMountRootPath = &parameter.value
mountOptions.dirAutoCreate = &parsed case "dirAutoCreate":
} else { if parsed, err := strconv.ParseBool(parameter.value); err != nil {
panic(fmt.Errorf("dirAutoCreate: %s", err)) mountOptions.dirAutoCreate = &parsed
} } else {
case "collection": panic(fmt.Errorf("dirAutoCreate: %s", err))
mountOptions.collection = &value }
case "replication": case "collection":
mountOptions.replication = &value mountOptions.collection = &parameter.value
case "disk": case "replication":
mountOptions.diskType = &value mountOptions.replication = &parameter.value
case "ttl": case "disk":
if parsed, err := strconv.ParseInt(value, 0, 32); err != nil { mountOptions.diskType = &parameter.value
intValue := int(parsed) case "ttl":
mountOptions.ttlSec = &intValue if parsed, err := strconv.ParseInt(parameter.value, 0, 32); err != nil {
} else { intValue := int(parsed)
panic(fmt.Errorf("ttl: %s", err)) mountOptions.ttlSec = &intValue
} } else {
case "chunkSizeLimitMB": panic(fmt.Errorf("ttl: %s", err))
if parsed, err := strconv.ParseInt(value, 0, 32); err != nil { }
intValue := int(parsed) case "chunkSizeLimitMB":
mountOptions.chunkSizeLimitMB = &intValue if parsed, err := strconv.ParseInt(parameter.value, 0, 32); err != nil {
} else { intValue := int(parsed)
panic(fmt.Errorf("chunkSizeLimitMB: %s", err)) mountOptions.chunkSizeLimitMB = &intValue
} } else {
case "concurrentWriters": panic(fmt.Errorf("chunkSizeLimitMB: %s", err))
if parsed, err := strconv.ParseInt(value, 0, 32); err != nil { }
intValue := int(parsed) case "concurrentWriters":
mountOptions.concurrentWriters = &intValue i++
} else { if parsed, err := strconv.ParseInt(parameter.value, 0, 32); err != nil {
panic(fmt.Errorf("concurrentWriters: %s", err)) intValue := int(parsed)
} mountOptions.concurrentWriters = &intValue
case "cacheDir": } else {
mountOptions.cacheDir = &value panic(fmt.Errorf("concurrentWriters: %s", err))
case "cacheCapacityMB": }
if parsed, err := strconv.ParseInt(value, 0, 64); err != nil { case "cacheDir":
mountOptions.cacheSizeMB = &parsed mountOptions.cacheDir = &parameter.value
} else { case "cacheCapacityMB":
panic(fmt.Errorf("cacheCapacityMB: %s", err)) if parsed, err := strconv.ParseInt(parameter.value, 0, 64); err != nil {
} mountOptions.cacheSizeMB = &parsed
case "dataCenter": } else {
mountOptions.dataCenter = &value panic(fmt.Errorf("cacheCapacityMB: %s", err))
case "allowOthers": }
if parsed, err := strconv.ParseBool(value); err != nil { case "dataCenter":
mountOptions.allowOthers = &parsed mountOptions.dataCenter = &parameter.value
} else { case "allowOthers":
panic(fmt.Errorf("allowOthers: %s", err)) if parsed, err := strconv.ParseBool(parameter.value); err != nil {
} mountOptions.allowOthers = &parsed
case "umask": } else {
mountOptions.umaskString = &value panic(fmt.Errorf("allowOthers: %s", err))
case "nonempty": }
if parsed, err := strconv.ParseBool(value); err != nil { case "umask":
mountOptions.nonempty = &parsed mountOptions.umaskString = &parameter.value
} else { case "nonempty":
panic(fmt.Errorf("nonempty: %s", err)) if parsed, err := strconv.ParseBool(parameter.value); err != nil {
} mountOptions.nonempty = &parsed
case "volumeServerAccess": } else {
mountOptions.volumeServerAccess = &value panic(fmt.Errorf("nonempty: %s", err))
case "map.uid": }
mountOptions.uidMap = &value case "volumeServerAccess":
case "map.gid": mountOptions.volumeServerAccess = &parameter.value
mountOptions.gidMap = &value case "map.uid":
case "readOnly": mountOptions.uidMap = &parameter.value
if parsed, err := strconv.ParseBool(value); err != nil { case "map.gid":
mountOptions.readOnly = &parsed mountOptions.gidMap = &parameter.value
} else { case "readOnly":
panic(fmt.Errorf("readOnly: %s", err)) if parsed, err := strconv.ParseBool(parameter.value); err != nil {
} mountOptions.readOnly = &parsed
case "cpuprofile": } else {
mountCpuProfile = &value panic(fmt.Errorf("readOnly: %s", err))
case "memprofile": }
mountMemProfile = &value case "cpuprofile":
case "readRetryTime": mountCpuProfile = &parameter.value
if parsed, err := time.ParseDuration(value); err != nil { case "memprofile":
mountReadRetryTime = &parsed mountMemProfile = &parameter.value
} else { case "readRetryTime":
panic(fmt.Errorf("readRetryTime: %s", err)) if parsed, err := time.ParseDuration(parameter.value); err != nil {
} mountReadRetryTime = &parsed
} else {
panic(fmt.Errorf("readRetryTime: %s", err))
}
} }
} }
@ -160,6 +214,9 @@ var cmdFuse = &Command{
mount -t fuse./home/user/bin/weed fuse /mnt -o "filer=localhost:8888,filer.path=/" mount -t fuse./home/user/bin/weed fuse /mnt -o "filer=localhost:8888,filer.path=/"
mount -t fuse "/home/user/bin/weed#fuse" /mnt -o "filer=localhost:8888,filer.path=/" mount -t fuse "/home/user/bin/weed#fuse" /mnt -o "filer=localhost:8888,filer.path=/"
To pass more than one parameter use quotes, example:
mount -t weed fuse /mnt -o "filer='192.168.0.1:8888,192.168.0.2:8888',filer.path=/"
To check valid options look "weed mount --help" To check valid options look "weed mount --help"
`, `,
} }
|||||||
100:0
Loading…
Cancel
Save