diff --git a/.gitignore b/.gitignore index f6e1c08..aecb190 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -gowsdl +./gowsdl myservice diff --git a/cmd/gowsdl/main.go b/cmd/gowsdl/main.go new file mode 100644 index 0000000..3a7b2ce --- /dev/null +++ b/cmd/gowsdl/main.go @@ -0,0 +1,135 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +/* + +Gowsdl generates Go code from a WSDL file. + +This project is originally intended to generate Go clients for WS-* services. + +Usage: gowsdl [options] myservice.wsdl + -o string + File where the generated code will be saved (default "myservice.go") + -p string + Package under which code will be generated (default "myservice") + -v Shows gowsdl version + +Features + +Supports only Document/Literal wrapped services, which are WS-I (http://ws-i.org/) compliant. + +Attempts to generate idiomatic Go code as much as possible. + +Supports WSDL 1.1, XML Schema 1.0, SOAP 1.1. + +Resolves external XML Schemas + +Supports providing WSDL HTTP URL as well as a local WSDL file. + +Not supported + +UDDI. + +TODO + +Add support for filters to allow the user to change the generated code. + +If WSDL file is local, resolve external XML schemas locally too instead of failing due to not having a URL to download them from. + +Resolve XSD element references. + +Support for generating namespaces. + +Make code generation agnostic so generating code to other programming languages is feasible through plugins. + +*/ + +package main + +import ( + "bytes" + "flag" + "go/format" + "log" + "os" + "fmt" + + gen "github.com/hooklift/gowsdl" +) + +const version = "v0.0.1" + +var vers = flag.Bool("v", false, "Shows gowsdl version") +var pkg = flag.String("p", "myservice", "Package under which code will be generated") +var outFile = flag.String("o", "myservice.go", "File where the generated code will be saved") + + +func init() { + log.SetFlags(0) + log.SetOutput(os.Stdout) + log.SetPrefix("🍀 ") +} + +func main() { + flag.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage: %s [options] myservice.wsdl\n", os.Args[0]) + flag.PrintDefaults() + } + + flag.Parse() + + // Show app version + if *vers { + log.Println(version) + os.Exit(0) + } + + if len(os.Args) < 2 { + flag.Usage() + os.Exit(0) + } + + wsdlPath := os.Args[len(os.Args)-1] + + if *outFile == wsdlPath { + log.Fatalln("Output file cannot be the same WSDL file") + } + + // load wsdl + gowsdl, err := gen.NewGoWsdl(wsdlPath, *pkg, false) + if err != nil { + log.Fatalln(err) + } + + // generate code + gocode, err := gowsdl.Start() + if err != nil { + log.Fatalln(err) + } + + pkg := "./" + *pkg + err = os.Mkdir(pkg, 0744) + + fd, err := os.Create(pkg + "/" + *outFile) + if err != nil { + log.Fatalln(err) + } + defer fd.Close() + + data := new(bytes.Buffer) + data.Write(gocode["header"]) + data.Write(gocode["types"]) + data.Write(gocode["operations"]) + data.Write(gocode["soap"]) + + // go fmt the generated code + source, err := format.Source(data.Bytes()) + if err != nil { + fd.Write(data.Bytes()) + log.Fatalln(err) + } + + fd.Write(source) + + log.Println("Done 💩") +}