diff options
| author | Shulhan <ms@kilabit.info> | 2023-11-17 03:18:52 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-11-17 03:18:52 +0700 |
| commit | 19f5053c19bdd2f9f4f764eb17ef0f096eb87f3e (patch) | |
| tree | 2c6553269f2c3d562fb9f0f7950268c90dd6a198 /awwan.go | |
| parent | 68cbc52f2195063a0b4c9ce8c6c4cf030182c53a (diff) | |
| download | awwan-19f5053c19bdd2f9f4f764eb17ef0f096eb87f3e.tar.xz | |
all: implement command "env-get" to get value from environment files
The env-get command get the value from environment files.
Syntax,
----
<key> [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.
Diffstat (limited to 'awwan.go')
| -rw-r--r-- | awwan.go | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -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 "<section>:<sub>:<name>" 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 "<section>:<sub>:<name>" format. |
