Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Bruce Flynn
sftper
Commits
2d138dd4
Commit
2d138dd4
authored
Oct 10, 2019
by
Bruce Flynn
Browse files
interfaces make for much slowness for some reason
parent
e2d53752
Changes
1
Hide whitespace changes
Inline
Side-by-side
api.go
View file @
2d138dd4
package
main
import
(
"strings"
"bufio"
"encoding/binary"
"encoding/json"
...
...
@@ -11,7 +10,9 @@ import (
url_
"net/url"
"os"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
...
...
@@ -19,11 +20,6 @@ import (
"github.com/pkg/errors"
)
var
(
errBadCommand
=
errors
.
New
(
"Invalid command"
)
errBadArgs
=
errors
.
New
(
"Invalid arguments"
)
)
// type yielded from binaryCommands or testCommands
type
command
struct
{
Name
string
`json:"command"`
...
...
@@ -62,11 +58,11 @@ type mySFTPClient struct {
SFTP
*
sftp
.
Client
}
func
(
c
mySFTPClient
)
Close
()
error
{
func
(
c
*
mySFTPClient
)
Close
()
error
{
return
c
.
SFTP
.
Close
()
}
func
(
c
mySFTPClient
)
Create
(
path
string
)
(
io
.
WriteCloser
,
error
)
{
func
(
c
*
mySFTPClient
)
Create
(
path
string
)
(
io
.
WriteCloser
,
error
)
{
f
,
err
:=
c
.
SFTP
.
Create
(
path
)
if
err
!=
nil
{
return
sftpFile
{},
err
...
...
@@ -74,26 +70,26 @@ func (c mySFTPClient) Create(path string) (io.WriteCloser, error) {
return
sftpFile
{
f
},
nil
}
func
(
c
mySFTPClient
)
Poke
()
bool
{
func
(
c
*
mySFTPClient
)
Poke
()
bool
{
_
,
err
:=
c
.
SFTP
.
Getwd
()
return
err
==
nil
}
func
(
c
mySFTPClient
)
Open
(
path
string
)
(
io
.
ReadCloser
,
error
)
{
func
(
c
*
mySFTPClient
)
Open
(
path
string
)
(
io
.
ReadCloser
,
error
)
{
return
c
.
SFTP
.
Open
(
path
)
}
func
(
c
mySFTPClient
)
ReadDir
(
path
string
)
([]
os
.
FileInfo
,
error
)
{
func
(
c
*
mySFTPClient
)
ReadDir
(
path
string
)
([]
os
.
FileInfo
,
error
)
{
return
c
.
SFTP
.
ReadDir
(
path
)
}
func
(
c
mySFTPClient
)
Remove
(
path
string
)
error
{
func
(
c
*
mySFTPClient
)
Remove
(
path
string
)
error
{
return
c
.
SFTP
.
Remove
(
path
)
}
type
sftpAPI
struct
{
URL
*
url_
.
URL
Client
sftpClient
Client
*
sftp
.
Client
Cfg
ssh
.
ClientConfig
Req
io
.
Reader
Resp
io
.
Writer
...
...
@@ -213,10 +209,13 @@ func (s sftpAPI) doPut(source, dest string) error {
}
defer
fin
.
Close
()
_
,
err
=
io
.
Copy
(
fout
,
fin
)
t
:=
time
.
Now
()
n
,
err
:=
io
.
Copy
(
fout
,
fin
)
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"upload failed for %s -> %s"
,
source
,
dest
)
}
tt
:=
time
.
Now
()
.
Sub
(
t
)
.
Seconds
()
debug
(
"%fs, %d bytes; throughput %f Mb/s"
,
tt
,
n
,
float64
(
n
)
*
8
/
1024
/
1024
/
tt
)
return
nil
}
...
...
@@ -286,7 +285,7 @@ func (s sftpAPI) doDelete(path string) error {
// make sure the client is connected by poking it
func
(
s
*
sftpAPI
)
ensureConnected
()
error
{
if
!
s
.
Client
.
Poke
()
{
if
_
,
err
:=
s
.
Client
.
Getwd
();
err
!=
nil
{
debug
(
"not connected, connecting"
)
return
s
.
connect
()
}
...
...
@@ -296,6 +295,7 @@ func (s *sftpAPI) ensureConnected() error {
func
(
s
sftpAPI
)
doCommand
(
cmd
command
)
result
{
zult
:=
result
{
"ok"
,
""
,
""
}
t
:=
time
.
Now
()
err
:=
s
.
ensureConnected
()
if
err
!=
nil
{
zult
.
Status
=
"fail"
...
...
@@ -333,6 +333,8 @@ func (s sftpAPI) doCommand(cmd command) result {
zult
.
Status
=
"error"
zult
.
Message
=
fmt
.
Sprintf
(
"unknown command:
\"
%s
\"
"
,
cmd
.
Name
)
}
debug
(
"command took %fs"
,
time
.
Now
()
.
Sub
(
t
)
.
Seconds
())
return
zult
}
...
...
@@ -369,7 +371,7 @@ func (s *sftpAPI) connect() error {
if
err
!=
nil
{
return
err
}
s
.
Client
=
mySFTPClient
{
c
}
s
.
Client
=
c
return
nil
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment