/
main.go
153 lines (130 loc) · 3.59 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
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"strings"
"github.com/ms-henglu/pal/formatter"
"github.com/ms-henglu/pal/formatter/azapi"
"github.com/ms-henglu/pal/trace"
)
const version = "0.4.0"
var showHelp = flag.Bool("help", false, "Show help")
var showVersion = flag.Bool("version", false, "Show version")
func main() {
input := ""
jsonInput := false
output := ""
mode := ""
flag.StringVar(&input, "i", "", "Input terraform text log file")
flag.BoolVar(&jsonInput, "j", false, "Input terraform file is json")
flag.StringVar(&output, "o", "", "Output directory")
flag.StringVar(&mode, "m", "markdown", "Output format, allowed values are `markdown`, `oav` and `azapi`")
// backward compatibility, the first argument is the input file
if len(os.Args) == 2 {
if _, err := os.Stat(os.Args[1]); err == nil {
input = os.Args[1]
mode = "markdown"
}
}
if input == "" {
flag.Parse()
if *showHelp {
flag.Usage()
os.Exit(0)
}
if *showVersion {
fmt.Println(version)
os.Exit(0)
}
}
if input == "" {
flag.Usage()
log.Fatalf("[ERROR] input text or json file is required")
}
if output == "" {
output = path.Dir(input)
}
log.Printf("[INFO] input file: %s", input)
log.Printf("[INFO] output directory: %s", output)
log.Printf("[INFO] output format: %s", mode)
// switch between different file parser implementations.
traceFormat := trace.TextParser
if jsonInput {
traceFormat = trace.JsonParser
}
traces, err := trace.NewRequestTraceParser(traceFormat).ParseFromFile(input)
if err != nil {
log.Fatalf("[ERROR] failed to parse request traces: %v", err)
}
for _, t := range traces {
if out := trace.VerifyRequestTrace(t); len(out) > 0 {
log.Printf("[WARN] verification failed: url %s\n%s", t.Url, strings.Join(out, "\n"))
}
}
switch mode {
case "oav":
format := formatter.OavTrafficFormatter{}
files, err := os.ReadDir(output)
if err != nil {
log.Fatalf("[ERROR] failed to read output directory: %v", err)
}
index := len(files)
for _, t := range traces {
out := format.Format(t)
index = index + 1
outputPath := path.Join(output, fmt.Sprintf("trace-%d.json", index))
if err := os.WriteFile(outputPath, []byte(out), 0644); err != nil {
log.Fatalf("[ERROR] failed to write file: %v", err)
}
log.Printf("[INFO] output file: %s", outputPath)
}
case "markdown":
content := markdownPrefix
format := formatter.MarkdownFormatter{}
for _, t := range traces {
content += format.Format(t)
}
outputPath := path.Clean(path.Join(output, "output.md"))
if err := os.WriteFile(outputPath, []byte(content), 0644); err != nil {
log.Fatalf("[ERROR] failed to write file: %v", err)
}
log.Printf("[INFO] output file: %s", outputPath)
case "azapi":
content := azapiPrefix
format := azapi.AzapiFormatter{}
for _, t := range traces {
if res := format.Format(t); res != "" {
content += res
content += "\n"
}
}
outputPath := path.Clean(path.Join(output, "pal-main.tf"))
if err := os.WriteFile(outputPath, []byte(content), 0644); err != nil {
log.Fatalf("[ERROR] failed to write file: %v", err)
}
log.Printf("[INFO] output file: %s", outputPath)
default:
log.Fatalf("[ERROR] unsupported output format: %s", mode)
}
}
const markdownPrefix = `<!--
Tips:
1. Use Markdown preview mode to get a better reading experience.
2. If you want to select some of the request traces, in VSCode, use shortcut "Ctrl + K, 0" to fold all blocks.
-->
`
const azapiPrefix = `
terraform {
required_providers {
azapi = {
source = "Azure/azapi"
}
}
}
provider "azapi" {
skip_provider_registration = false
}
`