Skip to content

Commit

Permalink
Merge pull request #11 from cameronbraid/flags-as-env
Browse files Browse the repository at this point in the history
Co-authored-by: Cameron Braid <[email protected]>
use env vars instead of flags for rclone arguments
  • Loading branch information
Jancis authored Aug 28, 2023
2 parents 50805cc + 99c8ceb commit 9857f39
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.3.2
v1.3.3
18 changes: 15 additions & 3 deletions pkg/rclone/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ func getSecret(secretName string) (*v1.Secret, error) {
return secret, nil
}

func flagToEnvName(flag string) string {
// To find the name of the environment variable, first, take the long option name, strip the leading --, change - to _, make upper case and prepend RCLONE_.
flag = strings.TrimPrefix(flag, "--") // we dont pass prefixed args, but strictly this is the algorithm
flag = strings.ReplaceAll(flag, "-", "_")
flag = strings.ToUpper(flag)
return fmt.Sprintf("RCLONE_%s", flag)
}

// Mount routine.
func Mount(remote string, remotePath string, targetPath string, configData string, flags map[string]string) error {
mountCmd := "rclone"
Expand Down Expand Up @@ -281,17 +289,19 @@ func Mount(remote string, remotePath string, targetPath string, configData strin
mountArgs = append(mountArgs, "--config", configFile.Name())
}

env := os.Environ()

// Add default flags
for k, v := range defaultFlags {
// Exclude overriden flags
if _, ok := flags[k]; !ok {
mountArgs = append(mountArgs, fmt.Sprintf("--%s=%s", k, v))
env = append(env, fmt.Sprintf("%s=%s", flagToEnvName(k), v))
}
}

// Add user supplied flags
for k, v := range flags {
mountArgs = append(mountArgs, fmt.Sprintf("--%s=%s", k, v))
env = append(env, fmt.Sprintf("%s=%s", flagToEnvName(k), v))
}

// create target, os.Mkdirall is noop if it exists
Expand All @@ -302,7 +312,9 @@ func Mount(remote string, remotePath string, targetPath string, configData strin

klog.Infof("executing mount command cmd=%s, remote=%s, targetpath=%s", mountCmd, remoteWithPath, targetPath)

out, err := exec.Command(mountCmd, mountArgs...).CombinedOutput()
cmd := exec.Command(mountCmd, mountArgs...)
cmd.Env = env
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("mounting failed: %v cmd: '%s' remote: '%s' targetpath: %s output: %q",
err, mountCmd, remoteWithPath, targetPath, string(out))
Expand Down

0 comments on commit 9857f39

Please sign in to comment.