-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
212 lines (190 loc) · 6.12 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
"codeAudit/checks"
"codeAudit/models"
"codeAudit/utils"
)
func main() {
downloadsFolderPath := "./"
userHomeDir, userHomeErr := os.UserHomeDir()
shouldGenerateFailureReport := true
if userHomeErr != nil {
utils.WarningPrintLn(fmt.Sprintf("Error occurred finding home directory: %s", userHomeErr.Error()))
}
system := utils.GetOs()
if system == "darwin" {
downloadsFolderPath = fmt.Sprintf("%s/Downloads", userHomeDir)
}
if system == "windows" {
downloadsFolderPath = fmt.Sprintf("%s/Downloads", userHomeDir)
}
if system == "linux" {
downloadsFolderPath = fmt.Sprintf("/home%s/Downloads", userHomeDir)
}
var fileType string
var check string
var verboseMode bool = false
root_directory := "./"
defaultCharacterLimit := 100
defaultVariableNamingConvention := "camel"
defaultIndentationSpaces := 2
defaultSemiColonUsage := true
terminalArgs := os.Args[1:]
if len(terminalArgs) == 0 {
fileType = ".js"
check = "all"
}
if len(terminalArgs) >= 1 {
if !utils.IsConfigFlag(terminalArgs[0]) {
check = terminalArgs[0]
}
fileType = ".js"
}
if len(terminalArgs) >= 2 {
if !utils.IsConfigFlag(terminalArgs[1]) {
if !utils.IsConfigFlag(terminalArgs[0]) {
check = terminalArgs[0]
}
isFlag, err := regexp.MatchString("-", terminalArgs[1])
if err != nil {
utils.ErrorPrintLn("Unexpected error occurred while checking for flag")
return
}
if isFlag {
flagValue := strings.Split(terminalArgs[1], "-")[1]
options := strings.Split(flagValue, "")
switch options[0] {
case "j":
fileType = ".js"
case "p":
fileType = ".py"
// case "t":
// fileType = ".ts"
// case "g":
// fileType = ".go"
default:
utils.WarningPrintLn("Invalid file type provided. Using default file type (.js)")
fileType = ".js"
}
}
}
}
if len(terminalArgs) >= 3 {
if !utils.IsConfigFlag(terminalArgs[2]) {
root_directory = terminalArgs[2]
}
}
for a := 0; a < len(terminalArgs); a++ {
arg := terminalArgs[a]
if utils.IsConfigFlag(arg) {
configTuple := strings.Split(arg, "=")
option := strings.Split(configTuple[0], "--")[1]
value := configTuple[1]
switch option {
case "i":
newIndentation, err := strconv.Atoi(value)
if err != nil {
utils.WarningPrintLn(fmt.Sprintf("Input %s wasn't able to be converted to a number to use as new number of indentation. Used default value of 2\n", value))
} else {
defaultIndentationSpaces = newIndentation
}
case "c":
newCharacterLineLimit, err := strconv.Atoi(value)
if err != nil {
utils.WarningPrintLn(fmt.Sprintf("Input %s wasn't able to be converted to a number to use as new character limit. Used default value of 100", value))
} else {
defaultCharacterLimit = newCharacterLineLimit
}
case "n":
isValidConventions := utils.IsValidNamingConvention(value)
if !isValidConventions {
utils.WarningPrintLn(fmt.Sprintf("%s isn't a valid or supported variable naming convention. See docs", value))
} else {
defaultVariableNamingConvention = value
}
case "r":
willGenerateReport, err := strconv.ParseBool(value)
if err != nil {
utils.WarningPrintLn(fmt.Sprintf("%s isn't a valid boolean value", value))
} else {
shouldGenerateFailureReport = willGenerateReport
}
case "v":
verbose, err := strconv.ParseBool(value)
if err != nil {
utils.WarningPrintLn(fmt.Sprintf("%s isn't a valid boolean value", value))
} else {
verboseMode = verbose
}
case "s":
newSemiColonUsage, err := strconv.ParseBool(value)
if err != nil {
utils.WarningPrintLn(fmt.Sprintf("%s isn't a valid boolean value", value))
} else {
defaultSemiColonUsage = newSemiColonUsage
}
}
}
}
if verboseMode {
utils.WarningPrintLn("\nVerbose mode enabled\n")
}
utils.DefaultPrintLn(fmt.Sprintf("Directory testing: %s\n", root_directory))
var report models.ConsistencyReport
var foundFiles []string
checksRan := true
switch check {
case "all":
result, files := checks.PerformAllChecks(root_directory, fileType, defaultVariableNamingConvention, defaultIndentationSpaces, defaultCharacterLimit, defaultSemiColonUsage)
report = result
foundFiles = files
case "indent":
result, files := checks.PerformIndentationChecks(root_directory, fileType, defaultIndentationSpaces)
report = result
foundFiles = files
case "naming":
result, files := checks.PerformVariableNamingChecks(root_directory, fileType, defaultVariableNamingConvention)
report = result
foundFiles = files
case "char":
result, files := checks.PerformCharacterCountChecks(root_directory, fileType, defaultCharacterLimit)
report = result
foundFiles = files
case "semi":
result, files := checks.PerformSemiColonChecks(root_directory, fileType, defaultSemiColonUsage)
report = result
foundFiles = files
default:
checksRan = false
utils.ErrorPrintLn("unexpected command please try again")
}
if len(foundFiles) == 0 {
utils.ErrorPrintLn("No files found in the specified directory matching the file type provided.")
return
}
if verboseMode {
utils.WarningPrintLn("\nConfigurations Values used\n")
utils.DefaultPrintLn(fmt.Sprintf("Line Character Limit: %d", defaultCharacterLimit))
utils.DefaultPrintLn(fmt.Sprintf("Variable Naming Convention: %s", defaultVariableNamingConvention))
utils.DefaultPrintLn(fmt.Sprintf("Number of Indentation Spaces: %d", defaultIndentationSpaces))
utils.DefaultPrintLn(fmt.Sprintf("Allow Lines Ending In Semi-Colon: %t\n", defaultSemiColonUsage))
}
if checksRan {
utils.DefaultPrintLn(fmt.Sprintf("Number of files tested: %d", len(foundFiles)))
utils.DefaultPrintLn(fmt.Sprintf("Number of issues found: %d\n", len(report.IssuesFound)))
if shouldGenerateFailureReport {
reportCreated := utils.GenerateFailureReport(report, downloadsFolderPath)
if !reportCreated {
utils.WarningPrintLn("Error occurred while generating report")
} else {
utils.SuccessPrintLn("Report generated successfully\n")
utils.DefaultPrintLn(fmt.Sprintf("Full Report can be found at %s/CodeAuditReport.csv\n", downloadsFolderPath))
}
}
}
}