forked from mmatczuk/go-http-tunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
106 lines (91 loc) · 2.25 KB
/
options.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
// Copyright (C) 2017 Michał Matczuk
// Use of this source code is governed by an AGPL-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"os"
)
const usage1 string = `Usage: tunnel [OPTIONS] <command> [command args] [...]
options:
`
const usage2 string = `
Commands:
tunnel id Show client identifier
tunnel list List tunnel names from config file
tunnel start [tunnel] [...] Start tunnels by name from config file
tunnel start-all Start all tunnels defined in config file
Examples:
tunnel start www ssh
tunnel -config config.yaml -log-level 2 start ssh
tunnel start-all
config.yaml:
server_addr: SERVER_IP:5223
tunnels:
webui:
proto: http
addr: localhost:8080
auth: user:password
host: webui.my-tunnel-host.com
ssh:
proto: tcp
addr: 192.168.0.5:22
remote_addr: 0.0.0.0:22
Author:
Written by M. Matczuk ([email protected])
Bugs:
Submit bugs to https://github.com/mmatczuk/go-http-tunnel/issues
`
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, usage1)
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, usage2)
}
}
type options struct {
config string
logLevel int
version bool
command string
args []string
}
func parseArgs() (*options, error) {
config := flag.String("config", "tunnel.yml", "Path to tunnel configuration file")
logLevel := flag.Int("log-level", 1, "Level of messages to log, 0-3")
version := flag.Bool("version", false, "Prints tunnel version")
flag.Parse()
opts := &options{
config: *config,
logLevel: *logLevel,
version: *version,
command: flag.Arg(0),
}
if opts.version {
return opts, nil
}
switch opts.command {
case "":
flag.Usage()
os.Exit(2)
case "id", "list":
opts.args = flag.Args()[1:]
if len(opts.args) > 0 {
return nil, fmt.Errorf("list takes no arguments")
}
case "start":
opts.args = flag.Args()[1:]
if len(opts.args) == 0 {
return nil, fmt.Errorf("you must specify at least one tunnel to start")
}
case "start-all":
opts.args = flag.Args()[1:]
if len(opts.args) > 0 {
return nil, fmt.Errorf("start-all takes no arguments")
}
default:
return nil, fmt.Errorf("unknown command %q", opts.command)
}
return opts, nil
}