Skip to content

Commit

Permalink
Address code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
djjuhasz committed Nov 3, 2023
1 parent 9089791 commit 84503ec
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 80 deletions.
25 changes: 25 additions & 0 deletions .am.secret
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: v1
kind: Secret
metadata:
name: am-secret
type: Opaque
stringData:
enduro_am_user: "analyst"
enduro_am_api_key: "1w9ioGI2reeeeua8uPaiOee4Eiueev6u"
enduro_known_hosts: |
# enduro-temporal.ss.analyst.archivematica.net:22 SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.9
enduro-temporal.ss.analyst.archivematica.net ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDW98QbhNAq2bwazlJb57ptBgxhWELNVo4R4Mh6WEO9FJHUfOLc6bE6eKD65MsJ0BZ4utpe64M1rvH6OX5PVQPFW8IjDgl132UPzkMSAJlze+aZhVI7Xjwq/ppJyRS3EJNyZ5cUV2yv/pDZ8zmaq06EpWnpbwYj8fmvdXqYJAvX7iJ4DZ9dycEPmNnWMwlh4pv3EOHlGO7gFqo2hurbzZFroz9O7mG/msf0qO3F/IXjJT6kw8yvxJ9nBMymp8ErdsLKhMxgBRPyrO2HULRUsDchZk550DCPrOAIH8Dea+WFsmczhJhKYDRWqiwTGhmaXQT3qezlMwNxJJnIeErEVSJx3Ic7pzcF8ZjTy2QwiGEdcMgFSgQobx9ujiuc+C7HaXkx8eOely8j6fiFr2cM6PzuuxgmBomcNOJjAz0r1vEGlbDLy5t/D7cYk3zElarXbqn1q9cronD6MR0h46hs7fxh+Ki6nwf60oOioD0kQmkflm6CCYA4LBTNt3+g3K6tqyU=
# enduro-temporal.ss.analyst.archivematica.net:22 SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.9
enduro-temporal.ss.analyst.archivematica.net ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBIbi18z3t2gzGJfZLWGVWJLEEquuhPIxTcFDFE4N4j2rXt5FLHJS8zb/CSyWKjsBmGrspQz4xfTYSJJ0lc6+fww=
# enduro-temporal.ss.analyst.archivematica.net:22 SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.9
enduro-temporal.ss.analyst.archivematica.net ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEYTs4txTlcU8iGQ6C9ilvITtZaW4S3mT32w+WvzMmC0
data:
enduro_id_ed25519: |
LS0tLS1CRUdJTiBPUEVOU1NIIFBSSVZBVEUgS0VZLS0tLS0KYjNCbGJuTnphQzFyWlhrdGRqRUFB
QUFBQkc1dmJtVUFBQUFFYm05dVpRQUFBQUFBQUFBQkFBQUFNd0FBQUF0emMyZ3RaVwpReU5UVXhP
UUFBQUNEV1ZhSVR5d0xwTnh2STQwY0xHQUI3dWhmZDArc1JqNTc3VDNsNFU0dUdpd0FBQUppbXJG
Y3ZwcXhYCkx3QUFBQXR6YzJndFpXUXlOVFV4T1FBQUFDRFdWYUlUeXdMcE54dkk0MGNMR0FCN3Vo
ZmQwK3NSajU3N1QzbDRVNHVHaXcKQUFBRUFZRGlGdWh5d3FGNTBVMUFLTnhJY05NTnZTUWMwZE9R
a0piZ25uMzlxb0c5WlZvaFBMQXVrM0c4ampSd3NZQUh1NgpGOTNUNnhHUG52dFBlWGhUaTRhTEFB
QUFGV1JoZG1sa1FHRnlkR1ZtWVdOMGRXRnNMbU52YlE9PQotLS0tLUVORCBPUEVOU1NIIFBSSVZB
VEUgS0VZLS0tLS0K
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/aws/aws-sdk-go v1.45.25
github.com/coreos/go-oidc/v3 v3.7.0
github.com/cyphar/filepath-securejoin v0.2.4
github.com/dolmen-go/contextio v1.0.0
github.com/fsnotify/fsnotify v1.6.0
github.com/gliderlabs/ssh v0.3.5
github.com/go-logr/logr v1.2.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,8 @@ github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5Xh
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/dolmen-go/contextio v1.0.0 h1:bNfCo4gsRIhMeo6Z1ImXzkxZG81B6I5t2fUFJjphdAU=
github.com/dolmen-go/contextio v1.0.0/go.mod h1:cxc20xI7fOgsFHWgt+PenlDDnMcrvh7Ocuj5hEFIdEk=
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 h1:iFaUwBSo5Svw6L7HYpRu/0lE3e0BaElwnNO1qkNQxBY=
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5/go.mod h1:qssHWj60/X5sZFNxpG4HBPDHVqxNm4DfnCKgrbZOT+s=
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
Expand Down
11 changes: 2 additions & 9 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ github.com/alecthomas/kingpin/v2 v2.3.2/go.mod h1:0gyi0zQnjuFk8xrkNKamJoyUo382HR
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg=
github.com/apache/arrow/go/v10 v10.0.1 h1:n9dERvixoC/1JjDmBcs9FPaEryoANa2sCgVFo6ez9cI=
github.com/apache/arrow/go/v11 v11.0.0 h1:hqauxvFQxww+0mEU/2XHG6LT7eZternCZq+A5Yly2uM=
Expand Down Expand Up @@ -303,7 +302,6 @@ github.com/gabriel-vasile/mimetype v1.4.1 h1:TRWk7se+TOjCYgRth7+1/OYLNiRNIotknkF
github.com/gabriel-vasile/mimetype v1.4.1/go.mod h1:05Vi0w3Y9c/lNvJOdmIwvrrAhX3rYhfQQCaf9VJcv7M=
github.com/getkin/kin-openapi v0.114.0/go.mod h1:l5e9PaFUo9fyLJCPGQeXI2ML8c3P8BHOEV2VaAVf/pc=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/gliderlabs/ssh v0.3.5/go.mod h1:8XB4KraRrX39qHhT6yxPsHedjA08I/uBVwj4xC+/+z4=
github.com/go-fonts/dejavu v0.1.0 h1:JSajPXURYqpr+Cu8U9bt8K+XcACIHWqWrvWCKyeFmVQ=
github.com/go-fonts/latin-modern v0.2.0 h1:5/Tv1Ek/QCr20C6ZOz15vw3g7GELYL98KWr8Hgo+3vk=
github.com/go-fonts/liberation v0.2.0 h1:jAkAWJP4S+OsrPLZM4/eC9iW7CtHy+HBXrEwZXWo5VM=
Expand Down Expand Up @@ -344,7 +342,6 @@ github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2V
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/glog v1.1.0 h1:/d3pCKDPWNnvIWe0vVUpNP32qc8U3PDVxySP/y360qE=
github.com/golang/glog v1.1.2 h1:DVjP2PbBOzHyzA+dn3WhHIq4NdVu3Q+pvivFICf/7fo=
github.com/golang/glog v1.1.2/go.mod h1:zR+okUeTbrL6EL3xHUDxZuEtGv04p5shwip1+mL/rLQ=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -380,8 +377,6 @@ github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMW
github.com/hashicorp/consul/api v1.20.0/go.mod h1:nR64eD44KQ59Of/ECwt2vUmIK2DKsDzAwTmwmLl8Wpo=
github.com/hashicorp/consul/api v1.25.1 h1:CqrdhYzc8XZuPnhIYZWH45toM0LB9ZeYr/gvpLVI3PE=
github.com/hashicorp/consul/api v1.25.1/go.mod h1:iiLVwR/htV7mas/sy0O+XSuEnrdBUUydemjxcUrAt4g=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v1.2.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-hclog v1.5.0 h1:bI2ocEMgcVlz55Oj1xZNBsVi900c7II+fWDyV9o+13c=
github.com/hashicorp/go-hclog v1.5.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
Expand Down Expand Up @@ -466,6 +461,7 @@ github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
Expand Down Expand Up @@ -496,6 +492,7 @@ github.com/muesli/termenv v0.15.1 h1:UzuTb/+hhlBugQz28rpzey4ZuKcZ03MeKsoG7IJZIxs
github.com/muesli/termenv v0.15.1/go.mod h1:HeAQPTzpfs016yGtA4g00CsdYnVLJvxsS4ANqrZs2sQ=
github.com/mutecomm/go-sqlcipher/v4 v4.4.0 h1:sV1tWCWGAVlPhNGT95Q+z/txFxuhAYWwHD1afF5bMZg=
github.com/mutecomm/go-sqlcipher/v4 v4.4.0/go.mod h1:PyN04SaWalavxRGH9E8ZftG6Ju7rsPrGmQRjrEaVpiY=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nakagami/firebirdsql v0.0.0-20190310045651-3c02a58cfed8 h1:P48LjvUQpTReR3TQRbxSeSBsMXzfK0uol7eRcr7VBYQ=
github.com/nakagami/firebirdsql v0.0.0-20190310045651-3c02a58cfed8/go.mod h1:86wM1zFnC6/uDBfZGNwB65O+pR2OFi5q/YQaEUid1qA=
Expand Down Expand Up @@ -652,7 +649,6 @@ go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN8
golang.org/x/arch v0.1.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I=
Expand All @@ -666,7 +662,6 @@ golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028 h1:4+4C/Iv2U4fMZBiMCc98MG
golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20200421231249-e086a090c8fd/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/net v0.16.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
Expand All @@ -677,11 +672,9 @@ golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=
gonum.org/v1/gonum v0.11.0 h1:f1IJhK4Km5tBJmaiJXtk/PkL4cdVX6J+tGiM187uT5E=
Expand Down
17 changes: 17 additions & 0 deletions internal/sftp/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sftp

