Skip to content

Commit c2863c1

Browse files
authored
Print the server version with command line (Shopify#353)
Add flag `-version` to print the current version and exit. ```shell $ toxiproxy-server -version toxiproxy-server version v2.3.0 ```
1 parent 40f6327 commit c2863c1

File tree

3 files changed

+53
-27
lines changed

3 files changed

+53
-27
lines changed

bin/e2e

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ go run testing/endpoint.go 2>&1 | sed -e 's/^/[web] /' &
2222

2323
echo "=== Starting Toxiproxy"
2424

25-
LOG_LEVEL=debug ./dist/toxiproxy-server 2>&1 | sed -e 's/^/[toxiproxy] /' &
25+
LOG_LEVEL=debug ./dist/toxiproxy-server 2>&1 | sed -e 's/^/[toxiproxy] /' &
2626

2727
echo "=== Wait when service are available"
2828

bin/release_test

+10-7
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ if [ $ARCH == "x86_64" ]; then
99
ARCH="amd64"
1010
fi
1111

12-
docker run -v $(PWD)/dist:/dist --pull always --rm -it ubuntu bash -c "set -xe; dpkg -i /dist/toxiproxy_*_linux_${ARCH}.deb; ls -1 /usr/bin/toxiproxy-*; /usr/bin/toxiproxy-cli --version ; /usr/bin/toxiproxy-cli --version | grep -o -e 'toxiproxy-cli version ${VERSION}'"
13-
docker run -v $(PWD)/dist:/dist --pull always --rm -it centos bash -c "set -xe; yum install -y /dist/toxiproxy_*_linux_${ARCH}.rpm; ls -1 /usr/bin/toxiproxy-*; /usr/bin/toxiproxy-cli --version | grep \"toxiproxy-cli version ${VERSION}\""
14-
docker run -v $(PWD)/dist:/dist --pull always --rm -it alpine sh -c "set -xe; apk add --allow-untrusted --no-cache /dist/toxiproxy_*_linux_${ARCH}.apk; ls -1 /usr/bin/toxiproxy-*; /usr/bin/toxiproxy-cli --version | grep \"toxiproxy-cli version ${VERSION}\""
12+
goreleaser release --rm-dist --skip-publish --skip-validate
13+
docker run -v $(PWD)/dist:/dist --pull always --rm -it ubuntu bash -c "set -xe; dpkg -i /dist/toxiproxy_*_linux_${ARCH}.deb; ls -1 /usr/bin/toxiproxy-*; /usr/bin/toxiproxy-server --version ; /usr/bin/toxiproxy-cli --version ; /usr/bin/toxiproxy-cli --version | grep -o -e 'toxiproxy-cli version ${VERSION}'"
14+
docker run -v $(PWD)/dist:/dist --pull always --rm -it centos bash -c "set -xe; yum install -y /dist/toxiproxy_*_linux_${ARCH}.rpm; ls -1 /usr/bin/toxiproxy-*; /usr/bin/toxiproxy-server --version ; /usr/bin/toxiproxy-cli --version | grep -o -e \"toxiproxy-cli version ${VERSION}\""
15+
docker run -v $(PWD)/dist:/dist --pull always --rm -it alpine sh -c "set -xe; apk add --allow-untrusted --no-cache /dist/toxiproxy_*_linux_${ARCH}.apk; ls -1 /usr/bin/toxiproxy-*; /usr/bin/toxiproxy-server --version ; /usr/bin/toxiproxy-cli --version | grep -o -e \"toxiproxy-cli version ${VERSION}\""
1516

16-
tar -ztvf dist/toxiproxy_*_linux_amd64.tar.gz | grep toxiproxy-server
17-
tar -ztvf dist/toxiproxy_*_linux_amd64.tar.gz | grep toxiproxy-cli
17+
tar -ztvf dist/toxiproxy_*_linux_amd64.tar.gz | grep -o -e toxiproxy-server
18+
tar -ztvf dist/toxiproxy_*_linux_amd64.tar.gz | grep -o -e toxiproxy-cli
1819

1920
goreleaser build --rm-dist --single-target --skip-validate --id server
20-
./dist/toxiproxy-server-* --help 2>&1 | grep "Usage of ./dist/toxiproxy-server"
21+
./dist/toxiproxy-server-* --help 2>&1 | grep -o -e "Usage of ./dist/toxiproxy-server"
22+
./dist/toxiproxy-server-* --version | grep -o -e "toxiproxy-server version ${VERSION}"
2123

2224
goreleaser build --rm-dist --single-target --skip-validate --id client
23-
./dist/toxiproxy-cli-* --version | grep "toxiproxy-cli version ${VERSION}"
25+
./dist/toxiproxy-cli-* --help 2>&1 | grep -o -e "toxiproxy-cli - Simulate network and system conditions"
26+
./dist/toxiproxy-cli-* --version | grep -o -e "toxiproxy-cli version ${VERSION}"

cmd/server/server.go

+42-19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"flag"
5+
"fmt"
56
"math/rand"
67
"os"
78
"os/signal"
@@ -14,29 +15,32 @@ import (
1415
"github.com/Shopify/toxiproxy/v2"
1516
)
1617

17-
var (
18-
host string
19-
port string
20-
config string
21-
)
18+
type cliArguments struct {
19+
host string
20+
port string
21+
config string
22+
seed int64
23+
printVersion bool
24+
}
2225

23-
func init() {
24-
flag.StringVar(&host, "host", "localhost", "Host for toxiproxy's API to listen on")
25-
flag.StringVar(&port, "port", "8474", "Port for toxiproxy's API to listen on")
26-
flag.StringVar(&config, "config", "", "JSON file containing proxies to create on startup")
27-
seed := flag.Int64("seed", time.Now().UTC().UnixNano(), "Seed for randomizing toxics with")
26+
func parseArguments() cliArguments {
27+
result := cliArguments{}
28+
flag.StringVar(&result.host, "host", "localhost",
29+
"Host for toxiproxy's API to listen on")
30+
flag.StringVar(&result.port, "port", "8474",
31+
"Port for toxiproxy's API to listen on")
32+
flag.StringVar(&result.config, "config", "",
33+
"JSON file containing proxies to create on startup")
34+
flag.Int64Var(&result.seed, "seed", time.Now().UTC().UnixNano(),
35+
"Seed for randomizing toxics with")
36+
flag.BoolVar(&result.printVersion, "version", false,
37+
`print the version (default "false")`)
2838
flag.Parse()
29-
rand.Seed(*seed)
39+
40+
return result
3041
}
3142

3243
func main() {
33-
setupLogger()
34-
35-
server := toxiproxy.NewServer()
36-
if len(config) > 0 {
37-
server.PopulateConfig(config)
38-
}
39-
4044
// Handle SIGTERM to exit cleanly
4145
signals := make(chan os.Signal, 1)
4246
signal.Notify(signals, syscall.SIGTERM)
@@ -45,7 +49,26 @@ func main() {
4549
os.Exit(0)
4650
}()
4751

48-
server.Listen(host, port)
52+
cli := parseArguments()
53+
run(cli)
54+
}
55+
56+
func run(cli cliArguments) {
57+
if cli.printVersion {
58+
fmt.Printf("toxiproxy-server version %s\n", toxiproxy.Version)
59+
return
60+
}
61+
62+
setupLogger()
63+
64+
rand.Seed(cli.seed)
65+
66+
server := toxiproxy.NewServer()
67+
if len(cli.config) > 0 {
68+
server.PopulateConfig(cli.config)
69+
}
70+
71+
server.Listen(cli.host, cli.port)
4972
}
5073

5174
func setupLogger() {

0 commit comments

Comments
 (0)