forked from fiorix/wsdl2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (81 loc) · 1.69 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
package main
import (
"crypto/tls"
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"github.com/fiorix/wsdl2go/wsdl"
"github.com/fiorix/wsdl2go/wsdlgo"
)
var version = "tip"
func main() {
opts := struct {
Src string
Dst string
Insecure bool
Version bool
}{}
flag.StringVar(&opts.Src, "i", opts.Src, "input file, url, or '-' for stdin")
flag.StringVar(&opts.Dst, "o", opts.Dst, "output file, or '-' for stdout")
flag.BoolVar(&opts.Insecure, "yolo", opts.Insecure, "accept invalid https certificates")
flag.BoolVar(&opts.Version, "version", opts.Version, "show version and exit")
flag.Parse()
if opts.Version {
fmt.Printf("wsdl2go %s\n", version)
return
}
var w io.Writer
switch opts.Dst {
case "", "-":
w = os.Stdout
default:
f, err := os.OpenFile(opts.Dst, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
w = f
}
cli := http.DefaultClient
if opts.Insecure {
cli.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
err := decode(w, opts.Src, cli)
if err != nil {
log.Fatal(err)
}
}
func decode(w io.Writer, src string, cli *http.Client) error {
var err error
var f io.ReadCloser
if src == "" || src == "-" {
f = os.Stdin
} else if f, err = open(src, cli); err != nil {
return err
}
d, err := wsdl.Unmarshal(f)
if err != nil {
return err
}
f.Close()
enc := wsdlgo.NewEncoder(w)
enc.SetClient(cli)
return enc.Encode(d)
}
func open(name string, cli *http.Client) (io.ReadCloser, error) {
u, err := url.Parse(name)
if err != nil || u.Scheme == "" {
return os.Open(name)
}
resp, err := cli.Get(name)
if err != nil {
return nil, err
}
return resp.Body, err
}