From 6496027ab4a3ff2a0ea3a3d191ec9ef88159886d Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Sat, 5 Jun 2021 07:53:19 +0200 Subject: [PATCH] Add --env-file param and improve overall env-var loading --- app/cli.go | 24 ++++++++++++++++++++++-- pkg/cli/env.go | 41 +++++++++++++++++++++++++++++++++++++++++ pkg/logger/logger.go | 3 --- 3 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 pkg/cli/env.go diff --git a/app/cli.go b/app/cli.go index caa6deb10..542f01f4d 100644 --- a/app/cli.go +++ b/app/cli.go @@ -2,6 +2,7 @@ package app import ( "context" + "fmt" authCommands "github.com/cortezaproject/corteza-server/auth/commands" federationCommands "github.com/cortezaproject/corteza-server/federation/commands" @@ -14,9 +15,27 @@ import ( // InitCLI function initializes basic Corteza subsystems // and sets-up the command line interface func (app *CortezaApp) InitCLI() { - ctx := cli.Context() + var ( + ctx = cli.Context() - app.Command = cli.RootCommand(nil) + // path to all environmental files (or locations with .env file) + // filled from flag values + envs = []string{"."} + ) + + app.Command = cli.RootCommand(func() error { + if err := cli.LoadEnv(envs...); err != nil { + return fmt.Errorf("failed to load environmental variables: %w", err) + } + + return nil + }) + + app.Command.Flags().StringSliceVar(&envs, "env-file", nil, + "Load environmental variables from files and directories containing .env file.\n"+ + "Values from loaded files DO NOT override existing variables from the environment.\n"+ + "This flag can be used multiple times, values are loaded from all provided locations.\n"+ + "If no paths are provided, corteza loads .env file from the current directory (equivalent to --env-file .)") serveCmd := cli.ServeCommand(func() (err error) { if err = app.Activate(ctx); err != nil { @@ -62,6 +81,7 @@ func (app *CortezaApp) InitCLI() { provisionCmd, authCommands.General(app, app.Opt.Auth), federationCommands.Sync(app), + cli.EnvCommand(), cli.VersionCommand(), ) diff --git a/pkg/cli/env.go b/pkg/cli/env.go new file mode 100644 index 000000000..54a1733a1 --- /dev/null +++ b/pkg/cli/env.go @@ -0,0 +1,41 @@ +package cli + +import ( + "os" + "path" + "sort" + + "github.com/joho/godotenv" + "github.com/spf13/cobra" +) + +// LoadEnv loads env-variables (KEY=VAL) from provided files and paths (by searching for .env file in the given path) +// +// Please not that loaded values DO NOT OVERRIDE the existing environmental variables +func LoadEnv(pp ...string) error { + // preparse the input and try to figure out if .env should be appended + for i, p := range pp { + if s, err := os.Stat(p); err != nil { + return err + } else if s.IsDir() { + pp[i] = path.Join(p, ".env") + } + } + + return godotenv.Load(pp...) +} + +// EnvCommand outputs all loaded env variables +func EnvCommand() *cobra.Command { + return &cobra.Command{ + Use: "env", + Long: "Outputs list (sorted by key) of of all environmental variables. Can be used for diagnosis and debugging", + Run: func(cmd *cobra.Command, args []string) { + kv := os.Environ() + sort.Strings(kv) + for _, l := range kv { + cmd.Println(l) + } + }, + } +} diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 5e884dce7..ca354bf30 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -4,9 +4,6 @@ import ( "os" "time" - // Make sure we read the ENV from .env - _ "github.com/joho/godotenv/autoload" - "go.uber.org/zap" "go.uber.org/zap/zapcore"