pkg / sftp

SFTP support for the go.crypto/ssh package
BSD 2-Clause "Simplified" License
1.5k stars 379 forks source link

Create file using env variables #508

Closed pioz closed 2 years ago

pioz commented 2 years ago

I don't know if this is possible in some way, but what I would like to do is:

dstFile, err := sftp.Create("$HOME/.config/systemd/user/hello.service")

using the $HOME environment variable.

I've tried but I get the error "file does not exist", if instead I use the exploded path "/home/pioz/.config/systemd/user/hello.service" I get no errors.

puellanivis commented 2 years ago

Go does not do string environment-variable interpolation. In order to get this you need to expand the value yourself. So: sftp.Create(filepath.Join(os.Getenv("HOME"), "/.config/systemd/user/hello.service"))

pioz commented 2 years ago

Go does not do string environment-variable interpolation. In order to get this you need to expand the value yourself. So: sftp.Create(filepath.Join(os.Getenv("HOME"), "/.config/systemd/user/hello.service"))

Thanks, but I need the remote os.Getenv("HOME") not the local home directory.

puellanivis commented 2 years ago

Use sftp.Client.Getwd() to get the current working directory from the server, this should default to the user’s home directory.

pioz commented 2 years ago

Thanks, it works! 😗

SwagLYY commented 1 year ago

Use sftp.Client.Getwd() to get the current working directory from the server, this should default to the user’s home directory.

why sftp.Client.Getwd() always reutrn the same directory ? like '/root' , which method could effect this result?