-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
107 lines (86 loc) · 2.44 KB
/
helpers.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
package gsuitemdm
//
// GSuiteMDM various helper functions
//
import (
"bufio"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"regexp"
"runtime"
"strings"
"time"
)
// Sort funcs for devices
func (s DatastoreMobileDevices) Len() int {
return len(s.Mobiledevices)
}
func (s DatastoreMobileDevices) Less(i, j int) bool {
return []rune(strings.ToLower(s.Mobiledevices[i].Name))[0] < []rune(strings.ToLower(s.Mobiledevices[j].Name))[0]
}
func (s DatastoreMobileDevices) Swap(i, j int) {
s.Mobiledevices[i], s.Mobiledevices[j] = s.Mobiledevices[j], s.Mobiledevices[i]
}
// Sort funcs for directory data
func (s AllDirectoryData) Len() int {
return len(s.Data)
}
func (s AllDirectoryData) Less(i, j int) bool {
return s.Data[i].Name < s.Data[j].Name
}
func (s AllDirectoryData) Swap(i, j int) {
s.Data[i], s.Data[j] = s.Data[j], s.Data[i]
}
// Helper function to ask for user confirmation in the CLI
func checkUserConfirmation(s string) bool {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("%s [y/n]: ", s)
response, err := reader.ReadString('\n')
if err != nil {
// TODO: fix this
}
response = strings.ToLower(strings.TrimSpace(response))
if response == "y" || response == "yes" {
return true
} else if response == "n" || response == "no" {
return false
}
}
}
// Helper function to strip the domain name from an email address
func getEmailDomain(email string) string {
components := strings.Split(email, "@")
return components[1]
}
// Helper function to get a remote IP from an http.Request
func GetIP(r *http.Request) string {
fwd := r.Header.Get("X-FORWARDED-FOR")
if fwd != "" {
return fwd
}
return r.RemoteAddr
}
// Load configuration and return a config struct
func loadConfig(config string) (GSuiteMDMConfig, error) {
var c GSuiteMDMConfig
jp := json.NewDecoder(strings.NewReader(config))
jp.Decode(&c)
return c, nil
}
// Helper func to track how long a func takes to execute (found on StackExchange I think!)
func TimeTrack(start time.Time) {
elapsed := time.Since(start)
// Skip this function, and fetch the PC and file for its parent.
pc, _, _, _ := runtime.Caller(1)
// Retrieve a function object this functions parent.
funcObj := runtime.FuncForPC(pc)
// Regex to extract just the function name (and not the module path).
runtimeFunc := regexp.MustCompile(`^.*\.(.*)$`)
name := runtimeFunc.ReplaceAllString(funcObj.Name(), "$1")
log.Println(fmt.Sprintf("DEBUG %s() took %s", name, elapsed))
}
// EOF