import (
"context"
"io"
)

// A Client manages the transmission of data over SFTP.
//
// Implementations of the Client interface handle the connection details,
// authentication, and other intricacies associated with different SFTP
// servers and protocols.
type Client interface {
// Upload transfers data from the provided source reader to a specified
// destination on the SFTP server.
Upload(ctx context.Context, src io.Reader, dest string) (bytes int64, err error)
}
37 changes: 32 additions & 5 deletions internal/sftp/config.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
package sftp

import "path/filepath"
import (
"os"
"path/filepath"
)

type Config struct {
// Host address, e.g. 127.0.0.1 (default), sftp.example.org.
Host string

// User name.
User string

// Host port (default: 22).
Port string

// Path to known_hosts file as per https://linux.die.net/man/8/sshd
// "SSH_KNOWN_HOSTS FILE FORMAT" (default: "$HOME/.ssh/known_hosts"). The
// known_hosts file must include the public key of the SFTP server for
// authentication to succeed.
KnownHostsFile string
PrivateKey PrivateKey

// Private key used for authentication.
PrivateKey PrivateKey

// Default directory on SFTP server for file transfers.
RemoteDir string
}

type PrivateKey struct {
Path string
// Path to private key file used for authentication (default:
// "$HOME/.ssh/id_rsa")
Path string

// Passphrase (if any) used to decrypt private key.
Passphrase string
}

