-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
145 lines (123 loc) · 3.73 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
/*
Package main provides CLI entrypoint
*/
package main
import (
"fmt"
"os"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
flag "github.com/spf13/pflag"
)
// Build time variables.
//
//nolint:gochecknoglobals // Expected to be global
var (
version = "dev"
commit = "none"
date = "unknown"
// Base flags.
workspacePathFlag string
defaultWorkspacePathFlag = "."
configDirFlag string
defaultConfigDirFlag = "config"
templateDirFlag string
defaultTemplateDirFlag = "templates"
yamlAnchorDirFlag string
defaultYamlAnchorDirFlag = "yaml-anchors"
// printImportsFlag bool
// skipImportListFlag []string
// defaultSkipImportListFlag []string = nil.
// Logging flags.
verboseFlag int
quietFlag bool
disableAnsiFlag bool
// Miscellaneous flags.
helpFlag bool
versionFlag bool
)
//nolint:gochecknoinits // Normal CLI flag declaration
func init() {
// init command line flags
flag.StringVarP(&workspacePathFlag, "workspace", "w", defaultWorkspacePathFlag, `Workspace directory`)
flag.StringVarP(&configDirFlag, "config", "c", defaultConfigDirFlag, `Config directory`)
flag.StringVarP(&templateDirFlag, "templates", "t", defaultTemplateDirFlag, `Template directory`)
flag.StringVar(&yamlAnchorDirFlag, "yaml-anchors", defaultYamlAnchorDirFlag, `YAML anchors directory`)
// flag.BoolVar(
// &printImportsFlag,
// "print-imports",
// false,
// "Read terraform files and print related terraform import commands"
// )
// flag.StringSliceVar(
// &skipImportListFlag,
// "skip",
// defaultSkipImportListFlag,
// "Skip provided import from the list"
// )
flag.BoolVarP(&quietFlag, "quiet", "q", false, "Disable output")
flag.CountVarP(&verboseFlag, "verbose", "v", "Enable verbose output. -v for Info, -vv for Debug and -vvv for Trace")
flag.BoolVar(&disableAnsiFlag, "no-ansi", false, "Disable ANSI output")
flag.BoolVarP(&versionFlag, "version", "V", false, `Print current version`)
flag.BoolVarP(&helpFlag, "help", "h", false, "Display this help")
}
func main() {
os.Exit(run())
}
func run() int {
parseFlags()
setupLogOutput(computeLogLevel(), disableAnsiFlag)
terraformDir := "terraform"
log.Debug().Msgf("Workspace: %s", workspacePathFlag)
log.Debug().Msgf("Config directory: %s", configDirFlag)
log.Debug().Msgf("Template directory: %s", templateDirFlag)
log.Debug().Msgf("YAML anchor directory: %s", yamlAnchorDirFlag)
exitCode := 0
switch {
case helpFlag:
flag.PrintDefaults()
case versionFlag:
//nolint:forbidigo // Expected output
fmt.Printf("github-tf version: %s (commit %s from %s)\n", version, commit, date)
/*} else if printImportsFlag {
exitCode = printTerraformImports(filepath.Join(workspacePathFlag, terraformDir), &skipImportListFlag)
*/
default:
exitCode = loadYamlAndWriteTerraform(
workspacePathFlag,
configDirFlag,
templateDirFlag,
terraformDir,
yamlAnchorDirFlag,
)
}
return exitCode
}
func computeLogLevel() zerolog.Level {
switch {
case quietFlag:
return zerolog.Disabled
case verboseFlag == 1:
return zerolog.InfoLevel
case verboseFlag == 2: //nolint:gomnd // Doesn't make sense here to wrap 2
return zerolog.DebugLevel
case verboseFlag > 2: //nolint:gomnd // Doesn't make sense here to wrap 2
return zerolog.TraceLevel
}
return zerolog.WarnLevel
}
func parseFlags() {
// Reset input (useful only for tests execution)
workspacePathFlag = defaultWorkspacePathFlag
configDirFlag = defaultConfigDirFlag
templateDirFlag = defaultTemplateDirFlag
yamlAnchorDirFlag = defaultYamlAnchorDirFlag
// printImportsFlag = false
// skipImportListFlag = defaultSkipImportListFlag
helpFlag = false
verboseFlag = 0
quietFlag = false
disableAnsiFlag = false
versionFlag = false
flag.Parse()
}