-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (133 loc) · 3.52 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"fmt"
"io"
"log"
"os"
"path"
"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
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() {
arguments, _ := docopt.ParseDoc(usage)
if arguments["--version"] == true {
print("Version: " + Version)
os.Exit(0)
}
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 files {
copyFile(client, file, path.Base(file), Text)
}
}
func initiateSftpConnection(user string, pass string, remote string, port string) (*ssh.Client, *sftp.Client) {
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(pass),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
// Initiate ssh connection
conn, err := ssh.Dial("tcp", remote+":"+port, config)
if err != nil {
log.Fatal(err)
}
// Create sftp channel
client, err := sftp.NewClient(conn)
if err != nil {
log.Fatal(err)
}
return conn, client
}
// Copies file to remote host
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)
if err != nil {
log.Fatal(err)
}
defer dstFile.Close()
// The source file
srcFile, err := os.Open(source)
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)
}
}
}
// Takes a comma separated string and returns a cleaned up slice
func cleanFiles(files []string) []string {
var cleanFileList []string
for _, file := range files {
file = strings.TrimSpace(file)
if file != "" {
cleanFileList = append(cleanFileList, file)
}
}
return cleanFileList
}
// Removed trailing "sftp://" and also trimms the string
func cleanHostname(remote string) string {
if strings.HasPrefix(remote, "sftp://") {
remote = strings.TrimSpace(strings.Replace(remote, "sftp://", "", 1))
}
// TODO: Check for port
return remote
}