From ba9dc5470e7337c949f0aa15005a73f74ffdd394 Mon Sep 17 00:00:00 2001 From: jhawk28 Date: Wed, 14 Aug 2019 14:46:32 -0400 Subject: [PATCH] add support for a host in ensurePortFormat (#1342) * add support for an host in ensurePortFormat using a host enables the ability to bind to a specific interface * add support for an host in ensurePortFormat using a host enables the ability to bind to a specific interface --- pkg/config/config.go | 8 +++----- pkg/config/config_test.go | 6 ++++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 2e7d9519..df45492f 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -6,12 +6,13 @@ import ( "os" "path/filepath" "runtime" + "strconv" "github.com/BurntSushi/toml" "github.com/gomods/athens/pkg/download/mode" "github.com/gomods/athens/pkg/errors" "github.com/kelseyhightower/envconfig" - validator "gopkg.in/go-playground/validator.v9" + "gopkg.in/go-playground/validator.v9" ) const defaultConfigFile = "athens.toml" @@ -188,10 +189,7 @@ func envOverride(config *Config) error { } func ensurePortFormat(s string) string { - if len(s) == 0 { - return "" - } - if s[0] != ':' { + if _, err := strconv.Atoi(s); err == nil { return ":" + s } return s diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index f0fbd508..22692456 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -143,6 +143,12 @@ func TestEnsurePortFormat(t *testing.T) { if given != expected { t.Fatalf("expected ensurePortFormat to not add a colon when it's present but got %v", given) } + port = "127.0.0.1:3000" + expected = "127.0.0.1:3000" + given = ensurePortFormat(port) + if given != expected { + t.Fatalf("expected ensurePortFormat to not add a colon when it's present but got %v", given) + } } func TestStorageEnvOverrides(t *testing.T) {