github.com/grokify/go-ringcentral-client@v0.3.31/codegen/merge.go (about) 1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 "path/filepath" 8 9 "github.com/grokify/mogo/fmt/fmtutil" 10 "github.com/grokify/mogo/path/filepathutil" 11 "github.com/grokify/spectrum/openapi2" 12 "github.com/grokify/spectrum/openapi3" 13 "github.com/jessevdk/go-flags" 14 ) 15 16 type options struct { 17 Directory string `short:"d" long:"directory" description:"OAS Directory" required:"true"` 18 Version int `short:"v" long:"version" description:"OAS Version" required:"true"` 19 } 20 21 func main() { 22 if 1 == 0 { 23 // file := "specs-voice_v3.0.0/openapi-spec_campaigns.json" 24 // file = "specs-voice_v3.0.0/openapi-spec_countries.json" 25 // file = "specs-example_v3.0.0.json" 26 // file = "specs-voice_v3.0.0/openapi-spec_agent-groups.json" 27 file := "specs-voice_v3.0.0/openapi-spec_agents.json" 28 if 1 == 1 { 29 spec, err := openapi3.ReadFile(file, false) 30 if err != nil { 31 fmt.Println("TEST_UNMARSHAL") 32 log.Fatal(err) 33 } 34 fmtutil.MustPrintJSON(spec) 35 name := "Country" 36 fmtutil.MustPrintJSON(spec.Components.Schemas[name]) 37 38 moreSpec := openapi3.SpecMore{Spec: spec} 39 40 fmt.Printf("HAS [%v][%v]\n", name, moreSpec.SchemaNameExists(name, false)) 41 err = spec.Validate(context.Background()) 42 if err != nil { 43 log.Fatal(err) 44 } 45 panic("READ_FILE_NEW") 46 } 47 spec, err := openapi3.ReadFile(file, true) 48 if err != nil { 49 fmt.Println("TEST_LOADER") 50 log.Fatal(err) 51 } 52 fmtutil.MustPrintJSON(spec) 53 err = spec.Validate(context.Background()) 54 if err != nil { 55 fmt.Println("TEST") 56 log.Fatal(err) 57 } 58 fmtutil.MustPrintJSON(spec) 59 60 fmt.Println("DONE") 61 panic("Z") 62 } 63 opts := options{} 64 _, err := flags.Parse(&opts) 65 if err != nil { 66 log.Fatal(err) 67 } 68 69 version := 2 70 if opts.Version == 2 || opts.Version == 3 { 71 version = opts.Version 72 } 73 dir := opts.Directory 74 75 dir = filepathutil.TrimRight(dir) 76 77 _, leaf := filepath.Split(dir) 78 79 outfile := leaf + ".json" 80 81 switch version { 82 case 2: 83 err = openapi2.WriteFileDirMerge(outfile, dir, 0644) 84 case 3: 85 _, err = openapi3.WriteFileDirMerge(outfile, dir, 0644, nil) 86 } 87 if err != nil { 88 log.Fatal(err) 89 } 90 91 fmt.Println("DONE") 92 }