forked from hooklift/gowsdl
/
main.go
139 lines (100 loc) · 2.98 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
// 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"
"fmt"
"go/format"
"log"
"os"
gen "github.com/hooklift/gowsdl"
)
// Version is initialized in compilation time by go build.
var Version string
// Name is initialized in compilation time by go build.
var Name string
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")
var insecure = flag.Bool("i", false, "Skips TLS Verification")
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, *insecure)
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)
file, err := os.Create(pkg + "/" + *outFile)
if err != nil {
log.Fatalln(err)
}
defer file.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 {
file.Write(data.Bytes())
log.Fatalln(err)
}
file.Write(source)
log.Println("Done 💩")
}