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"
"io"
"net/textproto"
"os"
"path"
"runtime"
"strings"
@ -22,10 +21,15 @@ import (
"github.com/rclone/rclone/fs/config/obscure"
"github.com/rclone/rclone/fs/hash"
"github.com/rclone/rclone/lib/encoder"
"github.com/rclone/rclone/lib/env"
"github.com/rclone/rclone/lib/pacer"
"github.com/rclone/rclone/lib/readers"
)
var (
currentUser = env.CurrentUser()
)
// Register with Fs
func init() {
fs.Register(&fs.RegInfo{
@ -42,7 +46,7 @@ func init() {
}},
}, {
Name: "user",
Help: "FTP username, leave blank for current username, " + os.Getenv("USER"),
Help: "FTP username, leave blank for current username, " + currentUser,
}, {
Name: "port",
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
if user == "" {
user = os.Getenv("USER")
user = currentUser
}
port := opt.Port
if port == "" {

View File

@ -11,7 +11,6 @@ import (
"io"
"io/ioutil"
"os"
"os/user"
"path"
"regexp"
"strconv"
@ -43,7 +42,7 @@ const (
)
var (
currentUser = readCurrentUser()
currentUser = env.CurrentUser()
)
func init() {
@ -237,20 +236,6 @@ type Object struct {
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
// convenience function that connects to the given network address,
// 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
\ "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 ("").
user>
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
\ "example.com"
host> example.com
SSH username, leave blank for current username, ncw
SSH username, leave blank for current username, $USER
user> sftpuser
SSH port, leave blank to use default (22)
port>
@ -192,7 +192,7 @@ SSH host to connect to
#### --sftp-user
SSH username, leave blank for current username, ncw
SSH username, leave blank for current username, $USER
- Config: user
- Env Var: RCLONE_SFTP_USER
@ -256,7 +256,9 @@ in the new OpenSSH format can't be used.
#### --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}`.

20
lib/env/env.go vendored
View File

@ -3,6 +3,7 @@ package env
import (
"os"
"os/user"
homedir "github.com/mitchellh/go-homedir"
)
@ -24,3 +25,22 @@ func ShellExpand(s string) string {
}
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")
}