-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
183 lines (158 loc) · 4.06 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"context"
"flag"
"fmt"
"io"
"net/http"
"os"
"github.com/BurntSushi/toml"
"golang.org/x/oauth2/clientcredentials"
)
type Config struct {
ClientID string `toml:"ClientID"`
ClientSecret string `toml:"ClientSecret"`
Scopes []string `toml:"Scopes"`
}
var (
// These will get overridden goreleaser.
buildVersion = "dev"
buildCommit = "none"
buildDate = "unknown"
)
var configFile = flag.String("config", "config.toml", "Config file name and path")
var showVersion = flag.Bool("version", false, "Show version and exit.")
func getClient() (*http.Client, error) {
var config Config
_, err := toml.DecodeFile(*configFile, &config)
if err != nil {
return nil, fmt.Errorf("failed to read config file %v: %w", *configFile, err)
}
ctx := context.Background()
conf := &clientcredentials.Config{
ClientID: config.ClientID,
ClientSecret: config.ClientSecret,
Scopes: config.Scopes,
TokenURL: "https://auth.globus.org/v2/oauth2/token",
}
client := conf.Client(ctx)
return client, nil
}
func download(source, destination string) error {
client, err := getClient()
if err != nil {
return fmt.Errorf("failed to get client: %w", err)
}
res, err := client.Get(source)
if err != nil {
return fmt.Errorf("failed to download %v: %w", source, err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("failed to download %v: got status code: %v", source, res.StatusCode)
}
out, err := os.Create(destination)
if err != nil {
return fmt.Errorf("failed to open destination %v for writing: %w", destination, err)
}
defer out.Close()
_, err = io.Copy(out, res.Body)
if err != nil {
return fmt.Errorf("failed to save %v as %v: %w", source, destination, err)
}
return nil
}
func upload(source, destination string) error {
client, err := getClient()
if err != nil {
return fmt.Errorf("failed to get client: %w", err)
}
input, err := os.Open(source)
if err != nil {
return fmt.Errorf("failed to open source %v for reading: %w", source, err)
}
info, err := input.Stat()
if err != nil {
return fmt.Errorf("failed to open source %v stats: %w", source, err)
}
fileSize := info.Size()
req, err := http.NewRequest("PUT", destination, input)
if err != nil {
return fmt.Errorf("failed to generate request: %w", err)
}
req.ContentLength = fileSize
res, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to upload to %v: %w", destination, err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
body, err := io.ReadAll(res.Body)
return fmt.Errorf("failed to upload to %v: got status code %v, response: %v, body read err=%v", destination, res.StatusCode, string(body), err)
}
return nil
}
func printVersion() {
fmt.Printf(`
globus-http %v, git-%v. Built %v
`, buildVersion, buildCommit, buildDate)
}
func help() {
fmt.Print(`
globus-http allows download and upload throught the Globus HTTP API.
Usage:
Download
globus-http [options] download https://g-123456.12345.1234.data.globus.org/filename.txt filename.txt
Upload
globus-http [options] upload filename https://g-123456.12345.1234.data.globus.org/filename.txt
Options:
`)
flag.PrintDefaults()
fmt.Print(`
The configuration file is a TOML file with the following fields:
ClientID = "your-client-id"
ClientSecret = "your-client-secret"
Scopes = ["scope1", "scope2", "scope3"]
`)
printVersion()
}
func main() {
flag.Usage = help
flag.Parse()
args := flag.Args()
if *showVersion {
printVersion()
os.Exit(0)
}
if len(args) < 1 {
fmt.Println("Missing command.")
help()
os.Exit(1)
}
switch args[0] {
case "download":
if len(args) < 3 {
fmt.Println("Missing source and/or destination for download")
help()
os.Exit(1)
}
err := download(args[1], args[2])
if err != nil {
fmt.Println("ERROR: ", err)
os.Exit(1)
}
os.Exit(0)
case "upload":
if len(args) < 3 {
fmt.Println("Missing source and/or destination for upload")
help()
os.Exit(1)
}
err := upload(args[1], args[2])
if err != nil {
fmt.Println("ERROR: ", err)
os.Exit(1)
}
os.Exit(0)
}
}