Skip to content

Commit

Permalink
Print the server version with command line (#353)
Browse files Browse the repository at this point in the history
Add flag `-version` to print the current version and exit.

```shell
$ toxiproxy-server -version
toxiproxy-server version v2.3.0
```
  • Loading branch information
miry authored Jan 10, 2022
1 parent 40f6327 commit c2863c1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 27 deletions.
2 changes: 1 addition & 1 deletion bin/e2e
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
17 changes: 10 additions & 7 deletions bin/release_test
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
61 changes: 42 additions & 19 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"fmt"
"math/rand"
"os"
"os/signal"
Expand All @@ -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)
Expand All @@ -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() {
Expand Down

0 comments on commit c2863c1

Please sign in to comment.