-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (71 loc) · 1.92 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
package main
import (
"flag"
"fmt"
"github.com/go-resty/resty/v2"
"log"
"os"
"time"
"github.com/csmith/envflag"
)
var (
filename = flag.String("filename", "adventures.zip", "path to an adventures zip file")
apiurl = flag.String("apiurl", "http://localhost:8090/", "URL of the WOM instance")
email = flag.String("email", "", "Admin email address")
password = flag.String("password", "", "Admin password")
)
func main() {
envflag.Parse()
if err := uploadAdventures(*filename, *apiurl, *email, *password); err != nil {
log.Fatalf("Failed to upload: %v", err)
}
}
type authRequest struct {
Identity string `json:"identity"`
Password string `json:"password"`
}
type authResponse struct {
Token string `json:"token"`
}
func uploadAdventures(fileName, apiurl string, email string, password string) error {
file, err := os.Open(fileName)
if err != nil {
return fmt.Errorf("unable to open zip: %s", err)
}
defer func() {
_ = file.Close()
}()
var response authResponse
//TODO: Should remove resty really, its overkill now I'm just uploading a file
client := resty.New()
client.SetRetryCount(3).
SetRetryWaitTime(3*time.Second).
SetRetryMaxWaitTime(10*time.Second).
SetBaseURL(apiurl).
SetHeader("Content-Type", "application/json")
resp, err := client.R().
SetBody(authRequest{
Identity: email,
Password: password,
}).
SetResult(&response).
Post("api/admins/auth-with-password")
if err != nil {
return fmt.Errorf("unable to login")
}
if resp.StatusCode() != 200 {
return fmt.Errorf("unable to login: Status %d", resp.StatusCode())
}
client.SetAuthToken(response.Token)
resp, err = client.R().
SetContentLength(true).
SetFileReader("adventures.zip", "adventures.zip", file).
Post("/wom/importzip")
if err != nil {
return fmt.Errorf("unable to upload zip: %s", err)
}
if resp.StatusCode() != 200 {
return fmt.Errorf("unable to upload zip: Status %d", resp.StatusCode())
}
return nil
}