-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (55 loc) · 1.36 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
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/RPGillespie6/typed-fetch/pkg/typedfetch"
"github.com/swaggest/openapi-go/openapi31"
)
func main() {
openApiSpecPath := flag.String("openapi", "", "Input file path (.json or .yaml)")
outputPath := flag.String("output", "", "Output file path")
flag.Parse()
if *openApiSpecPath == "" {
panic("openapi document path is required")
}
reflector, err := openApi31ReflectorFromFile(*openApiSpecPath)
if err != nil {
panic(err)
}
generatedOutput, err := typedfetch.GenerateTypedFetch(reflector)
if err != nil {
panic(err)
}
// Generate typed fetch
if *outputPath != "" {
err = os.WriteFile(*outputPath, []byte(generatedOutput), 0644)
if err != nil {
panic(err)
}
} else {
fmt.Println(generatedOutput)
}
}
func openApi31ReflectorFromFile(path string) (*openapi31.Reflector, error) {
specBytes, err := os.ReadFile(path)
if err != nil {
return nil, err
}
reflector := openapi31.NewReflector()
if strings.HasSuffix(path, ".json") {
err = reflector.Spec.UnmarshalJSON(specBytes)
if err != nil {
return nil, err
}
} else if strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml") {
err = reflector.Spec.UnmarshalYAML(specBytes)
if err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("Unsupported file format: %s", path)
}
return reflector, nil
}