-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
125 lines (115 loc) · 3.15 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
package main
import (
"flag"
"fmt"
"github.com/fatih/color"
"github.com/maskimko/go-3ff/hclparser"
"github.com/maskimko/go-3ff/utils"
"log"
"os"
"strings"
)
//Version info
var major string
var minor string
var revision string
var Logger *log.Logger
//optionSet is a CLI flags holder
type optionSet struct {
version *bool
debug *bool
trfout *bool
nopb *bool
//Source file/directoryl
s *string
//Modified file/directoryuint8
m *string
}
//checkEnv function checks environment variables for a debug flag
func checkEnv(opts *optionSet) {
if _, ok := os.LookupEnv("3FF_DEBUG"); ok {
debug := true
opts.debug = &debug
}
}
//main function starts the binary
func main() {
opts := parseOptions()
checkEnv(opts)
green := color.New(color.FgGreen).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
Logger = log.New(os.Stderr, "", 0)
if *opts.debug {
Logger.Printf("Program was been launched with this arguments \"%s\"", strings.Join(os.Args, " "))
}
hclparser.Debug = *opts.debug
hclparser.TerraformOutput = *opts.trfout
var res *hclparser.ModifiedResources
if *opts.debug && *opts.trfout {
Logger.Println("Warning! Debug output can spoil terraform output for consumption")
}
orig, err := os.Open(*opts.s)
if err != nil {
log.Fatalf("Cannot open file %s. Error: %s", *opts.s, err)
}
modif, err := os.Open(*opts.m)
res, err = hclparser.CompareFiles(orig, modif)
if err != nil {
log.Fatalf("Cannot open file %s. Error: %s", *opts.m, err)
}
if *opts.trfout {
for _, r := range *res.List() {
if strings.HasPrefix(r, "resource") || strings.HasPrefix(r, "module") || strings.HasPrefix(r, "data") {
fmt.Println(strings.Replace(strings.Split(r, "/")[0], "resource.", "", 1))
}
}
}
if res.IsEmpty() {
if *opts.debug {
Logger.Println(green("OK! No changes"))
}
} else {
if *opts.debug {
Logger.Println(red("There are changes"))
}
}
if *opts.debug {
Logger.Printf("File cache hits: %d", utils.CacheHits)
}
}
//parseOptions function parses the command line arguments
func parseOptions() *optionSet {
o := optionSet{}
o.debug = flag.Bool("d", false, "Enable debug output to StdErr")
o.version = flag.Bool("version", false, "Show version info")
o.trfout = flag.Bool("t", false, "Output modified resources only (For terraform command)")
//o.nopb = flag.Bool("nopb", false, "Disable displaying of progress bar")
flag.Parse()
if *o.version {
fmt.Printf("Version: v%s.%s.%s\n", major, minor, revision)
os.Exit(0)
}
switch len(flag.Args()) {
case 0:
log.Fatalf("%s You have to specify files or directories for comparison", color.RedString("Error:"))
case 1:
if *o.debug {
Logger.Println("If you specify only one argument as source directory outside git.go context, the current directory will be treated as a modified file directory")
}
sd := flag.Arg(0)
o.s = &sd
wd, err := os.Getwd()
if err != nil {
log.Fatalf("Cannot get cuurrent working directory: %s", err)
}
o.m = &wd
case 2:
sd := flag.Arg(0)
o.s = &sd
md := flag.Arg(1)
o.m = &md
default:
log.Fatalln("Outside git.go context you must specify two arguments <source> <modified> file/directory to perform diff calculation")
}
return &o
}