sftp: use correct OS way of reading username - fixes running under crontab
This commit is contained in:
parent
72349bdaae
commit
28480c0570
1 changed files with 21 additions and 2 deletions
23
sftp/sftp.go
23
sftp/sftp.go
|
@ -8,6 +8,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"os/user"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -27,6 +28,10 @@ const (
|
||||||
connectionsPerSecond = 10 // don't make more than this many ssh connections/s
|
connectionsPerSecond = 10 // don't make more than this many ssh connections/s
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
currentUser = readCurrentUser()
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
fsi := &fs.RegInfo{
|
fsi := &fs.RegInfo{
|
||||||
Name: "sftp",
|
Name: "sftp",
|
||||||
|
@ -42,7 +47,7 @@ func init() {
|
||||||
}},
|
}},
|
||||||
}, {
|
}, {
|
||||||
Name: "user",
|
Name: "user",
|
||||||
Help: "SSH username, leave blank for current username, " + os.Getenv("USER"),
|
Help: "SSH username, leave blank for current username, " + currentUser,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
}, {
|
}, {
|
||||||
Name: "port",
|
Name: "port",
|
||||||
|
@ -112,6 +117,20 @@ type ObjectReader struct {
|
||||||
sftpFile *sftp.File
|
sftpFile *sftp.File
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 $HOME then $LOGNAME
|
||||||
|
userName = os.Getenv("HOME")
|
||||||
|
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.
|
||||||
|
@ -251,7 +270,7 @@ func NewFs(name, root string) (fs.Fs, error) {
|
||||||
keyFile := fs.ConfigFileGet(name, "key_file")
|
keyFile := fs.ConfigFileGet(name, "key_file")
|
||||||
insecureCipher := fs.ConfigFileGetBool(name, "use_insecure_cipher")
|
insecureCipher := fs.ConfigFileGetBool(name, "use_insecure_cipher")
|
||||||
if user == "" {
|
if user == "" {
|
||||||
user = os.Getenv("USER")
|
user = currentUser
|
||||||
}
|
}
|
||||||
if port == "" {
|
if port == "" {
|
||||||
port = "22"
|
port = "22"
|
||||||
|
|
Loading…
Add table
Reference in a new issue