Skip to content

Commit

Permalink
feat!: if SERVICE_ENV is not set, default to dev
Browse files Browse the repository at this point in the history
If an environment is not set, it will try to load weird paths, which
might not exist in Vault

Make sure SERVICE_NAME is set as a lot of paths need that to work

Remove peer references as that's no longer a think

BREAKING CHANGE: will exit if SERVICE_NAME is not set
  • Loading branch information
mloberg committed Oct 16, 2023
1 parent a6f51d7 commit fcbd42b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
13 changes: 12 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ linters:
- misspell
- whitespace
- goimports
- gofmt
- gosec
- testpackage
- lll
- unused
- nonamedreturns
- tenv
- funlen
- nestif
- forbidigo
disable:
- scopelint
- rowserrcheck
Expand All @@ -25,3 +31,8 @@ linters-settings:
min-complexity: 10
goimports:
local-prefixes: github.com/articulate/docker-consul-template-bootstrap
forbidigo:
analyze-types: true
forbid:
- ^(fmt\\.Print(|f|ln)|print|println)$
- ^spew\.(ConfigState\.)?Dump$
30 changes: 17 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ func main() {
Region: os.Getenv("AWS_REGION"),
}

if cfg.Service == "" {
log.Fatal().Msg("SERVICE_NAME cannot be blank")
}

if cfg.Environment == "" {
log.Warn().Msg("SERVICE_ENV is blank, defaulting to dev")
cfg.Environment = "dev"
}

if cfg.Region == "" {
cfg.Region = "us-east-1"
}
Expand All @@ -37,22 +46,11 @@ func main() {
Str("region", cfg.Region).
Logger()

// handles peer environments (peer-some-thing => peer), which loads stage vars
if strings.HasPrefix(cfg.Environment, "peer") {
cfg.Environment = "stage"
}

if len(os.Args) < 2 {
logger.Fatal().Msg("Missing command")
}

env := NewEnvMap()
pwd, err := os.Getwd()
if err != nil {
logger.Warn().Err(err).Msg("Cannot determine PWD")
}
env.Add("PWD", pwd)
env.Add("AWS_REGION", cfg.Region)

if addr := os.Getenv("CONSUL_ADDR"); addr != "" {
env.Merge(loadConsul(addr, cfg, logger))
Expand All @@ -66,10 +64,16 @@ func main() {
logger.Warn().Msg("Not loading values from Vault. VAULT_ADDR is not set")
}

pwd, err := os.Getwd()
if err != nil {
logger.Warn().Err(err).Msg("Cannot determine PWD")
}
env.Add("PWD", pwd)
env.Add("AWS_REGION", cfg.Region)
env.Add("SERVICE_ENV", cfg.Environment)
env.Add("PROCESSOR_COUNT", strconv.Itoa(runtime.NumCPU()))

exit := run(os.Args[1], os.Args[2:], env.Environ(), logger)
os.Exit(exit)
os.Exit(run(os.Args[1], os.Args[2:], env.Environ(), logger))
}

func loadConsul(addr string, c Config, l zerolog.Logger) Dict {
Expand Down

0 comments on commit fcbd42b

Please sign in to comment.