forked from kellegous/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (74 loc) · 1.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
const (
apiPath = "%s://%s:%s/api/url/%s"
)
type config struct {
proto string
host string
port string
dumpFile string
}
type RoutesDump struct {
OK bool `json:"ok"`
Routes []Route `json:"routes"`
Next string `json:"next"`
}
type Route struct {
Name string `json:"name"`
URL string `json:"url"`
}
func main() {
c := config{}
pflag.StringVar(&c.proto, "protocol", "https", "protocol to use. Only HTTP or HTTPS supported")
pflag.StringVar(&c.host, "host", "localhost", "host to post data to")
pflag.StringVar(&c.port, "port", "8067", "port on host to talk to")
pflag.StringVar(&c.dumpFile, "file", "", "dump file to load from")
pflag.Parse()
if err := viper.BindPFlags(pflag.CommandLine); err != nil {
log.Panic(err)
}
// allow env vars to set pflags
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
if c.dumpFile == "" {
log.Fatal("dump file must be specified with --file argument")
}
var d RoutesDump
f, err := ioutil.ReadFile(c.dumpFile)
if err != nil {
log.Printf("error reading dump file : %s\n", c.dumpFile)
log.Fatal(err)
}
err = json.Unmarshal(f, &d)
if err != nil {
log.Printf("error parsing dump file : %s\n", c.dumpFile)
log.Fatal(err)
}
for _, v := range d.Routes {
req := fmt.Sprintf(apiPath, c.proto, c.host, c.port, v.Name)
p, err := json.Marshal(&v)
if err != nil {
log.Printf("error marshalling data for link : %s\n", v.Name)
log.Println(err)
continue
}
resp, err := http.Post(req, "application/json", bytes.NewReader(p))
if err != nil {
log.Printf("error POSTing link : %s %s\n", v.Name, err.Error())
} else {
log.Printf("POSTed short link (%s) : %s\n", resp.Status, v.Name)
resp.Body.Close()
}
}
}