Expand All @@ -25,11 +47,16 @@ func (c *Config) SetDefaults() {
c.Port = "22"
}

home, err := os.UserHomeDir()
if err != nil {
return // Don't set default paths if homedir is unknown.
}

if c.KnownHostsFile == "" {
c.KnownHostsFile = filepath.Join("$HOME", ".ssh", "known_hosts")
c.KnownHostsFile = filepath.Join(home, ".ssh", "known_hosts")
}

if c.PrivateKey.Path == "" {
c.PrivateKey.Path = filepath.Join("$HOME", ".ssh", "id_rsa")
c.PrivateKey.Path = filepath.Join(home, ".ssh", "id_rsa")
}
}
44 changes: 28 additions & 16 deletions internal/sftp/goclient.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package sftp

import (
"context"
"errors"
"fmt"
"io"
"os"

"github.com/dolmen-go/contextio"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
Expand All @@ -18,7 +20,7 @@ type GoClient struct {
sftp *sftp.Client
}

var _ Service = (*GoClient)(nil)
var _ Client = (*GoClient)(nil)

// NewGoClient returns a new GoSFTP client with the given configuration.
func NewGoClient(cfg Config) *GoClient {
Expand All @@ -29,8 +31,10 @@ func NewGoClient(cfg Config) *GoClient {

// Upload writes the data from src to the remote file at dest and returns the
// number of bytes written. A new SFTP connection is opened before writing, and
// closed when the upload is complete.
func (c *GoClient) Upload(src io.Reader, dest string) (int64, error) {
// closed when the upload is complete or cancelled.
//
// Upload is not thread safe.
func (c *GoClient) Upload(ctx context.Context, src io.Reader, dest string) (int64, error) {
if err := c.dial(); err != nil {
return 0, err
}
Expand All @@ -39,12 +43,15 @@ func (c *GoClient) Upload(src io.Reader, dest string) (int64, error) {
// Note: Some SFTP servers don't support O_RDWR mode.
w, err := c.sftp.OpenFile(dest, (os.O_WRONLY | os.O_CREATE | os.O_TRUNC))
if err != nil {
return 0, fmt.Errorf("SFTP: couldn't create remote file %q: %w", dest, err)
return 0, fmt.Errorf("SFTP: couldn't create remote file %q: %v", dest, err)
}
defer w.Close()

bytes, err := io.Copy(w, src)
// Use contextio to stop the upload if a context cancellation signal is
// received.
bytes, err := io.Copy(contextio.NewWriter(ctx, w), contextio.NewReader(ctx, src))
if err != nil {
return 0, fmt.Errorf("SFTP: failed to write to %q: %w", dest, err)
return 0, fmt.Errorf("SFTP: failed to write to %q: %v", dest, err)
}

return bytes, nil
Expand All @@ -54,17 +61,17 @@ func (c *GoClient) Upload(src io.Reader, dest string) (int64, error) {
// When the clients are no longer needed, close() must be called to prevent
// leaks.
func (c *GoClient) dial() error {
sshc, err := SSHConnect(c.cfg)
var err error

c.ssh, err = sshConnect(c.cfg)
if err != nil {
return fmt.Errorf("SSH: %w", err)
return fmt.Errorf("SSH: %v", err)
}
c.ssh = sshc

sftpc, err := sftp.NewClient(sshc)
c.sftp, err = sftp.NewClient(c.ssh)
if err != nil {
return fmt.Errorf("Unable to start SFTP subsystem: %w", err)
return fmt.Errorf("Unable to start SFTP subsystem: %v", err)
}
c.sftp = sftpc

return nil
}
Expand All @@ -73,11 +80,16 @@ func (c *GoClient) dial() error {
func (c *GoClient) close() error {
var errs error

if err := c.sftp.Close(); err != nil {
errs = errors.Join(err, errs)
if c.sftp != nil {
if err := c.sftp.Close(); err != nil {
errs = errors.Join(err, errs)
}
}
if err := c.ssh.Close(); err != nil {
errs = errors.Join(err, errs)

if c.ssh != nil {
if err := c.ssh.Close(); err != nil {
errs = errors.Join(err, errs)
}
}

return errs
Expand Down
Loading

0 comments on commit 84503ec

Please sign in to comment.