-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcli.go
96 lines (77 loc) · 2.18 KB
/
cli.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
// 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/.
package main
import (
"bytes"
"go/format"
"log"
"os"
"runtime"
gen "github.com/cdwlabs/gowsdl/generator"
flags "github.com/jessevdk/go-flags"
)
const version = "v0.0.1, with mods by CDW Labs"
var opts struct {
Version bool `short:"v" long:"version" description:"Shows gowsdl version"`
Package string `short:"p" long:"package" description:"Package under which code will be generated" default:"myservice"`
OutputFile string `short:"o" long:"output" description:"File where the generated code will be saved" default:"myservice.go"`
IgnoreTls bool `short:"i" long:"ignore-tls" description:"Ignores invalid TLS certificates. It is not recomended for production. Use at your own risk" default:"false"`
}
func init() {
if os.Getenv("GOMAXPROCS") == "" {
runtime.GOMAXPROCS(runtime.NumCPU())
}
log.SetFlags(0)
log.SetOutput(os.Stdout)
log.SetPrefix("🍀 ")
}
func main() {
args, err := flags.Parse(&opts)
if err != nil {
os.Exit(1)
}
if opts.Version {
log.Println(version)
os.Exit(0)
}
if len(args) == 0 {
log.Fatalln("WSDL file is required to start the party")
}
if opts.OutputFile == args[0] {
log.Fatalln("Output file cannot be the same WSDL file")
}
gowsdl, err := gen.NewGoWsdl(args[0], opts.Package, opts.IgnoreTls)
if err != nil {
log.Fatalln(err)
}
gocode, err := gowsdl.Start()
if err != nil {
log.Fatalln(err)
}
pkg := "./" + opts.Package
err = os.Mkdir(pkg, 0744)
if perr, ok := err.(*os.PathError); ok && os.IsExist(perr.Err) {
log.Printf("Package directory %s already exist, skipping creation\n", pkg)
} else {
if err != nil {
log.Fatalln(err)
}
}
fd, err := os.Create(pkg + "/" + opts.OutputFile)
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"])
source, err := format.Source(data.Bytes())
if err != nil {
fd.Write(data.Bytes())
log.Fatalln(err)
}
fd.Write(source)
log.Println("Done 💩")
}