From 19f5053c19bdd2f9f4f764eb17ef0f096eb87f3e Mon Sep 17 00:00:00 2001 From: Shulhan Date: Fri, 17 Nov 2023 03:18:52 +0700 Subject: all: implement command "env-get" to get value from environment files The env-get command get the value from environment files. Syntax, ---- [dir] ---- The "key" argument define the key where value is stored in environment using "section:sub:name" format. The "dir" argument is optional, its define the directory where environment files will be loaded, recursively, from BaseDir to dir. If its empty default to the current directory. --- awwan.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'awwan.go') diff --git a/awwan.go b/awwan.go index 7e4aee9..f0e71bd 100644 --- a/awwan.go +++ b/awwan.go @@ -22,6 +22,7 @@ var Version = `0.9.0` const ( CommandModeDecrypt = `decrypt` CommandModeEncrypt = `encrypt` + CommandModeEnvGet = `env-get` CommandModeEnvSet = `env-set` CommandModeLocal = `local` CommandModePlay = `play` @@ -164,6 +165,44 @@ func (aww *Awwan) Encrypt(file string) (fileVault string, err error) { return fileVault, nil } +// EnvGet get the value of environment based on the key. +// This method is similar to [Session.Val] when executed inside the script. +// +// The dir parameter is optional, its define the directory where environment +// files will be loaded, recursively, from BaseDir to dir. +// If its empty default to the current directory. +// +// The key parameter is using the "
::" format. +// +// If the key is not exist it will return an empty string. +func (aww *Awwan) EnvGet(dir, key string) (val string, err error) { + var logp = `EnvGet` + + dir = strings.TrimSpace(dir) + if len(dir) == 0 { + dir, err = os.Getwd() + if err != nil { + return ``, fmt.Errorf(`%s: empty key`, logp) + } + } + + key = strings.TrimSpace(key) + if len(key) == 0 { + return ``, fmt.Errorf(`%s: empty key`, logp) + } + + var ses *Session + + ses, err = NewSession(aww, dir) + if err != nil { + return ``, fmt.Errorf(`%s: %w`, logp, err) + } + + val = ses.vars.Val(key) + + return val, nil +} + // EnvSet set key with value in the environment file. // // The key is using the "
::" format. -- cgit v1.3