-
Notifications
You must be signed in to change notification settings - Fork 1
/
srink.go
81 lines (77 loc) · 1.82 KB
/
srink.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 (
_ "embed"
"fmt"
"log"
"os"
"strconv"
"strings"
)
func main() {
args := os.Args
if len(args) == 1 {
fmt.Println("srink:", "no argument given")
os.Exit(1)
}
switch opt := strings.ToLower(args[1]); opt {
case "server":
if len(args) >= 3 && strings.ToLower(args[2]) == "update" {
updateServerConf(args[3:])
fmt.Println("srink:", "updated config successfully")
return
}
server := newServer(log.Default())
server.start()
default:
if len(args) >= 2 && strings.ToLower(args[1]) == "update" {
updateClientConf(args[2:])
fmt.Println("srink:", "updated config successfully")
return
}
var hash string
if len(args) >= 3 {
hash = args[2]
}
client := newClient(log.Default())
client.updateApiUrl("http://0.0.0.0:7837")
surl, err := client.shortenUrl(hash, opt)
if err != nil {
fmt.Println("srink:", err)
os.Exit(1)
}
fmt.Println("srink:", `shortened your url to`, surl)
}
}
func updateServerConf(args []string) {
if len(args) <= 1 {
fmt.Println("srink:", "provide me the key and its value to update")
os.Exit(1)
}
conf := readUserConfig("server-conf.yml", log.Default())
switch strings.ToLower(args[0]) {
case "token", "auth-token", "api-key":
conf.add("token", args[1])
case "port", "api-port":
port, err := strconv.Atoi(args[1])
if err != nil {
fmt.Println("srink:", "port needs to be a valid integer")
os.Exit(1)
}
conf.add("port", port)
}
conf.write()
}
func updateClientConf(args []string) {
if len(args) <= 1 {
fmt.Println("srink:", "provide me the key and its value to update")
os.Exit(1)
}
conf := readUserConfig("client-conf.yml", log.Default())
switch strings.ToLower(args[0]) {
case "token", "auth-token", "api-key":
conf.add("token", args[1])
case "api-url", "api", "url":
conf.add("api-url", args[1])
}
conf.write()
}