aboutsummaryrefslogtreecommitdiff
path: root/awwan.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2024-01-16 22:00:38 +0700
committerShulhan <ms@kilabit.info>2024-01-16 22:22:19 +0700
commit71b40922e6b6cc4f2d4b3fa94cbe586720bf6f8c (patch)
treee66171fb37dd51af1ac1b09beb02339b4f66ed92 /awwan.go
parenta1339c8b1fb34d272426f97ca42cecd7971e627c (diff)
downloadawwan-71b40922e6b6cc4f2d4b3fa94cbe586720bf6f8c.tar.xz
all: refactoring "env-set" arguments
Previously, the "env-set" take the file argument as the last argument and optional. This changes move the file argument to the first argument so the bash completion can detect and print the completion for list of keys. While at it, fix handling key with quoted in EnvSet and EnvGet. Closes: https://todo.sr.ht/~shulhan/awwan/10
Diffstat (limited to 'awwan.go')
-rw-r--r--awwan.go46
1 files changed, 30 insertions, 16 deletions
diff --git a/awwan.go b/awwan.go
index f4fe389..d604d8d 100644
--- a/awwan.go
+++ b/awwan.go
@@ -9,6 +9,7 @@ import (
"log"
"os"
"path/filepath"
+ "strconv"
"strings"
"time"
@@ -215,6 +216,13 @@ func (aww *Awwan) EnvGet(dir, key string) (val string, err error) {
return ``, fmt.Errorf(`%s: empty key`, logp)
}
+ var unqkey string
+
+ unqkey, err = strconv.Unquote(key)
+ if err == nil {
+ key = unqkey
+ }
+
var ses *Session
ses, err = NewSession(aww, dir)
@@ -267,13 +275,17 @@ func (aww *Awwan) EnvKeys(path string) (keys []string, err error) {
// EnvSet set the value in the environment file based on the key.
//
-// The key is using the "<section>:<sub>:<name>" format.
+// The file parameter point to "awwan.env" or INI formatted file.
//
-// 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) {
+// The key is using the "<section>:<sub>:<name>" format.
+func (aww *Awwan) EnvSet(file, key, val string) (err error) {
var logp = `EnvSet`
+ file = strings.TrimSpace(file)
+ if len(file) == 0 {
+ return fmt.Errorf(`%s: empty file`, logp)
+ }
+
key = strings.TrimSpace(key)
if len(key) == 0 {
return fmt.Errorf(`%s: empty key`, logp)
@@ -284,20 +296,15 @@ func (aww *Awwan) EnvSet(key, val, file string) (err error) {
return fmt.Errorf(`%s: empty value`, logp)
}
- file = strings.TrimSpace(file)
- if len(file) == 0 {
- file = filepath.Join(aww.currDir, defEnvFileName)
- }
-
- var env *ini.Ini
-
- env, err = ini.Open(file)
- if err != nil {
- return fmt.Errorf(`%s: %w`, logp, err)
+ var (
+ tags []string
+ unqkey string
+ )
+ unqkey, err = strconv.Unquote(key)
+ if err == nil {
+ key = unqkey
}
- var tags []string
-
tags = ini.ParseTag(key)
if len(tags[0]) == 0 {
@@ -307,6 +314,13 @@ func (aww *Awwan) EnvSet(key, val, file string) (err error) {
return fmt.Errorf(`%s: missing name in key`, logp)
}
+ var env *ini.Ini
+
+ env, err = ini.Open(file)
+ if err != nil {
+ return fmt.Errorf(`%s: %w`, logp, err)
+ }
+
var ok bool
ok = env.Set(tags[0], tags[1], tags[2], val)