diff --git a/README.md b/README.md index 63a724a..621f83b 100644 --- a/README.md +++ b/README.md @@ -74,14 +74,15 @@ COMMANDS: help, h Shows a list of commands or help for one command GLOBAL OPTIONS: - --env value, -e value Additional environment variables (accepts multiple inputs) - --file value, -f value Paths to env files (default: ".env") (accepts multiple inputs) - --help, -h show help (default: false) - --interpolate, -i Interpolate environment variables in command arguments (default: false) - --override, -o Override existing environment variables with new ones (default: true) - --silent, -s Ignore errors if .env file is not found (default: false) - --version, -v print the version (default: false) - --watch, -w Watch for changes in .env files and reload them (default: false) + --env value, -e value [ --env value, -e value ] Additional environment variables + --file value, -f value [ --file value, -f value ] Paths to env files (default: ".env") + --help, -h show help (default: false) + --interpolate, -i Interpolate environment variables in command arguments (default: false) + --override, -o Override existing environment variables with new ones (default: true) + --prefix value, -p value Prefix for environment variables + --silent, -s Ignore errors if .env file is not found (default: false) + --version, -v print the version (default: false) + --watch, -w Watch for changes in .env files and reload them (default: false) ``` ## 🧬 Related Projects diff --git a/cmd/main.go b/cmd/main.go index b0dca2e..00ec607 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -55,6 +55,12 @@ func main() { Value: false, Usage: "Watch for changes in .env files and reload them", }, + &cli.StringFlag{ + Name: "prefix", + Aliases: []string{"p"}, + Value: "", + Usage: "Prefix for environment variables", + }, }, Commands: []*cli.Command{ commands.List(), diff --git a/internal/common/read.go b/internal/common/read.go index 3671fb7..dc808ef 100644 --- a/internal/common/read.go +++ b/internal/common/read.go @@ -13,6 +13,7 @@ func Read(cCtx *cli.Context) (dict map[string]string, err error) { filenames := cCtx.StringSlice("file") extraEnvs := cCtx.StringSlice("env") silent := cCtx.Bool("silent") + prefix := cCtx.String("prefix") dict, err = readFiles(filenames, silent) @@ -29,6 +30,10 @@ func Read(cCtx *cli.Context) (dict map[string]string, err error) { dict[kv[0]] = kv[1] } + if prefix != "" { + dict = prefixDict(dict, prefix) + } + return } @@ -65,3 +70,13 @@ func readFile(filename string) (envMap map[string]string, err error) { return godotenv.Parse(file) } + +func prefixDict(dict map[string]string, prefix string) map[string]string { + prefixedDict := make(map[string]string) + + for key, value := range dict { + prefixedDict[prefix+key] = value + } + + return prefixedDict +}