/
common.go
70 lines (60 loc) · 1.54 KB
/
common.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
package utils
import (
"encoding/json"
"errors"
"flag"
"fmt"
"strings"
)
// PrettyPrint Print json to console elegantly, which is helpful for degbugging
func PrettyPrint(data interface{}, pre string, post string) {
dataJSON, err := json.MarshalIndent(data, "", "\t")
if err != nil {
fmt.Printf("Data cannot be reformatted, the original data is:\n %#v\n", data)
} else {
if pre != "" {
fmt.Printf("%s\n", pre)
}
fmt.Printf("%s\n", dataJSON)
if post != "" {
fmt.Printf("%s\n", post)
}
}
}
// GetLoginOptions Get login information
func GetLoginOptions() (string, string, string, error) {
var server, username, password string
flag.StringVar(&server, "server", "", "Server address")
flag.StringVar(&username, "username", "admin", "Server login username, admin as default")
flag.StringVar(&password, "password", "", "Server login password")
flag.Parse()
if server == "" || username == "" || password == "" {
flag.PrintDefaults()
return "", "", "", errors.New("server, username, and password must all be specified")
}
return server, username, password, nil
}
// URL Compose a URL based on protocol, server address, URI, etc.
func URL(proto string, fqdn string, port string, uri ...string) string {
var url string
if proto == "" {
url = fqdn
} else {
url = proto + "://" + fqdn
}
if port != "" {
url += ":" + port
}
for _, uri := range uri {
url += uri
}
return url
}
func EmptyStrExists(vars ...string) bool {
for _, v := range vars {
if c := strings.TrimSpace(v); c == "" {
return true
}
}
return false
}