Add --env-file param and improve overall env-var loading
This commit is contained in:
24
app/cli.go
24
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(),
|
||||
)
|
||||
|
||||
|
||||
41
pkg/cli/env.go
Normal file
41
pkg/cli/env.go
Normal file
@@ -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)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user