From c2863c11b2b98330cd4833f0b05d7447040526b4 Mon Sep 17 00:00:00 2001 From: Michael Nikitochkin Date: Mon, 10 Jan 2022 10:45:54 +0100 Subject: [PATCH] Print the server version with command line (#353) Add flag `-version` to print the current version and exit. ```shell $ toxiproxy-server -version toxiproxy-server version v2.3.0 ``` --- bin/e2e | 2 +- bin/release_test | 17 +++++++----- cmd/server/server.go | 61 ++++++++++++++++++++++++++++++-------------- 3 files changed, 53 insertions(+), 27 deletions(-) diff --git a/bin/e2e b/bin/e2e index bd08b1c3..08cbb2bd 100755 --- a/bin/e2e +++ b/bin/e2e @@ -22,7 +22,7 @@ go run testing/endpoint.go 2>&1 | sed -e 's/^/[web] /' & echo "=== Starting Toxiproxy" -LOG_LEVEL=debug ./dist/toxiproxy-server 2>&1 | sed -e 's/^/[toxiproxy] /' & +LOG_LEVEL=debug ./dist/toxiproxy-server 2>&1 | sed -e 's/^/[toxiproxy] /' & echo "=== Wait when service are available" diff --git a/bin/release_test b/bin/release_test index a13b63a5..1a964c93 100755 --- a/bin/release_test +++ b/bin/release_test @@ -9,15 +9,18 @@ if [ $ARCH == "x86_64" ]; then ARCH="amd64" fi -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}'" -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}\"" -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}\"" +goreleaser release --rm-dist --skip-publish --skip-validate +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}'" +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}\"" +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}\"" -tar -ztvf dist/toxiproxy_*_linux_amd64.tar.gz | grep toxiproxy-server -tar -ztvf dist/toxiproxy_*_linux_amd64.tar.gz | grep toxiproxy-cli +tar -ztvf dist/toxiproxy_*_linux_amd64.tar.gz | grep -o -e toxiproxy-server +tar -ztvf dist/toxiproxy_*_linux_amd64.tar.gz | grep -o -e toxiproxy-cli goreleaser build --rm-dist --single-target --skip-validate --id server -./dist/toxiproxy-server-* --help 2>&1 | grep "Usage of ./dist/toxiproxy-server" +./dist/toxiproxy-server-* --help 2>&1 | grep -o -e "Usage of ./dist/toxiproxy-server" +./dist/toxiproxy-server-* --version | grep -o -e "toxiproxy-server version ${VERSION}" goreleaser build --rm-dist --single-target --skip-validate --id client -./dist/toxiproxy-cli-* --version | grep "toxiproxy-cli version ${VERSION}" +./dist/toxiproxy-cli-* --help 2>&1 | grep -o -e "toxiproxy-cli - Simulate network and system conditions" +./dist/toxiproxy-cli-* --version | grep -o -e "toxiproxy-cli version ${VERSION}" diff --git a/cmd/server/server.go b/cmd/server/server.go index 27b0c3f4..f65dc388 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -2,6 +2,7 @@ package main import ( "flag" + "fmt" "math/rand" "os" "os/signal" @@ -14,29 +15,32 @@ import ( "github.com/Shopify/toxiproxy/v2" ) -var ( - host string - port string - config string -) +type cliArguments struct { + host string + port string + config string + seed int64 + printVersion bool +} -func init() { - flag.StringVar(&host, "host", "localhost", "Host for toxiproxy's API to listen on") - flag.StringVar(&port, "port", "8474", "Port for toxiproxy's API to listen on") - flag.StringVar(&config, "config", "", "JSON file containing proxies to create on startup") - seed := flag.Int64("seed", time.Now().UTC().UnixNano(), "Seed for randomizing toxics with") +func parseArguments() cliArguments { + result := cliArguments{} + flag.StringVar(&result.host, "host", "localhost", + "Host for toxiproxy's API to listen on") + flag.StringVar(&result.port, "port", "8474", + "Port for toxiproxy's API to listen on") + flag.StringVar(&result.config, "config", "", + "JSON file containing proxies to create on startup") + flag.Int64Var(&result.seed, "seed", time.Now().UTC().UnixNano(), + "Seed for randomizing toxics with") + flag.BoolVar(&result.printVersion, "version", false, + `print the version (default "false")`) flag.Parse() - rand.Seed(*seed) + + return result } func main() { - setupLogger() - - server := toxiproxy.NewServer() - if len(config) > 0 { - server.PopulateConfig(config) - } - // Handle SIGTERM to exit cleanly signals := make(chan os.Signal, 1) signal.Notify(signals, syscall.SIGTERM) @@ -45,7 +49,26 @@ func main() { os.Exit(0) }() - server.Listen(host, port) + cli := parseArguments() + run(cli) +} + +func run(cli cliArguments) { + if cli.printVersion { + fmt.Printf("toxiproxy-server version %s\n", toxiproxy.Version) + return + } + + setupLogger() + + rand.Seed(cli.seed) + + server := toxiproxy.NewServer() + if len(cli.config) > 0 { + server.PopulateConfig(cli.config) + } + + server.Listen(cli.host, cli.port) } func setupLogger() {