Skip to content

Commit

Permalink
Read TOXIPROXY_* environment variables if they exist as default value…
Browse files Browse the repository at this point in the history
…s for the hostname
  • Loading branch information
maaslalani committed Sep 16, 2021
1 parent 1eed55a commit 98bb2ab
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func main() {
&cli.StringFlag{
Name: "host",
Aliases: []string{"h"},
Value: "http://localhost:8474",
Value: defaultUrl(),
Usage: "toxiproxy host to connect to",
Destination: &hostname,
},
Expand Down Expand Up @@ -614,6 +614,26 @@ func listToxics(toxics toxiproxy.Toxics, stream string) {
}
}

const fallbackUrl = "http://localhost:8474"

func defaultUrl() string {
// For our purposes an unset variable is treated the same as a variable set
// to an empty string
url, _ := os.LookupEnv("TOXIPROXY_URL")
if url != "" {
return url
}

host, _ := os.LookupEnv("TOXIPROXY_HOST")
port, _ := os.LookupEnv("TOXIPROXY_PORT")

if host != "" && port != "" {
return fmt.Sprintf("http://%s:%s", strings.TrimPrefix(host, "http://"), port)
}

return fallbackUrl
}

func getArgOrFail(c *cli.Context, name string) (string, error) {
arg := c.String(name)
if arg == "" {
Expand Down

0 comments on commit 98bb2ab

Please sign in to comment.