-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
175 lines (165 loc) · 4.13 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"encoding/json"
"fmt"
"log"
"net/url"
"os"
"github.com/magodo/azure-rest-api-index/azidx"
"github.com/hashicorp/go-hclog"
"github.com/urfave/cli/v2"
)
var (
flagVerbose bool
flagOutput string
flagDedup string
flagIndex string
flagMethod string
flagURL string
flagSpecDir string
)
func main() {
app := &cli.App{
Name: "azure-rest-api-index",
Version: getVersion(),
Usage: "Index of azure-rest-api-specs",
UsageText: "azure-rest-api-index <command> [option]",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Usage: `Show debug logs`,
Destination: &flagVerbose,
},
},
Commands: []*cli.Command{
{
Name: "build",
Usage: `Building the index`,
UsageText: "azure-rest-api-index build [option] <specdir>",
Before: func(ctx *cli.Context) error {
initLogger()
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: `Output file`,
Destination: &flagOutput,
},
&cli.StringFlag{
Name: "dedup",
Usage: `Deduplicate file`,
Destination: &flagDedup,
},
},
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
return fmt.Errorf("The swagger spec dir not specified")
}
if c.NArg() > 1 {
return fmt.Errorf("More than one arguments specified")
}
specdir := c.Args().First()
index, err := azidx.BuildIndex(specdir, flagDedup)
if err != nil {
return err
}
b, err := json.MarshalIndent(index, "", " ")
if err != nil {
log.Fatal(err)
}
if flagOutput == "" {
fmt.Println(string(b))
return nil
}
return os.WriteFile(flagOutput, b, 0644)
},
},
{
Name: "lookup",
Usage: `Lookup a request's swagger definition based on the index`,
UsageText: "azure-rest-api-index lookup [option]",
Before: func(ctx *cli.Context) error {
initLogger()
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "index",
Usage: `Use the pre-built index file by the "build" subcommand`,
Destination: &flagIndex,
Required: true,
},
&cli.StringFlag{
Name: "method",
Usage: `The request method (e.g. GET)`,
Destination: &flagMethod,
Required: true,
},
&cli.StringFlag{
Name: "url",
Usage: `The request URL`,
Destination: &flagURL,
Required: true,
},
&cli.StringFlag{
Name: "specdir",
Usage: `The spec dir, which is used to generate the Github permlink to the operation (the commit of the repo has to be the same as the index)`,
Destination: &flagSpecDir,
},
},
Action: func(c *cli.Context) error {
b, err := os.ReadFile(flagIndex)
if err != nil {
return fmt.Errorf("reading index file %s: %v", flagIndex, err)
}
var index azidx.Index
if err := json.Unmarshal(b, &index); err != nil {
return fmt.Errorf("unmarshal index file: %v", err)
}
uRL, err := url.Parse(flagURL)
if err != nil {
return fmt.Errorf("parsing URL %s: %v", flagURL, err)
}
ref, err := index.Lookup(flagMethod, *uRL)
if err != nil {
return err
}
pointerTokens := ref.GetPointer().DecodedTokens()
out := fmt.Sprintf(`
Raw : %s
File : %s
Path : %s
Method : %s
`, ref.String(), ref.GetURL().Path, pointerTokens[1], pointerTokens[2])
if index.Commit != "" && flagSpecDir != "" {
link, err := azidx.BuildGithubLink(*ref, index.Commit, flagSpecDir)
if err != nil {
return err
}
out += "Link : " + link + "\n"
}
fmt.Println(out)
return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func initLogger() {
lvl := "INFO"
if flagVerbose {
lvl = "DEBUG"
}
logger := hclog.New(&hclog.LoggerOptions{
Name: "azure-rest-api-index",
Level: hclog.LevelFromString(lvl),
Color: hclog.AutoColor,
})
azidx.SetLogger(logger)
}