-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
70 lines (62 loc) · 1.77 KB
/
env.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 main
import (
"fmt"
"os"
"strconv"
)
// GetEnvVar gets the environment variables. The return argument
// indicates whether or not the function was successful.
func GetEnvVar(env *envVariables) bool {
if !getEnvString("LAB_FILES_BASE_DIRECTORY", &env.labDir) {
return false
}
if !getEnvString("MOSS_FULLY_QUALIFIED_NAME", &env.mossFqn) {
return false
}
if !getEnvString("JPLAG_FULLY_QUALIFIED_NAME", &env.jplagFqn) {
return false
}
if !getEnvString("RESULTS_DIRECTORY", &env.resultsDir) {
return false
}
if !getEnvInt("MOSS_THRESHOLD", &env.mossThreshold) {
return false
}
if !getEnvInt("DUPL_THRESHOLD", &env.duplThreshold) {
return false
}
if !getEnvInt("JPLAG_THRESHOLD", &env.jplagThreshold) {
return false
}
return true
}
// getEnvString gets an environment variable.
// The return argument indicates whether or not the function was successful.
// It takes as input the name of the environment variable,
// and a pointer where to store the result.
func getEnvString(name string, variable *string) bool {
*variable = os.Getenv(name)
if *variable == "" {
fmt.Printf("%s environment variable not set.\n", name)
return false
}
return true
}
// getEnvInt gets an environment variable and converts it to an integer.
// The return argument indicates whether or not the function was successful.
// It takes as input the name of the environment variable,
// and a pointer where to store the result.
func getEnvInt(name string, variable *int) bool {
temp := os.Getenv(name)
if temp == "" {
fmt.Printf("%s environment variable not set.\n", name)
return false
}
var err error
*variable, err = strconv.Atoi(temp)
if err != nil {
fmt.Printf("Could not convert %s environment variable %v to an integer.\n", name, temp)
return false
}
return true
}