-
Notifications
You must be signed in to change notification settings - Fork 71
/
main.go
82 lines (70 loc) · 1.51 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
package main
import (
"fmt"
"io"
"net/url"
"os"
"os/user"
"path/filepath"
"github.com/chzyer/readline"
"github.com/spf13/cobra"
)
const Version = "0.2.1"
var options struct {
origin string
printVersion bool
insecure bool
}
func main() {
rootCmd := &cobra.Command{
Use: "ws URL",
Short: "websocket tool",
Run: root,
}
rootCmd.Flags().StringVarP(&options.origin, "origin", "o", "", "websocket origin")
rootCmd.Flags().BoolVarP(&options.printVersion, "version", "v", false, "print version")
rootCmd.Flags().BoolVarP(&options.insecure, "insecure", "k", false, "skip ssl certificate check")
rootCmd.Execute()
}
func root(cmd *cobra.Command, args []string) {
if options.printVersion {
fmt.Printf("ws v%s\n", Version)
os.Exit(0)
}
if len(args) != 1 {
cmd.Help()
os.Exit(1)
}
dest, err := url.Parse(args[0])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
var origin string
if options.origin != "" {
origin = options.origin
} else {
originURL := *dest
if dest.Scheme == "wss" {
originURL.Scheme = "https"
} else {
originURL.Scheme = "http"
}
origin = originURL.String()
}
var historyFile string
user, err := user.Current()
if err == nil {
historyFile = filepath.Join(user.HomeDir, ".ws_history")
}
err = connect(dest.String(), origin, &readline.Config{
Prompt: "> ",
HistoryFile: historyFile,
}, options.insecure)
if err != nil {
fmt.Fprintln(os.Stderr, err)
if err != io.EOF && err != readline.ErrInterrupt {
os.Exit(1)
}
}
}