Skip to content

Commit

Permalink
Switch to docopt (#5)
Browse files Browse the repository at this point in the history
docopt allows to have a nice mix of options and arguments.
  • Loading branch information
brejoc committed Sep 26, 2023
1 parent f2e391f commit 19177f4
Show file tree
Hide file tree
Showing 16 changed files with 2,814 additions and 28 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/brejoc/stockuploader
go 1.21.1

require (
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
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
@@ -1,6 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
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/pkg/sftp v1.13.6 h1:JFZT4XbOU7l77xGSpOdW+pwIMqP044IyjXX6FGyEKFo=
Expand Down
63 changes: 35 additions & 28 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,63 @@
package main

import (
"flag"
"fmt"
"io"
"log"
"os"
"path"
"strings"

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

var username string
var password string
var remote string
var port string
var files string
var Version = "development"
var usage string

func init() {
flag.StringVar(&username, "username", "", "Your username")
flag.StringVar(&password, "password", "", "Your password")
flag.StringVar(&remote, "remote", "sftp.contributor.adobestock.com", "The remote host. Usually this is 'sftp.contributor.adobestock.com'.")
flag.StringVar(&port, "port", "22", "Port of the remote host. Usually this is port 22")
flag.StringVar(&files, "files", "", "Comma separated list of files you'd like to upload")
usage = `stockuploader
Usage:
stockuploader --username peter --password secret <file>...
stockuploader --username peter --password secret [--remote hostname] <file>...
stockuploader --username peter --password secret [--remote hostname] [--port 22] <file>...
stockuploader (-v | --version)
stockuploader (-h | --help)
Arguments:
<file> One or multiple files you'd like to upload
Options:
-h --help Show this screen.
-v --version Show version number.
-u --username=<username> Provide your Adobe username.
-p --password=<password> Provide your Adobe password.
-r --remote=<host> sFTP remote host [default: sftp.contributor.adobestock.com].
-P --port=<port> Remote port [default: 22].`
}

func main() {
flag.Parse()

remote = cleanHostname(remote)
filePaths := cleanFiles(files)

if username == "" {
log.Fatal("You need to provide a username: --username peter")
arguments, _ := docopt.ParseDoc(usage)
if arguments["--version"] == true {
print("Version: " + Version)
os.Exit(0)
}

if password == "" {
log.Fatal("You need to provide a password: --password secret")
}
remote := cleanHostname(arguments["--remote"].(string))
username := arguments["--username"].(string)
password := arguments["--password"].(string)
port := arguments["--port"].(string)
files := cleanFiles(arguments["<file>"].([]string))

conn, client := initiateSftpConnection(username, password, remote, port)
defer conn.Close()
defer client.Close()

// uploading files to sftp
for _, file := range filePaths {
for _, file := range files {
fmt.Println("File: ", file)
copyFile(client, file, path.Base(file))
}
Expand Down Expand Up @@ -101,13 +112,9 @@ func copyFile(client *sftp.Client, source string, target string) {
}

// Takes a comma separated string and returns a cleaned up slice
func cleanFiles(files string) []string {
if files == "" {
return make([]string, 0)
}
func cleanFiles(files []string) []string {
var cleanFileList []string
fileList := strings.Split(files, ",")
for _, file := range fileList {
for _, file := range files {
file = strings.TrimSpace(file)
if file != "" {
cleanFileList = append(cleanFileList, file)
Expand Down
25 changes: 25 additions & 0 deletions vendor/github.com/docopt/docopt-go/.gitignore

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

32 changes: 32 additions & 0 deletions vendor/github.com/docopt/docopt-go/.travis.yml

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

21 changes: 21 additions & 0 deletions vendor/github.com/docopt/docopt-go/LICENSE

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

116 changes: 116 additions & 0 deletions vendor/github.com/docopt/docopt-go/README.md

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

49 changes: 49 additions & 0 deletions vendor/github.com/docopt/docopt-go/doc.go

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

Loading

0 comments on commit 19177f4

Please sign in to comment.