-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (75 loc) · 2.14 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/kics223w1/swagbridge/postman"
"github.com/kics223w1/swagbridge/swagger"
)
func main() {
// Define command line flags
inputFile := flag.String("i", "", "Path to the input JSON file")
schema := flag.String("s", "https", "Schema to use (http, https, ws, wss)")
host := flag.String("h", "", "Host for the API endpoints")
outputFile := flag.String("o", "postman_collection.json", "Output file path for the Postman collection")
// Parse flags
flag.Parse()
// Validate required flags
if *inputFile == "" || *host == "" {
flag.Usage()
os.Exit(1)
}
// Ensure output file has .json extension
if !strings.HasSuffix(*outputFile, ".json") {
*outputFile = *outputFile + ".json"
}
// If output path doesn't contain directory, use current directory
outputPath := *outputFile
if !strings.Contains(outputPath, string(os.PathSeparator)) {
currentDir, err := os.Getwd()
if err != nil {
log.Fatalf("Error getting current directory: %v\n", err)
}
outputPath = filepath.Join(currentDir, outputPath)
}
// Create output directory if it doesn't exist
outputDir := filepath.Dir(outputPath)
if err := os.MkdirAll(outputDir, 0755); err != nil {
log.Fatalf("Error creating output directory: %v\n", err)
}
// Read file content
content, err := os.ReadFile(*inputFile)
if err != nil {
log.Fatalf("Error reading file: %v\n", err)
}
// Validate JSON format
if !json.Valid(content) {
log.Fatalf("Error: Input file is not valid JSON\n")
}
// Parse the swagger specification
spec, err := swagger.ParseSwagger(content)
if err != nil {
log.Fatalf("Error parsing swagger: %v\n", err)
}
// Generate Postman collection
collection, err := postman.GeneratePostmanCollection(spec, *host, *schema)
if err != nil {
log.Fatal(err)
}
// Convert to JSON
jsonData, err := json.MarshalIndent(collection, "", " ")
if err != nil {
log.Fatal(err)
}
// Write to file
err = ioutil.WriteFile(outputPath, jsonData, 0644)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Successfully generated Postman collection at: %s\n", outputPath)
}