aboutsummaryrefslogtreecommitdiff
path: root/awwan.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-11-17 21:40:04 +0700
committerShulhan <ms@kilabit.info>2023-11-17 21:40:04 +0700
commit49f18f5fde5350545c6399823edd31ad8826fb90 (patch)
tree41906b7da6b1a8da285527a80f1308c785b14380 /awwan.go
parent6535427ec6de33da4c0eaa88c63318dff827b27b (diff)
downloadawwan-49f18f5fde5350545c6399823edd31ad8826fb90.tar.xz
all: move the file argument in env-set to the last argument
The "file" argument is optional, its define path to environment file. If its empty it will be set to "awwan.env" in the current directory.
Diffstat (limited to 'awwan.go')
-rw-r--r--awwan.go29
1 files changed, 22 insertions, 7 deletions
diff --git a/awwan.go b/awwan.go
index f0e71bd..030d284 100644
--- a/awwan.go
+++ b/awwan.go
@@ -18,6 +18,12 @@ import (
// Version current version of this module (library and program).
var Version = `0.9.0`
+// osGetwd define the handler to get current working directory.
+//
+// This variable will be overriden in testing to test running awwan in sub
+// directory of workspace.
+var osGetwd = os.Getwd
+
// List of command available for program awwan.
const (
CommandModeDecrypt = `decrypt`
@@ -203,17 +209,15 @@ func (aww *Awwan) EnvGet(dir, key string) (val string, err error) {
return val, nil
}
-// EnvSet set key with value in the environment file.
+// EnvSet set the value in the environment file based on the key.
//
// The key is using the "<section>:<sub>:<name>" format.
-func (aww *Awwan) EnvSet(file, key, val string) (err error) {
+//
+// The file is optional, if its empty default to "awwan.env" in the current
+// directory.
+func (aww *Awwan) EnvSet(key, val, file string) (err error) {
var logp = `EnvSet`
- file = strings.TrimSpace(file)
- if len(file) == 0 {
- return fmt.Errorf(`%s: empty file argument`, logp)
- }
-
key = strings.TrimSpace(key)
if len(key) == 0 {
return fmt.Errorf(`%s: empty key`, logp)
@@ -224,6 +228,17 @@ func (aww *Awwan) EnvSet(file, key, val string) (err error) {
return fmt.Errorf(`%s: empty value`, logp)
}
+ file = strings.TrimSpace(file)
+ if len(file) == 0 {
+ var wd string
+ wd, err = osGetwd()
+ if err != nil {
+ return fmt.Errorf(`%s: %w`, logp, err)
+ }
+
+ file = filepath.Join(wd, defEnvFileName)
+ }
+
var env *ini.Ini
env, err = ini.Open(file)