-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIsUserInApple.go
98 lines (77 loc) · 2.01 KB
/
IsUserInApple.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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"sort"
"strings"
)
func main() {
// Define command line options
configPtr := flag.String("config", "./IsUserInApple.json", "Configuration file")
usernamePtr := flag.String("username", "", "Username to find (in quotes)")
userFileListPtr := flag.String("userlist", "", "list of usernames")
flag.Parse()
var userName string = *usernamePtr
var userFileList string = *userFileListPtr
var ConfigFileName string = *configPtr
if (len(userName) == 0) && (len(userFileList) == 0) {
fmt.Println("Please specify an username or a list of users to match")
flag.PrintDefaults()
os.Exit(1)
}
// If a config file was not specified from the commmand line, assume it's
// in the same folder as the executable
if len(ConfigFileName) == 0 {
ex, err := os.Executable()
if err != nil {
panic(err)
}
ConfigFileName = filepath.Join(filepath.Dir(ex), "IsUserInApple.json")
}
if _, err := os.Stat(ConfigFileName); os.IsNotExist(err) {
fmt.Println(err)
os.Exit(2)
}
var users []string
var err error
if len(userFileList) > 0 {
if _, err := os.Stat(userFileList); os.IsNotExist(err) {
fmt.Println(err)
os.Exit(2)
}
users, err = readLines(userFileList)
if err != nil {
log.Fatalf("readLines: %s", err)
}
// Sort the list so that if there are duplicates, we can skip over the dups
sort.Strings(users)
} else {
// Treat a single user name as an array of 1 element
fmt.Println("Looking for " + userName)
users = append(users, userName)
}
config, err := ReadConfig(ConfigFileName)
if err != nil {
fmt.Println(err)
os.Exit(3)
}
CheckUsers(config, users)
}
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
// Close the file when the readLines function returns
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, strings.ToLower(scanner.Text()))
}
return lines, scanner.Err()
}