-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
139 lines (109 loc) · 3.72 KB
/
Copy pathcli.go
File metadata and controls
139 lines (109 loc) · 3.72 KB
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
129
130
131
132
133
134
135
136
137
138
139
/*
Copyright 2021 Mark Wilkerson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>
*/
package main
import (
"fmt"
"io"
"os"
flag "github.com/spf13/pflag"
"markhuge.com/donate"
)
type options struct {
BindAddr, NameSpace, Dest string
Port int
Debug bool
SSLCert string
SSLKeyFile string
}
func Init() options {
var opts options
flags := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flags.StringVarP(&opts.Dest, "dest", "d", "", "Destination URI. Ex: https://git.markhuge.com")
flags.IntVarP(&opts.Port, "port", "p", 0, "Port to listen on")
flags.StringVar(&opts.BindAddr, "bind", "0.0.0.0", "Optional bind address")
flags.StringVarP(&opts.NameSpace, "namespace", "n", "", "Optional namespace Ex: markhuge.com. Default is the host in the request header")
flags.BoolVar(&opts.Debug, "verbose", false, "Verbose logging")
flags.StringVar(&opts.SSLCert, "ssl-cert", "", "Path to fully concatinated SSL certificate. Used optionally to enable SSL and serve HTTPS. (--ssl-key is also required with this option)")
flags.StringVar(&opts.SSLKeyFile, "ssl-key", "", "Path to SSL Keyfile (ex: key.pem). Used in conjunction with --ssl-cert")
askedForVersion := flags.Bool("v", false, "Print version")
askedForHelp := flags.BoolP("help", "h", false, "Print this help")
donationType := flags.String("donate", "", "Display QR code to donate to the project. possible values: btc, xmr, eth")
flags.SortFlags = false
flags.Usage = usage(os.Stderr, flags)
err := flags.Parse(os.Args[1:])
if err != nil {
flags.Usage()
os.Exit(1)
}
if flags.Lookup("donate").Changed {
switch *donationType {
case "xmr":
fmt.Println(donate.XMR.QRCode)
os.Exit(0)
case "eth":
fmt.Println(donate.ETH.QRCode)
os.Exit(0)
case "btc":
fmt.Println(donate.BTC.QRCode)
os.Exit(0)
default:
flags.Usage()
os.Exit(1)
}
}
if *askedForVersion {
fmt.Printf("vanity v%s\n", VERSION)
os.Exit(0)
}
if *askedForHelp {
usage(os.Stdout, flags)()
os.Exit(0)
}
// Ensure both ssl keys have the same state
if flags.Lookup("ssl-cert").Changed != flags.Lookup("ssl-key").Changed {
flags.Usage()
os.Exit(1)
}
if flags.Lookup("ssl-cert").Changed && flags.Lookup("ssl-key").Changed {
if len(opts.SSLCert) == 0 || len(opts.SSLKeyFile) == 0 {
flags.Usage()
os.Exit(1)
}
}
if len(opts.Dest) == 0 || opts.Port == 0 {
flags.Usage()
os.Exit(1)
}
return opts
}
// This is me being a pedant about help output going to Stdout but
// incorrect syntax going to Stderr
func usage(w io.Writer, f *flag.FlagSet) func() {
return func() {
fmt.Fprintf(w, versionFmt, VERSION)
fmt.Fprint(w, f.FlagUsages())
fmt.Fprintf(w, footer, donate.XMR, donate.BTC, donate.ETH)
}
}
const versionFmt = "vanity v%s - A tiny server for golang vanity redirects\n\nUsage: vanity -d <destination URI>\n\n"
const footer = `
Copyright 2021 Mark Wilkerson <mark@markhuge.com>
This is free software licensed under the GPLv3 <https://www.gnu.org/licenses/>
Source: https://git.markhuge.com/vanity
Bugs: bugs@markhuge.com
Patches: patches@markhuge.com
Donate to this project:
Monero (XMR): %s
Bitcoin (BTC): %s
Ethereum (ETH): %s
`