Skip to content

Commit 2662eb8

Browse files
committed
📮 cmd: add version flag
1 parent cb963b3 commit 2662eb8

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

cmd/shadowsocks-go-domain-set-converter/main.go

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,67 @@ import (
77
"flag"
88
"fmt"
99
"os"
10+
"runtime/debug"
1011
"strings"
1112

13+
"github.com/database64128/shadowsocks-go"
1214
"github.com/database64128/shadowsocks-go/bytestrings"
1315
"github.com/database64128/shadowsocks-go/domainset"
1416
"github.com/database64128/shadowsocks-go/mmap"
1517
)
1618

1719
var (
18-
inDlc = flag.String("inDlc", "", "Path to input domain set file in v2fly/dlc format.")
19-
inText = flag.String("inText", "", "Path to input domain set file in plaintext format.")
20-
inGob = flag.String("inGob", "", "Path to input domain set file in gob format.")
21-
outText = flag.String("outText", "", "Path to output domain set file in plaintext format.")
22-
outGob = flag.String("outGob", "", "Path to output domain set file in gob format.")
23-
tag = flag.String("tag", "", "Select lines with the specified tag. If empty, select all lines. Only applicable to v2fly/dlc format.")
20+
version bool
21+
inDlc string
22+
inText string
23+
inGob string
24+
outText string
25+
outGob string
26+
tag string
2427
)
2528

29+
func init() {
30+
flag.BoolVar(&version, "version", false, "Print the version and exit")
31+
flag.StringVar(&inDlc, "inDlc", "", "Path to input domain set file in v2fly/dlc format")
32+
flag.StringVar(&inText, "inText", "", "Path to input domain set file in plaintext format")
33+
flag.StringVar(&inGob, "inGob", "", "Path to input domain set file in gob format")
34+
flag.StringVar(&outText, "outText", "", "Path to output domain set file in plaintext format")
35+
flag.StringVar(&outGob, "outGob", "", "Path to output domain set file in gob format")
36+
flag.StringVar(&tag, "tag", "", "Select lines with the specified tag. If empty, select all lines. Only applicable to v2fly/dlc format.")
37+
}
38+
2639
func main() {
2740
flag.Parse()
2841

42+
if version {
43+
os.Stdout.WriteString("shadowsocks-go-domain-set-converter " + shadowsocks.Version + "\n")
44+
if info, ok := debug.ReadBuildInfo(); ok {
45+
os.Stdout.WriteString(info.String())
46+
}
47+
return
48+
}
49+
2950
var (
3051
inCount int
3152
inPath string
3253
inFunc func(string) (domainset.Builder, error)
3354
)
3455

35-
if *inDlc != "" {
56+
if inDlc != "" {
3657
inCount++
37-
inPath = *inDlc
58+
inPath = inDlc
3859
inFunc = DomainSetBuilderFromDlc
3960
}
4061

41-
if *inText != "" {
62+
if inText != "" {
4263
inCount++
43-
inPath = *inText
64+
inPath = inText
4465
inFunc = domainset.BuilderFromText
4566
}
4667

47-
if *inGob != "" {
68+
if inGob != "" {
4869
inCount++
49-
inPath = *inGob
70+
inPath = inGob
5071
inFunc = domainset.BuilderFromGobString
5172
}
5273

@@ -56,7 +77,7 @@ func main() {
5677
os.Exit(1)
5778
}
5879

59-
if *outText == "" && *outGob == "" {
80+
if outText == "" && outGob == "" {
6081
fmt.Fprintln(os.Stderr, "Specify output file paths with -outText and/or -outGob.")
6182
flag.Usage()
6283
os.Exit(1)
@@ -75,8 +96,8 @@ func main() {
7596
return
7697
}
7798

78-
if *outText != "" {
79-
fout, err := os.Create(*outText)
99+
if outText != "" {
100+
fout, err := os.Create(outText)
80101
if err != nil {
81102
fmt.Fprintln(os.Stderr, "Failed to create output file:", err)
82103
return
@@ -90,8 +111,8 @@ func main() {
90111
}
91112
}
92113

93-
if *outGob != "" {
94-
fout, err := os.Create(*outGob)
114+
if outGob != "" {
115+
fout, err := os.Create(outGob)
95116
if err != nil {
96117
fmt.Fprintln(os.Stderr, "Failed to create output file:", err)
97118
return
@@ -135,14 +156,14 @@ func DomainSetBuilderFromDlc(text string) (domainset.Builder, error) {
135156
return dsb, fmt.Errorf("invalid line: %q", line)
136157
}
137158

138-
if *tag == "" { // select all lines
159+
if tag == "" { // select all lines
139160
if end == -1 {
140161
end = len(line)
141162
} else {
142163
end--
143164
}
144165
} else { // select matched tag
145-
if end == -1 || line[end+1:] != *tag { // no tag or different tag
166+
if end == -1 || line[end+1:] != tag { // no tag or different tag
146167
continue
147168
} else {
148169
end--

cmd/shadowsocks-go/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"fmt"
77
"os"
88
"os/signal"
9+
"runtime/debug"
910
"syscall"
1011

12+
"github.com/database64128/shadowsocks-go"
1113
"github.com/database64128/shadowsocks-go/jsonhelper"
1214
"github.com/database64128/shadowsocks-go/logging"
1315
"github.com/database64128/shadowsocks-go/service"
@@ -16,13 +18,15 @@ import (
1618
)
1719

1820
var (
21+
version bool
1922
testConf bool
2023
confPath string
2124
zapConf string
2225
logLevel zapcore.Level
2326
)
2427

2528
func init() {
29+
flag.BoolVar(&version, "version", false, "Print the version and exit")
2630
flag.BoolVar(&testConf, "testConf", false, "Test the configuration file and exit without starting the services")
2731
flag.StringVar(&confPath, "confPath", "config.json", "Path to the JSON configuration file")
2832
flag.StringVar(&zapConf, "zapConf", "console", "Preset name or path to the JSON configuration file for building the zap logger.\nAvailable presets: console, console-nocolor, console-notime, systemd, production, development")
@@ -32,6 +36,14 @@ func init() {
3236
func main() {
3337
flag.Parse()
3438

39+
if version {
40+
os.Stdout.WriteString("shadowsocks-go " + shadowsocks.Version + "\n")
41+
if info, ok := debug.ReadBuildInfo(); ok {
42+
os.Stdout.WriteString(info.String())
43+
}
44+
return
45+
}
46+
3547
logger, err := logging.NewZapLogger(zapConf, logLevel)
3648
if err != nil {
3749
fmt.Fprintln(os.Stderr, "Failed to build logger:", err)

shadowsocks.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
// Package shadowsocks implements the Shadowsocks protocol edition 2022 and later.
22
package shadowsocks
3+
4+
// Version is the current version of shadowsocks-go.
5+
const Version = "1.12.0"

0 commit comments

Comments
 (0)