ftp,sftp: fix docs for usernames

- factor env.CurrentUser out of backend/sftp
- Use env.CurrentUser in ftp and sftp
- fix docs to have correct username
This commit is contained in:
Nick Craig-Wood 2020-09-27 11:40:58 +01:00
parent 17acae2b00
commit 41ec712aa9
5 changed files with 34 additions and 23 deletions

View File

@ -6,7 +6,6 @@ import (
"crypto/tls" "crypto/tls"
"io" "io"
"net/textproto" "net/textproto"
"os"
"path" "path"
"runtime" "runtime"
"strings" "strings"
@ -22,10 +21,15 @@ import (
"github.com/rclone/rclone/fs/config/obscure" "github.com/rclone/rclone/fs/config/obscure"
"github.com/rclone/rclone/fs/hash" "github.com/rclone/rclone/fs/hash"
"github.com/rclone/rclone/lib/encoder" "github.com/rclone/rclone/lib/encoder"
"github.com/rclone/rclone/lib/env"
"github.com/rclone/rclone/lib/pacer" "github.com/rclone/rclone/lib/pacer"
"github.com/rclone/rclone/lib/readers" "github.com/rclone/rclone/lib/readers"
) )
var (
currentUser = env.CurrentUser()
)
// Register with Fs // Register with Fs
func init() { func init() {
fs.Register(&fs.RegInfo{ fs.Register(&fs.RegInfo{
@ -42,7 +46,7 @@ func init() {
}}, }},
}, { }, {
Name: "user", Name: "user",
Help: "FTP username, leave blank for current username, " + os.Getenv("USER"), Help: "FTP username, leave blank for current username, " + currentUser,
}, { }, {
Name: "port", Name: "port",
Help: "FTP port, leave blank to use default (21)", Help: "FTP port, leave blank to use default (21)",
@ -311,7 +315,7 @@ func NewFs(name, root string, m configmap.Mapper) (ff fs.Fs, err error) {
} }
user := opt.User user := opt.User
if user == "" { if user == "" {
user = os.Getenv("USER") user = currentUser
} }
port := opt.Port port := opt.Port
if port == "" { if port == "" {

View File

@ -11,7 +11,6 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"os/user"
"path" "path"
"regexp" "regexp"
"strconv" "strconv"
@ -43,7 +42,7 @@ const (
) )
var ( var (
currentUser = readCurrentUser() currentUser = env.CurrentUser()
) )
func init() { func init() {
@ -237,20 +236,6 @@ type Object struct {
sha1sum *string // Cached SHA1 checksum sha1sum *string // Cached SHA1 checksum
} }
// readCurrentUser finds the current user name or "" if not found
func readCurrentUser() (userName string) {
usr, err := user.Current()
if err == nil {
return usr.Username
}
// Fall back to reading $USER then $LOGNAME
userName = os.Getenv("USER")
if userName != "" {
return userName
}
return os.Getenv("LOGNAME")
}
// dial starts a client connection to the given SSH server. It is a // dial starts a client connection to the given SSH server. It is a
// convenience function that connects to the given network address, // convenience function that connects to the given network address,
// initiates the SSH handshake, and then sets up a Client. // initiates the SSH handshake, and then sets up a Client.

View File

@ -48,7 +48,7 @@ Choose a number from below, or type in your own value
1 / Connect to ftp.example.com 1 / Connect to ftp.example.com
\ "ftp.example.com" \ "ftp.example.com"
host> ftp.example.com host> ftp.example.com
FTP username, leave blank for current username, ncw FTP username, leave blank for current username, $USER
Enter a string value. Press Enter for the default (""). Enter a string value. Press Enter for the default ("").
user> user>
FTP port, leave blank to use default (21) FTP port, leave blank to use default (21)

View File

@ -52,7 +52,7 @@ Choose a number from below, or type in your own value
1 / Connect to example.com 1 / Connect to example.com
\ "example.com" \ "example.com"
host> example.com host> example.com
SSH username, leave blank for current username, ncw SSH username, leave blank for current username, $USER
user> sftpuser user> sftpuser
SSH port, leave blank to use default (22) SSH port, leave blank to use default (22)
port> port>
@ -192,7 +192,7 @@ SSH host to connect to
#### --sftp-user #### --sftp-user
SSH username, leave blank for current username, ncw SSH username, leave blank for current username, $USER
- Config: user - Config: user
- Env Var: RCLONE_SFTP_USER - Env Var: RCLONE_SFTP_USER
@ -256,7 +256,9 @@ in the new OpenSSH format can't be used.
#### --sftp-pubkey-file #### --sftp-pubkey-file
Optional path to public key file; set this if you have a signed certificate you want to use for authentication. Optional path to public key file.
Set this if you have a signed certificate you want to use for authentication.
Leading `~` will be expanded in the file name as will environment variables such as `${RCLONE_CONFIG_DIR}`. Leading `~` will be expanded in the file name as will environment variables such as `${RCLONE_CONFIG_DIR}`.

20
lib/env/env.go vendored
View File

@ -3,6 +3,7 @@ package env
import ( import (
"os" "os"
"os/user"
homedir "github.com/mitchellh/go-homedir" homedir "github.com/mitchellh/go-homedir"
) )
@ -24,3 +25,22 @@ func ShellExpand(s string) string {
} }
return s return s
} }
// CurrentUser finds the current user name or "" if not found
func CurrentUser() (userName string) {
userName = os.Getenv("USER")
// If we are making docs just use $USER
if userName == "$USER" {
return userName
}
// Try reading using the OS
usr, err := user.Current()
if err == nil {
return usr.Username
}
// Fall back to reading $USER then $LOGNAME
if userName != "" {
return userName
}
return os.Getenv("LOGNAME")
}