-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
128 lines (111 loc) · 2.84 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
package main
import (
"fmt"
"io"
"os"
"reflect"
"github.com/blang/semver"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
"github.com/xakep666/wurl/flags"
"github.com/xakep666/wurl/pkg/client"
"github.com/xakep666/wurl/pkg/util"
)
var Version = "0.0.2-alpha"
func main() {
app := &cli.App{
Name: "wurl",
Usage: "console websocket client",
Version: semver.MustParse(Version).String(),
Flags: []cli.Flag{
flags.InsecureSSLFlag,
flags.HeadersFlag,
flags.PingPeriodFlag,
flags.IgnorePingsFlag,
flags.TraceFlag,
flags.ShowHandshakeResponseFlag,
flags.ReadConfigFlag,
flags.SaveConfigToFlag,
flags.OutputFlag,
flags.MessageAfterConnectFlag,
flags.ProxyURLFlag,
// completion
util.InitCompletionFlag,
},
CustomAppHelpTemplate: util.AppHelp,
Commands: nil,
Before: setup,
Action: action,
After: after,
}
if err := app.Run(os.Args); err != nil {
fmt.Printf("ERROR: %v\n", err)
os.Exit(1)
}
}
func setup(ctx *cli.Context) error {
if ctx.NArg() < 1 {
return cli.Exit("Nothing to do.", 1)
}
if ctx.IsSet(util.InitCompletionFlag.Name) {
util.PrintCompletion(ctx)
return cli.Exit("", 0)
}
if ctx.IsSet(flags.ReadConfigFlag.Name) {
loadFromConfig := altsrc.InitInputSourceWithContext(ctx.App.Flags, altsrc.NewTomlSourceFromFlagFunc(flags.ReadConfigFlag.Name))
if err := loadFromConfig(ctx); err != nil {
return cli.Exit(err, 1)
}
}
if err := util.SetupOptions(ctx); err != nil {
return cli.Exit(err, 1)
}
util.SetupLogger(ctx)
logrus.Debugf("running with config %+v", util.MustGetOptions(ctx))
if err := util.SetupClientConstructor(ctx); err != nil {
return cli.Exit(err, 1)
}
return nil
}
func action(ctx *cli.Context) error {
opts := util.MustGetOptions(ctx)
cl, resp, err := util.MustGetClientConstructor(ctx)(ctx.Args().First(), opts)
if opts.ShowHandshakeResponse {
if resp != nil {
resp.Write(os.Stdout)
fmt.Fprintln(os.Stdout)
}
}
if err != nil {
return err
}
defer cl.Close()
if opts.MessageAfterConnect != nil {
if err := cl.WriteMessageFrom(opts.MessageAfterConnect); err != nil {
return err
}
}
err = cl.ReadTo(&client.BinaryCheckWriter{Opts: opts})
switch err {
case nil:
// pass
case client.BinaryOutError:
fmt.Println("WARNING: binary output can mess up your terminal.")
fmt.Printf("Use \"--%[1]s -\" to tell %[2]s to output it to your "+
"terminal anyway, or consider \"--%[1]s <FILE>\" to save to a file.\n", flags.OutputFlag.Name, ctx.App.Name)
return nil
default:
return err
}
return nil
}
func after(ctx *cli.Context) error {
opts := reflect.ValueOf(util.MustGetOptions(ctx)).Elem()
for i := 0; i < opts.NumField(); i++ {
if closer, ok := opts.Field(i).Interface().(io.Closer); ok {
closer.Close()
}
}
return nil
}