-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlittlehelper.go
83 lines (72 loc) · 1.6 KB
/
littlehelper.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
package opnborg
import (
"encoding/xml"
"errors"
"net/url"
"os"
"strings"
"sync"
)
//
// Display IO
//
// outSlice write messages to stdout
func outSlice(msg []byte, config *OPNCall) {
_, _ = os.Stdout.Write([]byte(config.AppName))
_, _ = os.Stdout.Write(msg)
_, _ = os.Stdout.Write([]byte("\n"))
}
// displayChan channel for the display engine
var displayChan, display, wg = make(chan []byte, 20), sync.WaitGroup{}, sync.WaitGroup{}
// startLog is a non-blocking, conditional, concurrent-save background output handler
func startLog(config *OPNCall) {
go func() {
for msg := range displayChan {
outSlice(msg, config)
}
display.Done()
}()
}
//
// Little Helper
//
// checkURL
func checkURL(env string) (*url.URL, error) {
s := strings.Split(env, "#")
if _, ok := os.LookupEnv(s[0]); ok {
out, err := url.Parse(os.Getenv(env))
if err != nil {
return nil, errors.New("[SETUP][" + env + "][INVALID-URL] " + err.Error())
}
return out, nil
}
return nil, nil
}
// checkPreURL check prefixed url
func checkPreURL(base *url.URL, prefix, env string) (*url.URL, error) {
out, err := url.Parse(base.String() + prefix + os.Getenv(env))
if err != nil {
return nil, errors.New("[SETUP][" + env + "][INVALID-URL] " + err.Error())
}
return out, nil
}
// isEnv
func isEnv(check string) bool {
if content, ok := os.LookupEnv(check); ok {
if content != "" {
return true
}
}
return false
}
// isValidXML
func isValidXML(s string) bool {
return xml.Unmarshal([]byte(s), new(interface{})) == nil
}
// padMonth
func padMonth(in string) string {
if len(in) == 1 {
return "0" + in
}
return in
}