-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (115 loc) · 2.86 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
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"net/url"
"os"
"path"
"time"
"github.com/tjhorner/fs/shorty"
"google.golang.org/api/option"
"cloud.google.com/go/storage"
)
var globalConfig *config
func oops(err interface{}) {
fmt.Println(err)
os.Exit(1)
}
func main() {
globalConfig = loadConfig(defaultConfigPath())
gcsClient, err := storage.NewClient(context.Background(), option.WithCredentialsFile(defaultServiceAccountPath()))
if err != nil {
oops(err)
}
bucket := gcsClient.Bucket(globalConfig.BucketName)
var name string
var shorten bool
var verbose bool
var del string
flag.BoolVar(&shorten, "shorten", false, "Shorten the resulting URL with shorty after uploading")
flag.StringVar(&name, "name", "", "Name of the file. Useful if piping into stdin")
flag.BoolVar(&verbose, "verbose", false, "Make it loud")
flag.StringVar(&del, "delete", "", "When passed, this function will delete the specified object")
flag.Parse()
if del != "" {
err = deleteObject(bucket, del)
if err != nil {
oops(err)
}
fmt.Printf("%s is gone\n", del)
return
}
if name == "" {
name = flag.Arg(0)
}
if name == "" {
oops("Please pass a file name when piping to stdin (if this isn't what you were expecting, see --help).")
}
usingStdin := flag.Arg(0) == ""
var obj *storage.ObjectHandle
if usingStdin {
if verbose {
fmt.Printf("Uploading file %s from stdin...\n", name)
}
obj, err = upload(bucket, os.Stdin, name)
} else {
fil, err := os.Open(name)
if err != nil {
oops(err)
}
fn := path.Base(name)
if verbose {
fmt.Printf("Uploading %s...\n", fn)
}
obj, err = upload(bucket, fil, fn)
}
if err != nil {
oops(err)
}
fqURL, err := url.Parse(fmt.Sprintf("%s/%s", globalConfig.Host, obj.ObjectName()))
if err != nil {
oops(err)
}
if globalConfig.Shorten || shorten {
if verbose {
fmt.Println("Shortening...")
}
short, err := shorty.Shorten(fqURL.String(), globalConfig.ShortyConfig)
if err != nil {
fmt.Printf("Failed to shorten (%v). Long URL: %s\n", err, fqURL.String())
return
}
fmt.Println(short)
return
}
fmt.Println(fqURL)
}
func upload(bucket *storage.BucketHandle, file *os.File, name string) (*storage.ObjectHandle, error) {
obj := newObject(bucket, name)
w := obj.NewWriter(context.Background())
data, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
_, err = w.Write(data)
if err != nil {
return nil, err
}
err = w.Close()
if err != nil {
return nil, err
}
return obj, nil
}
func newObject(bucket *storage.BucketHandle, name string) *storage.ObjectHandle {
now := time.Now()
path := fmt.Sprintf("%d/%d/%d/%d_%s", now.Year(), now.Month(), now.Day(), now.Unix(), name)
obj := bucket.Object(path)
return obj
}
func deleteObject(bucket *storage.BucketHandle, name string) error {
obj := bucket.Object(name)
return obj.Delete(context.Background())
}