-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (57 loc) · 1.53 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
package main
import (
"fmt"
"os"
"path/filepath"
)
type Output struct {
Data struct {
DisplayUrl string `json:"display_url,omitempty"`
Expiration int `json:"expiration,omitempty"`
Thumb struct {
Filename string `json:"filename,omitempty"`
Mime string `json:"mime,omitempty"`
Url string `json:"url,omitempty"`
} `json:"thumb,omitempty"`
DeleteUrl string `json:"delete_url,omitempty"`
} `json:"data"`
Success bool `json:"success,omitempty"`
Status int `json:"status,omitempty"`
StatusCode int `json:"status_code,omitempty"`
Error struct {
Message string `json:"message,omitempty"`
Code int `json:"code,omitempty"`
} `json:"error,omitempty"`
StatusTxt string `json:"status_txt,omitempty"`
}
func throwError(msg string) {
fmt.Printf("\033[31m[error]\033[0m: %s", msg)
os.Exit(0)
}
func main() {
// creating the uploader
var uploader Uploader
// checking the args
if len(os.Args) < 2 {
throwError("you must provide an image\n(e.g: upload image.jpg)")
}
// reading the filename as first argument
fileName := os.Args[1]
// get the currentPath
currentPath, _ := os.Getwd()
// setting the imagePath
imagePath := filepath.Join(currentPath, fileName)
// initializing the uploader
err := uploader.init(imagePath)
if err != nil {
msg := fmt.Sprintf("intializing uploader: %v", err)
throwError(msg)
}
// uploading the img
output, err := uploader.UploadImage()
if err != nil {
msg := fmt.Sprintf("uploading image: %v", err)
throwError(msg)
}
fmt.Println(output)
}