vfs,mount,cmount,mountlib: allow flags to be overriden by environment variables

This commit is contained in:
Nick Craig-Wood 2017-11-07 17:09:08 +00:00
parent 05a1e1532b
commit 8951875c21
6 changed files with 104 additions and 28 deletions

View File

@ -78,10 +78,10 @@ func mountOptions(device string, mountpoint string) (options []string) {
if mountlib.WritebackCache {
// FIXME? options = append(options, "-o", WritebackCache())
}
for _, option := range *mountlib.ExtraOptions {
for _, option := range mountlib.ExtraOptions {
options = append(options, "-o", option)
}
for _, option := range *mountlib.ExtraFlags {
for _, option := range mountlib.ExtraFlags {
options = append(options, option)
}
return options

View File

@ -57,10 +57,10 @@ func mountOptions(device string) (options []fuse.MountOption) {
if mountlib.WritebackCache {
options = append(options, fuse.WritebackCache())
}
if len(*mountlib.ExtraOptions) > 0 {
if len(mountlib.ExtraOptions) > 0 {
fs.Errorf(nil, "-o/--option not supported with this FUSE backend")
}
if len(*mountlib.ExtraOptions) > 0 {
if len(mountlib.ExtraOptions) > 0 {
fs.Errorf(nil, "--fuse-flag not supported with this FUSE backend")
}
return options

View File

@ -23,8 +23,8 @@ var (
DefaultPermissions = false
WritebackCache = false
MaxReadAhead fs.SizeSuffix = 128 * 1024
ExtraOptions *[]string
ExtraFlags *[]string
ExtraOptions []string
ExtraFlags []string
)
// Check is folder is empty
@ -182,17 +182,17 @@ will see all files and folders immediately in this mode.
// Add flags
flags := commandDefintion.Flags()
flags.BoolVarP(&DebugFUSE, "debug-fuse", "", DebugFUSE, "Debug the FUSE internals - needs -v.")
fs.BoolVarP(flags, &DebugFUSE, "debug-fuse", "", DebugFUSE, "Debug the FUSE internals - needs -v.")
// mount options
flags.BoolVarP(&AllowNonEmpty, "allow-non-empty", "", AllowNonEmpty, "Allow mounting over a non-empty directory.")
flags.BoolVarP(&AllowRoot, "allow-root", "", AllowRoot, "Allow access to root user.")
flags.BoolVarP(&AllowOther, "allow-other", "", AllowOther, "Allow access to other users.")
flags.BoolVarP(&DefaultPermissions, "default-permissions", "", DefaultPermissions, "Makes kernel enforce access control based on the file mode.")
flags.BoolVarP(&WritebackCache, "write-back-cache", "", WritebackCache, "Makes kernel buffer writes before sending them to rclone. Without this, writethrough caching is used.")
flags.VarP(&MaxReadAhead, "max-read-ahead", "", "The number of bytes that can be prefetched for sequential reads.")
ExtraOptions = flags.StringArrayP("option", "o", []string{}, "Option for libfuse/WinFsp. Repeat if required.")
ExtraFlags = flags.StringArrayP("fuse-flag", "", []string{}, "Flags or arguments to be passed direct to libfuse/WinFsp. Repeat if required.")
//flags.BoolVarP(&foreground, "foreground", "", foreground, "Do not detach.")
fs.BoolVarP(flags, &AllowNonEmpty, "allow-non-empty", "", AllowNonEmpty, "Allow mounting over a non-empty directory.")
fs.BoolVarP(flags, &AllowRoot, "allow-root", "", AllowRoot, "Allow access to root user.")
fs.BoolVarP(flags, &AllowOther, "allow-other", "", AllowOther, "Allow access to other users.")
fs.BoolVarP(flags, &DefaultPermissions, "default-permissions", "", DefaultPermissions, "Makes kernel enforce access control based on the file mode.")
fs.BoolVarP(flags, &WritebackCache, "write-back-cache", "", WritebackCache, "Makes kernel buffer writes before sending them to rclone. Without this, writethrough caching is used.")
fs.FlagsVarP(flags, &MaxReadAhead, "max-read-ahead", "", "The number of bytes that can be prefetched for sequential reads.")
fs.StringArrayVarP(flags, &ExtraOptions, "option", "o", []string{}, "Option for libfuse/WinFsp. Repeat if required.")
fs.StringArrayVarP(flags, &ExtraFlags, "fuse-flag", "", []string{}, "Flags or arguments to be passed direct to libfuse/WinFsp. Repeat if required.")
//fs.BoolVarP(flags, &foreground, "foreground", "", foreground, "Do not detach.")
// Add in the generic flags
vfsflags.AddFlags(flags)

View File

@ -268,6 +268,14 @@ func StringP(name, shorthand string, value string, usage string) (out *string) {
return out
}
// StringVarP defines a flag which can be overridden by an environment variable
//
// It is a thin wrapper around pflag.StringVarP
func StringVarP(flags *pflag.FlagSet, p *string, name, shorthand string, value string, usage string) {
flags.StringVarP(p, name, shorthand, value, usage)
setDefaultFromEnv(name)
}
// BoolP defines a flag which can be overridden by an environment variable
//
// It is a thin wrapper around pflag.BoolP
@ -277,6 +285,14 @@ func BoolP(name, shorthand string, value bool, usage string) (out *bool) {
return out
}
// BoolVarP defines a flag which can be overridden by an environment variable
//
// It is a thin wrapper around pflag.BoolVarP
func BoolVarP(flags *pflag.FlagSet, p *bool, name, shorthand string, value bool, usage string) {
flags.BoolVarP(p, name, shorthand, value, usage)
setDefaultFromEnv(name)
}
// IntP defines a flag which can be overridden by an environment variable
//
// It is a thin wrapper around pflag.IntP
@ -286,6 +302,22 @@ func IntP(name, shorthand string, value int, usage string) (out *int) {
return out
}
// IntVarP defines a flag which can be overridden by an environment variable
//
// It is a thin wrapper around pflag.IntVarP
func IntVarP(flags *pflag.FlagSet, p *int, name, shorthand string, value int, usage string) {
flags.IntVarP(p, name, shorthand, value, usage)
setDefaultFromEnv(name)
}
// Uint32VarP defines a flag which can be overridden by an environment variable
//
// It is a thin wrapper around pflag.Uint32VarP
func Uint32VarP(flags *pflag.FlagSet, p *uint32, name, shorthand string, value uint32, usage string) {
flags.Uint32VarP(p, name, shorthand, value, usage)
setDefaultFromEnv(name)
}
// Float64P defines a flag which can be overridden by an environment variable
//
// It is a thin wrapper around pflag.Float64P
@ -295,6 +327,14 @@ func Float64P(name, shorthand string, value float64, usage string) (out *float64
return out
}
// Float64VarP defines a flag which can be overridden by an environment variable
//
// It is a thin wrapper around pflag.Float64VarP
func Float64VarP(flags *pflag.FlagSet, p *float64, name, shorthand string, value float64, usage string) {
flags.Float64VarP(p, name, shorthand, value, usage)
setDefaultFromEnv(name)
}
// DurationP defines a flag which can be overridden by an environment variable
//
// It is a thin wrapper around pflag.DurationP
@ -304,6 +344,14 @@ func DurationP(name, shorthand string, value time.Duration, usage string) (out *
return out
}
// DurationVarP defines a flag which can be overridden by an environment variable
//
// It is a thin wrapper around pflag.DurationVarP
func DurationVarP(flags *pflag.FlagSet, p *time.Duration, name, shorthand string, value time.Duration, usage string) {
flags.DurationVarP(p, name, shorthand, value, usage)
setDefaultFromEnv(name)
}
// VarP defines a flag which can be overridden by an environment variable
//
// It is a thin wrapper around pflag.VarP
@ -312,6 +360,14 @@ func VarP(value pflag.Value, name, shorthand, usage string) {
setDefaultFromEnv(name)
}
// FlagsVarP defines a flag which can be overridden by an environment variable
//
// It is a thin wrapper around pflag.VarP
func FlagsVarP(flags *pflag.FlagSet, value pflag.Value, name, shorthand, usage string) {
flags.VarP(value, name, shorthand, usage)
setDefaultFromEnv(name)
}
// StringArrayP defines a flag which can be overridden by an environment variable
//
// It sets one value only - command line flags can be used to set more.
@ -323,6 +379,16 @@ func StringArrayP(name, shorthand string, value []string, usage string) (out *[]
return out
}
// StringArrayVarP defines a flag which can be overridden by an environment variable
//
// It sets one value only - command line flags can be used to set more.
//
// It is a thin wrapper around pflag.StringArrayVarP
func StringArrayVarP(flags *pflag.FlagSet, p *[]string, name, shorthand string, value []string, usage string) {
flags.StringArrayVarP(p, name, shorthand, value, usage)
setDefaultFromEnv(name)
}
// CountP defines a flag which can be overridden by an environment variable
//
// It is a thin wrapper around pflag.CountP
@ -331,3 +397,11 @@ func CountP(name, shorthand string, usage string) (out *int) {
setDefaultFromEnv(name)
return out
}
// CountVarP defines a flag which can be overridden by an environment variable
//
// It is a thin wrapper around pflag.CountVarP
func CountVarP(flags *pflag.FlagSet, p *int, name, shorthand string, usage string) {
flags.CountVarP(p, name, shorthand, usage)
setDefaultFromEnv(name)
}

View File

@ -2,6 +2,7 @@
package vfsflags
import (
"github.com/ncw/rclone/fs"
"github.com/ncw/rclone/vfs"
"github.com/spf13/pflag"
)
@ -13,14 +14,14 @@ var (
// AddFlags adds the non filing system specific flags to the command
func AddFlags(flags *pflag.FlagSet) {
flags.BoolVarP(&Opt.NoModTime, "no-modtime", "", Opt.NoModTime, "Don't read/write the modification time (can speed things up).")
flags.BoolVarP(&Opt.NoChecksum, "no-checksum", "", Opt.NoChecksum, "Don't compare checksums on up/download.")
flags.BoolVarP(&Opt.NoSeek, "no-seek", "", Opt.NoSeek, "Don't allow seeking in files.")
flags.DurationVarP(&Opt.DirCacheTime, "dir-cache-time", "", Opt.DirCacheTime, "Time to cache directory entries for.")
flags.DurationVarP(&Opt.PollInterval, "poll-interval", "", Opt.PollInterval, "Time to wait between polling for changes. Must be smaller than dir-cache-time. Only on supported remotes. Set to 0 to disable.")
flags.BoolVarP(&Opt.ReadOnly, "read-only", "", Opt.ReadOnly, "Mount read-only.")
flags.VarP(&Opt.CacheMode, "cache-mode", "", "Cache mode off|minimal|writes|full")
flags.DurationVarP(&Opt.CachePollInterval, "cache-poll-interval", "", Opt.CachePollInterval, "Interval to poll the cache for stale objects.")
flags.DurationVarP(&Opt.CacheMaxAge, "cache-max-age", "", Opt.CacheMaxAge, "Max age of objects in the cache.")
fs.BoolVarP(flags, &Opt.NoModTime, "no-modtime", "", Opt.NoModTime, "Don't read/write the modification time (can speed things up).")
fs.BoolVarP(flags, &Opt.NoChecksum, "no-checksum", "", Opt.NoChecksum, "Don't compare checksums on up/download.")
fs.BoolVarP(flags, &Opt.NoSeek, "no-seek", "", Opt.NoSeek, "Don't allow seeking in files.")
fs.DurationVarP(flags, &Opt.DirCacheTime, "dir-cache-time", "", Opt.DirCacheTime, "Time to cache directory entries for.")
fs.DurationVarP(flags, &Opt.PollInterval, "poll-interval", "", Opt.PollInterval, "Time to wait between polling for changes. Must be smaller than dir-cache-time. Only on supported remotes. Set to 0 to disable.")
fs.BoolVarP(flags, &Opt.ReadOnly, "read-only", "", Opt.ReadOnly, "Mount read-only.")
fs.FlagsVarP(flags, &Opt.CacheMode, "cache-mode", "", "Cache mode off|minimal|writes|full")
fs.DurationVarP(flags, &Opt.CachePollInterval, "cache-poll-interval", "", Opt.CachePollInterval, "Interval to poll the cache for stale objects.")
fs.DurationVarP(flags, &Opt.CacheMaxAge, "cache-max-age", "", Opt.CacheMaxAge, "Max age of objects in the cache.")
platformFlags(flags)
}

View File

@ -3,17 +3,18 @@
package vfsflags
import (
"github.com/ncw/rclone/fs"
"github.com/spf13/pflag"
"golang.org/x/sys/unix"
)
// add any extra platform specific flags
func platformFlags(flags *pflag.FlagSet) {
flags.IntVarP(&Opt.Umask, "umask", "", Opt.Umask, "Override the permission bits set by the filesystem.")
fs.IntVarP(flags, &Opt.Umask, "umask", "", Opt.Umask, "Override the permission bits set by the filesystem.")
Opt.Umask = unix.Umask(0) // read the umask
unix.Umask(Opt.Umask) // set it back to what it was
Opt.UID = uint32(unix.Geteuid())
Opt.GID = uint32(unix.Getegid())
flags.Uint32VarP(&Opt.UID, "uid", "", Opt.UID, "Override the uid field set by the filesystem.")
flags.Uint32VarP(&Opt.GID, "gid", "", Opt.GID, "Override the gid field set by the filesystem.")
fs.Uint32VarP(flags, &Opt.UID, "uid", "", Opt.UID, "Override the uid field set by the filesystem.")
fs.Uint32VarP(flags, &Opt.GID, "gid", "", Opt.GID, "Override the gid field set by the filesystem.")
}