Skip to content

Commit

Permalink
Showing progress now (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
brejoc committed Sep 29, 2023
1 parent 07bb35d commit eddbcc3
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21.1

require (
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
github.com/mitchellh/ioprogress v0.0.0-20180201004757-6a23b12fa88e
github.com/pkg/sftp v1.13.6
golang.org/x/crypto v0.13.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/mitchellh/ioprogress v0.0.0-20180201004757-6a23b12fa88e h1:Qa6dnn8DlasdXRnacluu8HzPts0S1I9zvvUPDbBnXFI=
github.com/mitchellh/ioprogress v0.0.0-20180201004757-6a23b12fa88e/go.mod h1:waEya8ee1Ro/lgxpVhkJI4BVASzkm3UZqkx/cFJiYHM=
github.com/pkg/sftp v1.13.6 h1:JFZT4XbOU7l77xGSpOdW+pwIMqP044IyjXX6FGyEKFo=
github.com/pkg/sftp v1.13.6/go.mod h1:tz1ryNURKu77RL+GuCzmoJYxQczL3wLNNpPWagdg4Qk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
44 changes: 35 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ import (
"strings"

"github.com/docopt/docopt-go"
"github.com/mitchellh/ioprogress"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)

var Version = "development"
var usage string

type cliOutput int

const (
NoOutput cliOutput = iota
Text
)

func init() {
usage = `stockuploader
Expand Down Expand Up @@ -58,7 +66,7 @@ func main() {

// uploading files to sftp
for _, file := range files {
copyFile(client, file, path.Base(file))
copyFile(client, file, path.Base(file), Text)
}
}

Expand Down Expand Up @@ -87,11 +95,10 @@ func initiateSftpConnection(user string, pass string, remote string, port string
}

// Copies file to remote host
func copyFile(client *sftp.Client, source string, target string) {
fmt.Print(source)
func copyFile(client *sftp.Client, source string, target string, output cliOutput) {

// Create destination file
dstFile, err := client.OpenFile(target, os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
//dstFile, err := client.Create(target)
if err != nil {
log.Fatal(err)
}
Expand All @@ -103,12 +110,31 @@ func copyFile(client *sftp.Client, source string, target string) {
log.Fatal(err)
}

// Copy file
bytes, err := io.Copy(dstFile, srcFile)
if err != nil {
log.Fatal(err)
switch output {
case Text:
fmt.Println(source)
sourceFileSize, err := srcFile.Stat()
if err != nil {
log.Fatal(err)
}

progressR := &ioprogress.Reader{
Reader: srcFile,
Size: sourceFileSize.Size(),
}

// copy with progress
_, err = io.Copy(dstFile, progressR)
if err != nil {
log.Fatal(err)
}
default:
// copy silently
_, err = io.Copy(dstFile, srcFile)
if err != nil {
log.Fatal(err)
}
}
fmt.Printf("\t\t(successful with %d bytes)\n", bytes)
}

// Takes a comma separated string and returns a cleaned up slice
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/mitchellh/ioprogress/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions vendor/github.com/mitchellh/ioprogress/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 104 additions & 0 deletions vendor/github.com/mitchellh/ioprogress/draw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 107 additions & 0 deletions vendor/github.com/mitchellh/ioprogress/reader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ github.com/docopt/docopt-go
# github.com/kr/fs v0.1.0
## explicit
github.com/kr/fs
# github.com/mitchellh/ioprogress v0.0.0-20180201004757-6a23b12fa88e
## explicit
github.com/mitchellh/ioprogress
# github.com/pkg/sftp v1.13.6
## explicit; go 1.15
github.com/pkg/sftp
Expand Down

0 comments on commit eddbcc3

Please sign in to comment.