Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

Factor ShellExpand from sftp backend to lib/env

Nick Craig-Wood5c0e5b85f727
diff --git a/backend/sftp/sftp.go b/backend/sftp/sftp.go
index c124e1eae..efd5412bc 100644
--- a/backend/sftp/sftp.go
+++ b/backend/sftp/sftp.go
@@ -19,7 +19,6 @@ import (
"sync"
"time"
- homedir "github.com/mitchellh/go-homedir"
"github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/config"
"github.com/ncw/rclone/fs/config/configmap"
@@ -27,6 +26,7 @@ import (
"github.com/ncw/rclone/fs/config/obscure"
"github.com/ncw/rclone/fs/fshttp"
"github.com/ncw/rclone/fs/hash"
+ "github.com/ncw/rclone/lib/env"
"github.com/ncw/rclone/lib/readers"
"github.com/pkg/errors"
"github.com/pkg/sftp"
@@ -318,21 +318,6 @@ func (f *Fs) putSftpConnection(pc **conn, err error) {
f.poolMu.Unlock()
}
-// shellExpand replaces a leading "~" with "${HOME}" and expands all environment
-// variables afterwards.
-func shellExpand(s string) string {
- if s != "" {
- if s[0] == '~' {
- newS, err := homedir.Expand(s)
- if err == nil {
- s = newS
- }
- }
- s = os.ExpandEnv(s)
- }
- return s
-}
-
// NewFs creates a new Fs object from the name and root. It connects to
// the host specified in the config file.
func NewFs(name, root string, m configmap.Mapper) (fs.Fs, error) {
@@ -361,7 +346,7 @@ func NewFs(name, root string, m configmap.Mapper) (fs.Fs, error) {
sshConfig.Config.Ciphers = append(sshConfig.Config.Ciphers, "aes128-cbc")
}
- keyFile := shellExpand(opt.KeyFile)
+ keyFile := env.ShellExpand(opt.KeyFile)
// Add ssh agent-auth if no password or file specified
if (opt.Pass == "" && keyFile == "") || opt.KeyUseAgent {
sshAgentClient, _, err := sshagent.New()
diff --git a/lib/env/env.go b/lib/env/env.go
new file mode 100644
index 000000000..6485e8214
--- /dev/null
+++ b/lib/env/env.go
@@ -0,0 +1,23 @@
+// Package env contains functions for dealing with environment variables
+package env
+
+import (
+ "os"
+
+ homedir "github.com/mitchellh/go-homedir"
+)
+
+// ShellExpand replaces a leading "~" with the home directory" and
+// expands all environment variables afterwards.
+func ShellExpand(s string) string {
+ if s != "" {
+ if s[0] == '~' {
+ newS, err := homedir.Expand(s)
+ if err == nil {
+ s = newS
+ }
+ }
+ s = os.ExpandEnv(s)
+ }
+ return s
+}
diff --git a/lib/env/env_test.go b/lib/env/env_test.go
new file mode 100644
index 000000000..0382ecfed
--- /dev/null
+++ b/lib/env/env_test.go
@@ -0,0 +1,31 @@
+package env
+
+import (
+ "os"
+ "testing"
+
+ homedir "github.com/mitchellh/go-homedir"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestShellExpand(t *testing.T) {
+ home, err := homedir.Dir()
+ require.NoError(t, err)
+ require.NoError(t, os.Setenv("EXPAND_TEST", "potato"))
+ defer func() {
+ require.NoError(t, os.Unsetenv("EXPAND_TEST"))
+ }()
+ for _, test := range []struct {
+ in, want string
+ }{
+ {"", ""},
+ {"~", home},
+ {"~/dir/file.txt", home + "/dir/file.txt"},
+ {"/dir/~/file.txt", "/dir/~/file.txt"},
+ {"~/${EXPAND_TEST}", home + "/potato"},
+ } {
+ got := ShellExpand(test.in)
+ assert.Equal(t, test.want, got, test.in)
+ }
+}