-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
215 lines (181 loc) · 5.44 KB
/
main.go
File metadata and controls
215 lines (181 loc) · 5.44 KB
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
213
214
215
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"slices"
"strings"
"github.com/tpriime/ec2diff/pkg"
"github.com/tpriime/ec2diff/pkg/aws"
"github.com/tpriime/ec2diff/pkg/drift"
"github.com/tpriime/ec2diff/pkg/logger"
"github.com/tpriime/ec2diff/pkg/tableprinter"
"github.com/tpriime/ec2diff/pkg/tfstate"
"github.com/tpriime/ec2diff/registry"
)
const (
// Page size per remote or live fetch. Must be greater than 5
fetchPageSize = 100
// Number of concurrent workers. Tune this based on system capacity
driftCheckWorkers = 4
)
func main() {
logger.Init(logger.LevelInfo)
ctx := logger.With(context.Background())
if err := run(ctx, os.Args[1:], os.Stdout); err != nil {
logger.Error(ctx, "Program terminated with error", "error", err)
os.Exit(1)
}
}
// Config holds parsed inputs and injected dependencies for drift checking.
type Config struct {
// CLI args
FilePath string // Path to HCL or tfstate file
Attributes []string // EC2 attributes to compare
ShowHelp bool // Whether to display CLI help
ListAttrs bool // Whether to list supported attributes
// Dependencies
Registry *registry.ParserRegistry
Fetcher pkg.PaginatedLiveFetcher
Checker pkg.DriftChecker
ReportPrinter pkg.ReportPrinter
HelpFn func()
}
// run parses flags and injects default dependencies before executing logic.
func run(ctx context.Context, args []string, out io.Writer) error {
cfg, err := parseFlags(args, out)
if err != nil {
return err
}
// Show help and exit
if cfg.ShowHelp {
cfg.HelpFn()
return nil
}
// List attributes and exit
if cfg.ListAttrs {
fmt.Fprintln(out, "Supported attributes:")
for _, attr := range supportedAttributes() {
fmt.Fprintln(out, " -", attr)
}
return nil
}
// Initialize dependencies
logger.Debug(ctx, "initalzing dependencies")
cfg.Registry = registry.NewParserRegistry([]pkg.Parser{
tfstate.NewTfStateParser(),
})
cfg.Fetcher, err = aws.NewAwsFetcher(ctx, fetchPageSize)
if err != nil {
return fmt.Errorf("failed to init AWS client: %w", err)
}
cfg.Checker = drift.NewDriftChecker(driftCheckWorkers)
cfg.ReportPrinter = tableprinter.NewTablePrinter(out)
return execute(ctx, cfg)
}
// parseFlags reads command-line arguments and returns a populated Config.
func parseFlags(args []string, out io.Writer) (*Config, error) {
fs := flag.NewFlagSet("ec2diff", flag.ContinueOnError)
fs.SetOutput(out)
file := fs.String("file", "", "Path to file (.hcl or .tfstate).")
attrs := fs.String("attrs", "", "Comma-separated attributes to check.")
listAttrs := fs.Bool("list-attributes", false, "List supported attributes.")
showHelp := fs.Bool("h", false, "Show help.")
if err := fs.Parse(args); err != nil {
return nil, err
}
cfg := &Config{
FilePath: *file,
Attributes: parseCommaSep(*attrs),
ListAttrs: *listAttrs,
ShowHelp: *showHelp,
HelpFn: fs.Usage,
}
return cfg, nil
}
// execute performs the parse, fetch, check and report logic based on the provided Config.
func execute(ctx context.Context, cfg *Config) error {
if cfg.FilePath == "" {
cfg.HelpFn()
return errors.New("missing required -file argument")
}
if err := validateAttributes(cfg.Attributes); err != nil {
return err
} else if len(cfg.Attributes) == 0 {
cfg.Attributes = supportedAttributes() // Use all supported attributes if none are specified.
}
parser, ok := cfg.Registry.Get(cfg.FilePath)
if !ok {
return fmt.Errorf("no parser found for file extension %s", filepath.Ext(cfg.FilePath))
}
// Parse local state
state, err := parser.Parse(cfg.FilePath)
if err != nil {
return fmt.Errorf("failed to parse file: %w", err)
}
logger.Info(ctx, fmt.Sprintf("Found %d instances in file", len(state)), "file", cfg.FilePath)
// Fetch and compare instances
reports, err := fetchAndCompare(ctx, cfg, state)
if err != nil {
return fmt.Errorf("failed to check drifts: %w", err)
}
logger.Info(ctx, fmt.Sprintf("Generated %d reports in total", len(reports)))
// Display report
cfg.ReportPrinter.Print(reports)
return nil
}
// fetchAndCompare fetches live ec2 resources and checks for drifts per page
func fetchAndCompare(ctx context.Context, cfg *Config, state pkg.InstanceMap) ([]pkg.Report, error) {
reports := []pkg.Report{}
err := cfg.Fetcher.Fetch(ctx, func(page int, live pkg.InstanceMap) bool {
ctx := logger.With(ctx, "batch", page)
logger.Info(ctx, "Checking for drifts in batch...")
// Check for drifts and report
rpts := cfg.Checker.CheckDrift(ctx, live, state, cfg.Attributes)
reports = append(reports, rpts...)
return true
})
return reports, err
}
// parseCommaSep splits a comma-separated string into a clean string slice.
func parseCommaSep(input string) []string {
if input == "" {
return nil
}
parts := strings.Split(input, ",")
var clean []string
for _, p := range parts {
p = strings.TrimSpace(p)
if p != "" {
clean = append(clean, p)
}
}
return clean
}
// validateAttributes ensures each input attribute is supported.
func validateAttributes(attrs []string) error {
if len(attrs) == 0 {
return nil
}
supported := supportedAttributes()
for _, attr := range attrs {
if !slices.Contains(supported, attr) {
return fmt.Errorf("attribute '%s' not supported. Supported attributes: %v", attr, supported)
}
}
return nil
}
func supportedAttributes() []string {
return []string{
pkg.AttrInstanceType,
pkg.AttrInstanceState,
pkg.AttrKeyName,
pkg.AttrTags,
pkg.AttrSecurityGroups,
pkg.AttrPublicIP,
}
}