-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (101 loc) · 2.88 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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/warrensbox/jscheck/lib"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
/*
* Version 0.1.0
* Compatible with Mac OS X AND LINUX ONLY
*/
/*** OPERATION WORKFLOW ***/
var version = "0.1.0\n"
var (
debugFlag *bool
versionFlag *bool
directory *string
)
func init() {
const (
cmdDesc = "Install github app binaries on your local machine. Ex: hubapp install mmmorris1975/aws-runas"
versionFlagDesc = "Displays the version of hubapp"
actionArgDesc = "Provide action needed. Ex: install, update, or uninstall"
giturlArgDesc = "Provide giturl in user/repo format. Ex: mmmorris1975/aws-runas"
)
versionFlag = kingpin.Flag("version", versionFlagDesc).Short('v').Bool()
directory = kingpin.Flag("directory", versionFlagDesc).Short('d').String()
}
func main() {
validJSON := true
dir := "."
kingpin.CommandLine.Interspersed(false)
kingpin.Parse()
//Map of allowed extensions for variable
//Currently it only accepts 5 extension
var allowedExt = make(map[string]bool)
allowedExt[".tfvars"] = true
allowedExt[".tfvars.json"] = true
allowedExt[".auto.tfvars"] = true
allowedExt[".auto.tfvars.json"] = true
allowedExt[".json"] = true
//allowedExt[".properties"] = true needs more testing
if *versionFlag {
fmt.Printf("Version : %s\n", version)
os.Exit(0)
}
if *directory != "" {
dir = *directory
}
//Walking through current directory
errWalking := filepath.Walk(dir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println("Error walking through directory")
return err
}
//Get path of sub driectory and files
filePath, errPath := os.Stat(path)
mode := filePath.Mode()
if errPath != nil {
fmt.Println("Error getting path!")
}
//if if it is a file; continue
if mode.IsRegular() {
fileExt := filepath.Ext(path) //get file extension
if _, ok := allowedExt[fileExt]; ok { //check if it's an acceptable extension
fileContent, _ := ioutil.ReadFile(path)
fileStr := string(fileContent) //read file content and convert content into string
if len(fileStr) > 0 { //if file content > 0; continue
//get the first character in file
openChar := fileStr[0:3]
openChar = lib.RemoveNewLine(openChar)
//get the last character in file
closeChar := fileStr[len(fileContent)-2:]
closeChar = lib.RemoveNewLine(closeChar)
//if the file starts with { and }, it assume that it is a json file; continue
if openChar == "{" && closeChar == "}" {
//check if it's a valid json file
valid := lib.IsJSON(fileStr)
if !valid {
fmt.Println("ERROR in JSON:")
fmt.Println(path)
validJSON = false
}
}
}
}
}
return nil
})
if errWalking != nil {
log.Println(errWalking)
}
if !validJSON {
os.Exit(1)
}
os.Exit(0)
}