From 1e0251d7ac073b3d415bb541202d2dbcbb63fd90 Mon Sep 17 00:00:00 2001 From: Eitol Date: Sat, 6 Nov 2021 21:19:53 -0300 Subject: [PATCH 1/2] All configuration related code has been moved to a separate package so that it can be imported into external projects --- cli.go | 19 +- cli_test.go | 229 +++++++++--------- config.go => config/config.go | 2 +- config_prefix.go => config/config_prefix.go | 2 +- config_service.go => config/config_service.go | 2 +- config_test.go => config/config_test.go | 2 +- runner.go | 23 +- runner_test.go | 87 +++---- 8 files changed, 185 insertions(+), 181 deletions(-) rename config.go => config/config.go (99%) rename config_prefix.go => config/config_prefix.go (99%) rename config_service.go => config/config_service.go (99%) rename config_test.go => config/config_test.go (99%) diff --git a/cli.go b/cli.go index 17725906..5fc34690 100644 --- a/cli.go +++ b/cli.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + cfg "github.com/hashicorp/envconsul/config" "io" "io/ioutil" "log" @@ -207,10 +208,10 @@ func (cli *CLI) stop() { // Flag library. This is extracted into a helper to keep the main function // small, but it also makes writing tests for parsing command line arguments // much easier and cleaner. -func (cli *CLI) ParseFlags(args []string) (*Config, []string, bool, bool, error) { +func (cli *CLI) ParseFlags(args []string) (*cfg.Config, []string, bool, bool, error) { var once, isVersion bool var no_prefix *bool - var c = DefaultConfig() + var c = cfg.DefaultConfig() // configPaths stores the list of configuration paths on disk configPaths := make([]string, 0, 6) @@ -382,7 +383,7 @@ func (cli *CLI) ParseFlags(args []string) (*Config, []string, bool, bool, error) }), "pid-file", "") flags.Var((funcVar)(func(s string) error { - p, err := ParsePrefixConfig(s) + p, err := cfg.ParsePrefixConfig(s) if err != nil { return err } @@ -410,7 +411,7 @@ func (cli *CLI) ParseFlags(args []string) (*Config, []string, bool, bool, error) }), "sanitize", "") flags.Var((funcVar)(func(s string) error { - p, err := ParsePrefixConfig(s) + p, err := cfg.ParsePrefixConfig(s) if err != nil { return err } @@ -419,7 +420,7 @@ func (cli *CLI) ParseFlags(args []string) (*Config, []string, bool, bool, error) }), "secret", "") flags.Var((funcVar)(func(s string) error { - p, err := ParseServiceConfig(s) + p, err := cfg.ParseServiceConfig(s) if err != nil { return err } @@ -705,11 +706,11 @@ func (cli *CLI) ParseFlags(args []string) (*Config, []string, bool, bool, error) // configuration is the list of overrides to apply at the very end, taking // precendence over any configurations that were loaded from the paths. If any // errors occur when reading or parsing those sub-configs, it is returned. -func loadConfigs(paths []string, o *Config) (*Config, error) { - finalC := DefaultConfig() +func loadConfigs(paths []string, o *cfg.Config) (*cfg.Config, error) { + finalC := cfg.DefaultConfig() for _, path := range paths { - c, err := FromPath(path) + c, err := cfg.FromPath(path) if err != nil { return nil, err } @@ -728,7 +729,7 @@ func logError(err error, status int) int { return status } -func (cli *CLI) setup(conf *Config) (*Config, error) { +func (cli *CLI) setup(conf *cfg.Config) (*cfg.Config, error) { if err := logging.Setup(&logging.Config{ SyslogName: version.Name, Level: config.StringVal(conf.LogLevel), diff --git a/cli_test.go b/cli_test.go index 08012588..953b5701 100644 --- a/cli_test.go +++ b/cli_test.go @@ -2,6 +2,7 @@ package main import ( "fmt" + cfg "github.com/hashicorp/envconsul/config" "io/ioutil" "os" "reflect" @@ -25,7 +26,7 @@ func TestCLI_ParseFlags(t *testing.T) { cases := []struct { name string f []string - e *Config + e *cfg.Config err bool }{ // Deprecations @@ -33,7 +34,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "auth", []string{"-auth", "abcd:efgh"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ Auth: &config.AuthConfig{ Username: config.String("abcd"), @@ -46,7 +47,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul", []string{"-consul", "127.0.0.1:8500"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ Address: config.String("127.0.0.1:8500"), }, @@ -56,7 +57,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "retry", []string{"-retry", "10s"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ Retry: &config.RetryConfig{ Backoff: config.TimeDuration(10 * time.Second), @@ -75,7 +76,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "splay", []string{"-splay", "10s"}, - &Config{ + &cfg.Config{ Exec: &config.ExecConfig{ Splay: config.TimeDuration(10 * time.Second), }, @@ -85,7 +86,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "ssl", []string{"-ssl"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ SSL: &config.SSLConfig{ Enabled: config.Bool(true), @@ -102,7 +103,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "ssl_verify", []string{"-ssl-verify"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ SSL: &config.SSLConfig{ Verify: config.Bool(true), @@ -119,7 +120,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "ssl_ca-cert", []string{"-ssl-ca-cert", "foo"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ SSL: &config.SSLConfig{ CaCert: config.String("foo"), @@ -136,7 +137,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "ssl_cert", []string{"-ssl-cert", "foo"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ SSL: &config.SSLConfig{ Cert: config.String("foo"), @@ -153,7 +154,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "timeout", []string{"-timeout", "10s"}, - &Config{ + &cfg.Config{ Exec: &config.ExecConfig{ Timeout: config.TimeDuration(10 * time.Second), }, @@ -163,7 +164,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "token", []string{"-token", "abcd1234"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ Token: config.String("abcd1234"), }, @@ -176,7 +177,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "config", []string{"-config", f.Name()}, - &Config{}, + &cfg.Config{}, false, }, { @@ -185,13 +186,13 @@ func TestCLI_ParseFlags(t *testing.T) { "-config", f.Name(), "-config", f.Name(), }, - &Config{}, + &cfg.Config{}, false, }, { "consul_addr", []string{"-consul-addr", "1.2.3.4"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ Address: config.String("1.2.3.4"), }, @@ -207,7 +208,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul_auth_username", []string{"-consul-auth", "username"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ Auth: &config.AuthConfig{ Username: config.String("username"), @@ -219,7 +220,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul_auth_username_password", []string{"-consul-auth", "username:password"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ Auth: &config.AuthConfig{ Username: config.String("username"), @@ -232,7 +233,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul-retry", []string{"-consul-retry"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ Retry: &config.RetryConfig{ Enabled: config.Bool(true), @@ -244,7 +245,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul-retry-attempts", []string{"-consul-retry-attempts", "20"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ Retry: &config.RetryConfig{ Attempts: config.Int(20), @@ -256,7 +257,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul-retry-backoff", []string{"-consul-retry-backoff", "30s"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ Retry: &config.RetryConfig{ Backoff: config.TimeDuration(30 * time.Second), @@ -268,7 +269,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul-retry-max-backoff", []string{"-consul-retry-max-backoff", "60s"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ Retry: &config.RetryConfig{ MaxBackoff: config.TimeDuration(60 * time.Second), @@ -280,7 +281,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul-ssl", []string{"-consul-ssl"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ SSL: &config.SSLConfig{ Enabled: config.Bool(true), @@ -292,7 +293,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul-ssl-ca-cert", []string{"-consul-ssl-ca-cert", "ca_cert"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ SSL: &config.SSLConfig{ CaCert: config.String("ca_cert"), @@ -304,7 +305,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul-ssl-ca-path", []string{"-consul-ssl-ca-path", "ca_path"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ SSL: &config.SSLConfig{ CaPath: config.String("ca_path"), @@ -316,7 +317,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul-ssl-cert", []string{"-consul-ssl-cert", "cert"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ SSL: &config.SSLConfig{ Cert: config.String("cert"), @@ -328,7 +329,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul-ssl-key", []string{"-consul-ssl-key", "key"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ SSL: &config.SSLConfig{ Key: config.String("key"), @@ -340,7 +341,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul-ssl-server-name", []string{"-consul-ssl-server-name", "server_name"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ SSL: &config.SSLConfig{ ServerName: config.String("server_name"), @@ -352,7 +353,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul-ssl-verify", []string{"-consul-ssl-verify"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ SSL: &config.SSLConfig{ Verify: config.Bool(true), @@ -364,7 +365,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul-token", []string{"-consul-token", "token"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ Token: config.String("token"), }, @@ -374,7 +375,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul-transport-dial-keep-alive", []string{"-consul-transport-dial-keep-alive", "30s"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ Transport: &config.TransportConfig{ DialKeepAlive: config.TimeDuration(30 * time.Second), @@ -386,7 +387,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul-transport-dial-timeout", []string{"-consul-transport-dial-timeout", "30s"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ Transport: &config.TransportConfig{ DialTimeout: config.TimeDuration(30 * time.Second), @@ -398,7 +399,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul-transport-disable-keep-alives", []string{"-consul-transport-disable-keep-alives"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ Transport: &config.TransportConfig{ DisableKeepAlives: config.Bool(true), @@ -410,7 +411,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul-transport-max-idle-conns-per-host", []string{"-consul-transport-max-idle-conns-per-host", "100"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ Transport: &config.TransportConfig{ MaxIdleConnsPerHost: config.Int(100), @@ -422,7 +423,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "consul-transport-tls-handshake-timeout", []string{"-consul-transport-tls-handshake-timeout", "30s"}, - &Config{ + &cfg.Config{ Consul: &config.ConsulConfig{ Transport: &config.TransportConfig{ TLSHandshakeTimeout: config.TimeDuration(30 * time.Second), @@ -434,7 +435,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "exec", []string{"-exec", "command"}, - &Config{ + &cfg.Config{ Exec: &config.ExecConfig{ Enabled: config.Bool(true), Command: config.String("command"), @@ -445,7 +446,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "exec-kill-signal", []string{"-exec-kill-signal", "SIGUSR1"}, - &Config{ + &cfg.Config{ Exec: &config.ExecConfig{ KillSignal: config.Signal(syscall.SIGUSR1), }, @@ -455,7 +456,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "exec-kill-timeout", []string{"-exec-kill-timeout", "10s"}, - &Config{ + &cfg.Config{ Exec: &config.ExecConfig{ KillTimeout: config.TimeDuration(10 * time.Second), }, @@ -465,7 +466,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "exec-splay", []string{"-exec-splay", "10s"}, - &Config{ + &cfg.Config{ Exec: &config.ExecConfig{ Splay: config.TimeDuration(10 * time.Second), }, @@ -475,7 +476,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "kill-signal", []string{"-kill-signal", "SIGUSR1"}, - &Config{ + &cfg.Config{ KillSignal: config.Signal(syscall.SIGUSR1), }, false, @@ -483,7 +484,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "log-level", []string{"-log-level", "DEBUG"}, - &Config{ + &cfg.Config{ LogLevel: config.String("DEBUG"), }, false, @@ -491,7 +492,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "max-stale", []string{"-max-stale", "10s"}, - &Config{ + &cfg.Config{ MaxStale: config.TimeDuration(10 * time.Second), }, false, @@ -499,7 +500,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "pid-file", []string{"-pid-file", "/var/pid/file"}, - &Config{ + &cfg.Config{ PidFile: config.String("/var/pid/file"), }, false, @@ -507,9 +508,9 @@ func TestCLI_ParseFlags(t *testing.T) { { "prefix", []string{"-prefix", "foo/bar"}, - &Config{ - Prefixes: &PrefixConfigs{ - &PrefixConfig{ + &cfg.Config{ + Prefixes: &cfg.PrefixConfigs{ + &cfg.PrefixConfig{ Path: config.String("foo/bar"), }, }, @@ -522,12 +523,12 @@ func TestCLI_ParseFlags(t *testing.T) { "-prefix", "foo/bar", "-prefix", "zip/zap", }, - &Config{ - Prefixes: &PrefixConfigs{ - &PrefixConfig{ + &cfg.Config{ + Prefixes: &cfg.PrefixConfigs{ + &cfg.PrefixConfig{ Path: config.String("foo/bar"), }, - &PrefixConfig{ + &cfg.PrefixConfig{ Path: config.String("zip/zap"), }, }, @@ -537,9 +538,9 @@ func TestCLI_ParseFlags(t *testing.T) { { "no-prefix", []string{"-prefix", "foo/bar", "-no-prefix"}, - &Config{ - Prefixes: &PrefixConfigs{ - &PrefixConfig{ + &cfg.Config{ + Prefixes: &cfg.PrefixConfigs{ + &cfg.PrefixConfig{ Path: config.String("foo/bar"), NoPrefix: config.Bool(true), }, @@ -550,9 +551,9 @@ func TestCLI_ParseFlags(t *testing.T) { { "no-prefix", []string{"-secret", "foo/bar", "-no-prefix"}, - &Config{ - Secrets: &PrefixConfigs{ - &PrefixConfig{ + &cfg.Config{ + Secrets: &cfg.PrefixConfigs{ + &cfg.PrefixConfig{ Path: config.String("foo/bar"), NoPrefix: config.Bool(true), }, @@ -563,9 +564,9 @@ func TestCLI_ParseFlags(t *testing.T) { { "no-prefix-false", []string{"-prefix", "foo/bar", "-no-prefix=false"}, - &Config{ - Prefixes: &PrefixConfigs{ - &PrefixConfig{ + &cfg.Config{ + Prefixes: &cfg.PrefixConfigs{ + &cfg.PrefixConfig{ Path: config.String("foo/bar"), NoPrefix: config.Bool(false), }, @@ -576,9 +577,9 @@ func TestCLI_ParseFlags(t *testing.T) { { "no-prefix-nil-default", []string{"-prefix", "foo/bar"}, - &Config{ - Prefixes: &PrefixConfigs{ - &PrefixConfig{ + &cfg.Config{ + Prefixes: &cfg.PrefixConfigs{ + &cfg.PrefixConfig{ Path: config.String("foo/bar"), NoPrefix: nil, }, @@ -589,7 +590,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "pristine", []string{"-pristine"}, - &Config{ + &cfg.Config{ Pristine: config.Bool(true), }, false, @@ -597,7 +598,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "reload-signal", []string{"-reload-signal", "SIGUSR1"}, - &Config{ + &cfg.Config{ ReloadSignal: config.Signal(syscall.SIGUSR1), }, false, @@ -605,7 +606,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "sanitize", []string{"-sanitize"}, - &Config{ + &cfg.Config{ Sanitize: config.Bool(true), }, false, @@ -613,9 +614,9 @@ func TestCLI_ParseFlags(t *testing.T) { { "secret", []string{"-secret", "foo/bar"}, - &Config{ - Secrets: &PrefixConfigs{ - &PrefixConfig{ + &cfg.Config{ + Secrets: &cfg.PrefixConfigs{ + &cfg.PrefixConfig{ Path: config.String("foo/bar"), }, }, @@ -628,12 +629,12 @@ func TestCLI_ParseFlags(t *testing.T) { "-secret", "foo/bar", "-secret", "zip/zap", }, - &Config{ - Secrets: &PrefixConfigs{ - &PrefixConfig{ + &cfg.Config{ + Secrets: &cfg.PrefixConfigs{ + &cfg.PrefixConfig{ Path: config.String("foo/bar"), }, - &PrefixConfig{ + &cfg.PrefixConfig{ Path: config.String("zip/zap"), }, }, @@ -645,9 +646,9 @@ func TestCLI_ParseFlags(t *testing.T) { []string{ "-service-query", "service", }, - &Config{ - Services: &ServiceConfigs{ - &ServiceConfig{ + &cfg.Config{ + Services: &cfg.ServiceConfigs{ + &cfg.ServiceConfig{ Query: config.String("service"), }, }, @@ -661,15 +662,15 @@ func TestCLI_ParseFlags(t *testing.T) { "-service-query", "tag.service", "-service-query", "tag.service@datacenter", }, - &Config{ - Services: &ServiceConfigs{ - &ServiceConfig{ + &cfg.Config{ + Services: &cfg.ServiceConfigs{ + &cfg.ServiceConfig{ Query: config.String("service"), }, - &ServiceConfig{ + &cfg.ServiceConfig{ Query: config.String("tag.service"), }, - &ServiceConfig{ + &cfg.ServiceConfig{ Query: config.String("tag.service@datacenter"), }, }, @@ -686,9 +687,9 @@ func TestCLI_ParseFlags(t *testing.T) { "-service-format-tag", "tag", "-service-format-port", "port", }, - &Config{ - Services: &ServiceConfigs{ - &ServiceConfig{ + &cfg.Config{ + Services: &cfg.ServiceConfigs{ + &cfg.ServiceConfig{ Query: config.String("service"), FormatId: config.String("id"), FormatName: config.String("name"), @@ -716,9 +717,9 @@ func TestCLI_ParseFlags(t *testing.T) { "-service-format-tag", "bar/tag", "-service-format-port", "bar/port", }, - &Config{ - Services: &ServiceConfigs{ - &ServiceConfig{ + &cfg.Config{ + Services: &cfg.ServiceConfigs{ + &cfg.ServiceConfig{ Query: config.String("foo"), FormatId: config.String("foo/id"), FormatName: config.String("foo/name"), @@ -726,7 +727,7 @@ func TestCLI_ParseFlags(t *testing.T) { FormatTag: config.String("foo/tag"), FormatPort: config.String("foo/port"), }, - &ServiceConfig{ + &cfg.ServiceConfig{ Query: config.String("bar"), FormatId: config.String("bar/id"), FormatName: config.String("bar/name"), @@ -741,7 +742,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "syslog", []string{"-syslog"}, - &Config{ + &cfg.Config{ Syslog: &config.SyslogConfig{ Enabled: config.Bool(true), }, @@ -751,7 +752,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "syslog-facility", []string{"-syslog-facility", "LOCAL0"}, - &Config{ + &cfg.Config{ Syslog: &config.SyslogConfig{ Facility: config.String("LOCAL0"), }, @@ -761,7 +762,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "upcase", []string{"-upcase"}, - &Config{ + &cfg.Config{ Upcase: config.Bool(true), }, false, @@ -769,7 +770,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-addr", []string{"-vault-addr", "vault_addr"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ Address: config.String("vault_addr"), }, @@ -779,7 +780,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-namespace", []string{"-vault-namespace", "vault_namespace"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ Namespace: config.String("vault_namespace"), }, @@ -789,7 +790,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-retry", []string{"-vault-retry"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ Retry: &config.RetryConfig{ Enabled: config.Bool(true), @@ -801,7 +802,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-retry-attempts", []string{"-vault-retry-attempts", "20"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ Retry: &config.RetryConfig{ Attempts: config.Int(20), @@ -813,7 +814,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-retry-backoff", []string{"-vault-retry-backoff", "30s"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ Retry: &config.RetryConfig{ Backoff: config.TimeDuration(30 * time.Second), @@ -825,7 +826,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-retry-max-backoff", []string{"-vault-retry-max-backoff", "60s"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ Retry: &config.RetryConfig{ MaxBackoff: config.TimeDuration(60 * time.Second), @@ -837,7 +838,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-renew-token", []string{"-vault-renew-token"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ RenewToken: config.Bool(true), }, @@ -847,7 +848,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-ssl", []string{"-vault-ssl"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ SSL: &config.SSLConfig{ Enabled: config.Bool(true), @@ -859,7 +860,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-ssl-ca-cert", []string{"-vault-ssl-ca-cert", "ca_cert"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ SSL: &config.SSLConfig{ CaCert: config.String("ca_cert"), @@ -871,7 +872,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-ssl-ca-path", []string{"-vault-ssl-ca-path", "ca_path"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ SSL: &config.SSLConfig{ CaPath: config.String("ca_path"), @@ -883,7 +884,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-ssl-cert", []string{"-vault-ssl-cert", "cert"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ SSL: &config.SSLConfig{ Cert: config.String("cert"), @@ -895,7 +896,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-ssl-key", []string{"-vault-ssl-key", "key"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ SSL: &config.SSLConfig{ Key: config.String("key"), @@ -907,7 +908,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-ssl-server-name", []string{"-vault-ssl-server-name", "server_name"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ SSL: &config.SSLConfig{ ServerName: config.String("server_name"), @@ -919,7 +920,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-ssl-verify", []string{"-vault-ssl-verify"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ SSL: &config.SSLConfig{ Verify: config.Bool(true), @@ -931,7 +932,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-token", []string{"-vault-token", "token"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ Token: config.String("token"), }, @@ -941,7 +942,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-transport-dial-keep-alive", []string{"-vault-transport-dial-keep-alive", "30s"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ Transport: &config.TransportConfig{ DialKeepAlive: config.TimeDuration(30 * time.Second), @@ -953,7 +954,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-transport-dial-timeout", []string{"-vault-transport-dial-timeout", "30s"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ Transport: &config.TransportConfig{ DialTimeout: config.TimeDuration(30 * time.Second), @@ -965,7 +966,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-transport-disable-keep-alives", []string{"-vault-transport-disable-keep-alives"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ Transport: &config.TransportConfig{ DisableKeepAlives: config.Bool(true), @@ -977,7 +978,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-transport-max-idle-conns-per-host", []string{"-vault-transport-max-idle-conns-per-host", "100"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ Transport: &config.TransportConfig{ MaxIdleConnsPerHost: config.Int(100), @@ -989,7 +990,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-transport-tls-handshake-timeout", []string{"-vault-transport-tls-handshake-timeout", "30s"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ Transport: &config.TransportConfig{ TLSHandshakeTimeout: config.TimeDuration(30 * time.Second), @@ -1001,7 +1002,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "vault-unwrap-token", []string{"-vault-unwrap-token"}, - &Config{ + &cfg.Config{ Vault: &config.VaultConfig{ UnwrapToken: config.Bool(true), }, @@ -1011,7 +1012,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "wait_min", []string{"-wait", "10s"}, - &Config{ + &cfg.Config{ Wait: &config.WaitConfig{ Min: config.TimeDuration(10 * time.Second), Max: config.TimeDuration(40 * time.Second), @@ -1022,7 +1023,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "wait_min_max", []string{"-wait", "10s:30s"}, - &Config{ + &cfg.Config{ Wait: &config.WaitConfig{ Min: config.TimeDuration(10 * time.Second), Max: config.TimeDuration(30 * time.Second), @@ -1035,7 +1036,7 @@ func TestCLI_ParseFlags(t *testing.T) { { "command", []string{"my", "command", "to", "run"}, - &Config{ + &cfg.Config{ Exec: &config.ExecConfig{ Enabled: config.Bool(true), Command: config.String("my command to run"), @@ -1049,7 +1050,7 @@ func TestCLI_ParseFlags(t *testing.T) { "-exec", "command 1", "command", "2", }, - &Config{ + &cfg.Config{ Exec: &config.ExecConfig{ Enabled: config.Bool(true), Command: config.String("command 1"), @@ -1070,7 +1071,7 @@ func TestCLI_ParseFlags(t *testing.T) { } if tc.e != nil { - tc.e = DefaultConfig().Merge(tc.e) + tc.e = cfg.DefaultConfig().Merge(tc.e) } if !reflect.DeepEqual(tc.e, a) { diff --git a/config.go b/config/config.go similarity index 99% rename from config.go rename to config/config.go index 827856c2..f5effb4a 100644 --- a/config.go +++ b/config/config.go @@ -1,4 +1,4 @@ -package main +package config import ( "fmt" diff --git a/config_prefix.go b/config/config_prefix.go similarity index 99% rename from config_prefix.go rename to config/config_prefix.go index 5c0e18cc..8a7944b6 100644 --- a/config_prefix.go +++ b/config/config_prefix.go @@ -1,4 +1,4 @@ -package main +package config import ( "fmt" diff --git a/config_service.go b/config/config_service.go similarity index 99% rename from config_service.go rename to config/config_service.go index 5fc05751..e7afbff0 100644 --- a/config_service.go +++ b/config/config_service.go @@ -1,4 +1,4 @@ -package main +package config import ( "fmt" diff --git a/config_test.go b/config/config_test.go similarity index 99% rename from config_test.go rename to config/config_test.go index 3311927f..ee7954d3 100644 --- a/config_test.go +++ b/config/config_test.go @@ -1,4 +1,4 @@ -package main +package config import ( "fmt" diff --git a/runner.go b/runner.go index 1b6a8d43..4b5cfb0e 100644 --- a/runner.go +++ b/runner.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + config2 "github.com/hashicorp/envconsul/config" "html/template" "io" "log" @@ -46,13 +47,13 @@ type Runner struct { // config is the Config that created this Runner. It is used internally to // construct other objects and pass data. - config *Config + config *config2.Config // configPrefixMap is a map of a dependency's hashcode back to the config // prefix that created it. - configPrefixMap map[string]*PrefixConfig + configPrefixMap map[string]*config2.PrefixConfig - configServiceMap map[string]*ServiceConfig + configServiceMap map[string]*config2.ServiceConfig // data is the latest representation of the data from Consul. data map[string]interface{} @@ -90,7 +91,7 @@ type Runner struct { } // NewRunner accepts a config, command, and boolean value for once mode. -func NewRunner(config *Config, once bool) (*Runner, error) { +func NewRunner(config *config2.Config, once bool) (*Runner, error) { log.Printf("[INFO] (runner) creating new runner (once: %v)", once) runner := &Runner{ @@ -618,12 +619,12 @@ func (r *Runner) appendSecrets( } var applyPerKeyFormat bool - var keyFormats map[string]*KeyFormat + var keyFormats map[string]*config2.KeyFormat // pre-populate key formats map here so we don't have a potential O(n^2) complexity in the loop later if cp.Keys != nil && !config.StringPresent(cp.Format) { applyPerKeyFormat = true - keyFormats = make(map[string]*KeyFormat) + keyFormats = make(map[string]*config2.KeyFormat) for _, v := range *cp.Keys { keyFormats[*v.Name] = v } @@ -712,7 +713,7 @@ func (r *Runner) appendSecrets( // any problems occur. func (r *Runner) init() error { // Ensure default configuration values - r.config = DefaultConfig().Merge(r.config) + r.config = config2.DefaultConfig().Merge(r.config) r.config.Finalize() // Print the final config for debugging @@ -736,8 +737,8 @@ func (r *Runner) init() error { r.watcher = watcher r.data = make(map[string]interface{}) - r.configPrefixMap = make(map[string]*PrefixConfig) - r.configServiceMap = make(map[string]*ServiceConfig) + r.configPrefixMap = make(map[string]*config2.PrefixConfig) + r.configServiceMap = make(map[string]*config2.ServiceConfig) r.inStream = os.Stdin r.outStream = os.Stdout @@ -859,7 +860,7 @@ func (r *Runner) deletePid() error { } // newClientSet creates a new client set from the given config. -func newClientSet(c *Config) (*dep.ClientSet, error) { +func newClientSet(c *config2.Config) (*dep.ClientSet, error) { clients := dep.NewClientSet() if err := clients.CreateConsulClient(&dep.CreateConsulClientInput{ @@ -913,7 +914,7 @@ func newClientSet(c *Config) (*dep.ClientSet, error) { } // newWatcher creates a new watcher. -func newWatcher(c *Config, clients *dep.ClientSet, once bool) (*watch.Watcher, error) { +func newWatcher(c *config2.Config, clients *dep.ClientSet, once bool) (*watch.Watcher, error) { log.Printf("[INFO] (runner) creating watcher") w, err := watch.NewWatcher(&watch.NewWatcherInput{ diff --git a/runner_test.go b/runner_test.go index 15076448..b8de5b98 100644 --- a/runner_test.go +++ b/runner_test.go @@ -2,6 +2,7 @@ package main import ( "fmt" + config2 "github.com/hashicorp/envconsul/config" "reflect" "testing" @@ -174,24 +175,24 @@ func TestRunner_appendSecrets(t *testing.T) { for _, tc := range tt { t.Run(fmt.Sprintf("%s", tc.name), func(t *testing.T) { - cfg := map[bool]Config{true: Config{ - Secrets: &PrefixConfigs{ - &PrefixConfig{ + cfg := map[bool]config2.Config{true: config2.Config{ + Secrets: &config2.PrefixConfigs{ + &config2.PrefixConfig{ Path: config.String(tc.path), NoPrefix: tc.noPrefix, Format: &tc.format, }, }, - }, false: Config{ - Secrets: &PrefixConfigs{ - &PrefixConfig{ + }, false: config2.Config{ + Secrets: &config2.PrefixConfigs{ + &config2.PrefixConfig{ Path: config.String(tc.path), NoPrefix: tc.noPrefix, }, }, }}[tc.format != ""] - c := DefaultConfig().Merge(&cfg) + c := config2.DefaultConfig().Merge(&cfg) r, err := NewRunner(c, true) if err != nil { t.Fatal(err) @@ -240,7 +241,7 @@ func TestRunner_perKeyConfigurationOverride(t *testing.T) { upCase bool data *dependency.Secret expectedEnv map[string]string - keys *KeyFormats + keys *config2.KeyFormats }{ { name: "backward compatability, empty format, no key override", @@ -305,8 +306,8 @@ func TestRunner_perKeyConfigurationOverride(t *testing.T) { }, }, }, - keys: &KeyFormats{ - &KeyFormat{ + keys: &config2.KeyFormats{ + &config2.KeyFormat{ Name: config.String("user"), Format: config.String("DB_OVERRIDDEN_USER"), }, @@ -334,12 +335,12 @@ func TestRunner_perKeyConfigurationOverride(t *testing.T) { }, }, }, - keys: &KeyFormats{ - &KeyFormat{ + keys: &config2.KeyFormats{ + &config2.KeyFormat{ Name: config.String("user"), Format: config.String("DB_OVERRIDDEN_USER"), }, - &KeyFormat{ + &config2.KeyFormat{ Name: config.String("password"), Format: config.String("DB_OVERRIDDEN_PASSWORD"), }, @@ -367,12 +368,12 @@ func TestRunner_perKeyConfigurationOverride(t *testing.T) { }, }, }, - keys: &KeyFormats{ - &KeyFormat{ + keys: &config2.KeyFormats{ + &config2.KeyFormat{ Name: config.String("user"), Format: config.String("DB_OVERRIDDEN_USER"), }, - &KeyFormat{ + &config2.KeyFormat{ Name: config.String("password"), Format: config.String("DB_OVERRIDDEN_PASSWORD"), }, @@ -400,8 +401,8 @@ func TestRunner_perKeyConfigurationOverride(t *testing.T) { }, }, }, - keys: &KeyFormats{ - &KeyFormat{ + keys: &config2.KeyFormats{ + &config2.KeyFormat{ Name: config.String("password"), Format: config.String("DB_OVERRIDDEN_PASSWORD"), }, @@ -428,12 +429,12 @@ func TestRunner_perKeyConfigurationOverride(t *testing.T) { }, }, }, - keys: &KeyFormats{ - &KeyFormat{ + keys: &config2.KeyFormats{ + &config2.KeyFormat{ Name: config.String("password"), Format: config.String("DB_OVERRIDDEN_PASSWORD"), }, - &KeyFormat{ + &config2.KeyFormat{ Name: config.String("unknown_key"), Format: config.String("UNKNOWN_KEY_WILL_BE_IGNORED"), }, @@ -460,12 +461,12 @@ func TestRunner_perKeyConfigurationOverride(t *testing.T) { }, }, }, - keys: &KeyFormats{ - &KeyFormat{ + keys: &config2.KeyFormats{ + &config2.KeyFormat{ Name: config.String("password"), Format: nil, }, - &KeyFormat{ + &config2.KeyFormat{ Name: config.String("user"), Format: nil, }, @@ -479,9 +480,9 @@ func TestRunner_perKeyConfigurationOverride(t *testing.T) { for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { - cfg := Config{ - Secrets: &PrefixConfigs{ - &PrefixConfig{ + cfg := config2.Config{ + Secrets: &config2.PrefixConfigs{ + &config2.PrefixConfig{ NoPrefix: config.Bool(tc.noPrefix), Path: config.String(tc.path), Format: config.String(tc.format), @@ -491,7 +492,7 @@ func TestRunner_perKeyConfigurationOverride(t *testing.T) { Upcase: config.Bool(tc.upCase), } - c := DefaultConfig().Merge(&cfg) + c := config2.DefaultConfig().Merge(&cfg) r, err := NewRunner(c, true) if err != nil { t.Fatal(err) @@ -562,15 +563,15 @@ func TestRunner_appendPrefixes(t *testing.T) { for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { - cfg := Config{ - Prefixes: &PrefixConfigs{ - &PrefixConfig{ + cfg := config2.Config{ + Prefixes: &config2.PrefixConfigs{ + &config2.PrefixConfig{ Path: config.String(tc.path), NoPrefix: tc.noPrefix, }, }, } - c := DefaultConfig().Merge(&cfg) + c := config2.DefaultConfig().Merge(&cfg) r, err := NewRunner(c, true) if err != nil { t.Fatal(err) @@ -607,7 +608,7 @@ func TestRunner_appendServices(t *testing.T) { cases := []struct { name string query string - config Config + config config2.Config data []*dependency.CatalogService keyValue map[string]string serviceID string @@ -619,7 +620,7 @@ func TestRunner_appendServices(t *testing.T) { { name: "service appends data", query: "service", - config: Config{}, + config: config2.Config{}, data: []*dependency.CatalogService{ &dependency.CatalogService{ ServiceID: "id", @@ -645,7 +646,7 @@ func TestRunner_appendServices(t *testing.T) { { name: "service appends data", query: "service", - config: Config{}, + config: config2.Config{}, data: []*dependency.CatalogService{ &dependency.CatalogService{ ServiceID: "fail_id", @@ -678,9 +679,9 @@ func TestRunner_appendServices(t *testing.T) { { name: "service appends data with a custom format", query: "service", - config: Config{ - Services: &ServiceConfigs{ - &ServiceConfig{ + config: config2.Config{ + Services: &config2.ServiceConfigs{ + &config2.ServiceConfig{ Query: config.String("service"), FormatId: config.String("{{key}}/{{service}}/test"), FormatName: config.String("{{key}}/{{service}}/test"), @@ -716,7 +717,7 @@ func TestRunner_appendServices(t *testing.T) { for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { - c := DefaultConfig().Merge(&tc.config) + c := config2.DefaultConfig().Merge(&tc.config) r, err := NewRunner(c, true) if err != nil { t.Fatal(err) @@ -835,7 +836,7 @@ func TestRunner_configEnv(t *testing.T) { for _, tc := range tt { t.Run(tc.name, func(t *testing.T) { - cfg := Config{ + cfg := config2.Config{ Exec: &config.ExecConfig{ Env: &config.EnvConfig{ Pristine: &tc.pristine, @@ -845,7 +846,7 @@ func TestRunner_configEnv(t *testing.T) { }, }, } - c := DefaultConfig().Merge(&cfg) + c := config2.DefaultConfig().Merge(&cfg) r, err := NewRunner(c, true) if err != nil { t.Fatal(err) @@ -895,7 +896,7 @@ func TestRunner_configEnvDeprecated(t *testing.T) { for _, tc := range tt { t.Run(tc.name, func(t *testing.T) { - cfg := Config{ + cfg := config2.Config{ Exec: &config.ExecConfig{ Env: &config.EnvConfig{ Pristine: &tc.pristine, @@ -905,7 +906,7 @@ func TestRunner_configEnvDeprecated(t *testing.T) { }, }, } - c := DefaultConfig().Merge(&cfg) + c := config2.DefaultConfig().Merge(&cfg) r, err := NewRunner(c, true) if err != nil { t.Fatal(err) From 0667d1bcb6a0c44660f21ecf71180eb0eb8d2885 Mon Sep 17 00:00:00 2001 From: Hector Oliveros Date: Thu, 18 Aug 2022 22:40:59 -0400 Subject: [PATCH 2/2] Update go.mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 2c818e5c..9ddd0485 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/hashicorp/envconsul +module github.com/Eitol/envconsul go 1.12