From ed7f73739ea5ad270071872a8e373dd03f2e2935 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Mon, 15 Jan 2024 23:40:21 +0700 Subject: all: add command env-keys The "env-keys" command print list of environment variables under a directory. This command is internal, not documented, used by bash completion. --- awwan.go | 38 +++++++++++++++++++++++++++++++++++++ awwan_env_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/awwan/main.go | 15 +++++++++++++++ go.mod | 2 +- go.sum | 4 ++-- 5 files changed, 112 insertions(+), 3 deletions(-) diff --git a/awwan.go b/awwan.go index 0e96c03..f4fe389 100644 --- a/awwan.go +++ b/awwan.go @@ -227,6 +227,44 @@ func (aww *Awwan) EnvGet(dir, key string) (val string, err error) { return val, nil } +// EnvKeys load environment files from BaseDir to path and return all its +// keys. +// The path is optional, default to BaseDir if its empty. +func (aww *Awwan) EnvKeys(path string) (keys []string, err error) { + var ( + logp = `EnvKeys` + + dir string + fi os.FileInfo + ) + + if path == `` { + path = aww.BaseDir + } + + fi, err = os.Stat(path) + if err != nil { + return nil, fmt.Errorf(`%s: %w`, logp, err) + } + + if fi.IsDir() { + dir = path + } else { + dir = filepath.Dir(path) + } + + var ses *Session + + ses, err = NewSession(aww, dir) + if err != nil { + return nil, fmt.Errorf(`%s: %w`, logp, err) + } + + keys = ses.vars.Keys() + + return keys, nil +} + // EnvSet set the value in the environment file based on the key. // // The key is using the "
::" format. diff --git a/awwan_env_test.go b/awwan_env_test.go index 87d3b9f..ce386c9 100644 --- a/awwan_env_test.go +++ b/awwan_env_test.go @@ -74,6 +74,62 @@ func TestAwwanEnvGet(t *testing.T) { } } +func TestAwwanEnvKeys(t *testing.T) { + type testCase struct { + desc string + path string + expKeys []string + } + + var ( + baseDir = `testdata/env-get/` + + aww *Awwan + err error + ) + + aww, err = New(baseDir) + if err != nil { + t.Fatal(err) + } + + var cases = []testCase{{ + desc: `With empty path`, + expKeys: []string{ + `host::name`, + `user:database:pass`, + }, + }, { + desc: `With path is a directory`, + path: `testdata/env-get/myhost`, + expKeys: []string{ + `host::name`, + `user:database:pass`, + }, + }, { + desc: `With path is a file`, + path: `testdata/env-get/myhost/awwan.env`, + expKeys: []string{ + `host::name`, + `user:database:pass`, + }, + }} + + var ( + c testCase + gotKeys []string + ) + + for _, c = range cases { + gotKeys, err = aww.EnvKeys(c.path) + if err != nil { + t.Fatal(err) + } + + test.Assert(t, c.desc, c.expKeys, gotKeys) + } +} + func TestAwwanEnvSet(t *testing.T) { var ( baseDir = t.TempDir() diff --git a/cmd/awwan/main.go b/cmd/awwan/main.go index b431d2c..e7094ab 100644 --- a/cmd/awwan/main.go +++ b/cmd/awwan/main.go @@ -17,6 +17,11 @@ import ( ) const ( + // cmdEnvKeys print list of environment environment variables under + // a directory. + // This command is internal, used by bash completion. + cmdEnvKeys = `env-keys` + cmdHelp = "help" cmdVersion = "version" ) @@ -162,6 +167,11 @@ func main() { err = fmt.Errorf(`%s: missing key argument`, cmdMode) } + case cmdEnvKeys: + if flag.NArg() <= 1 { + err = fmt.Errorf(`%s: missing environment file argument`, cmdMode) + } + case awwan.CommandModeEnvSet: if flag.NArg() < 3 { err = fmt.Errorf(`%s: missing arguments`, cmdMode) @@ -233,6 +243,11 @@ func main() { } fmt.Println(val) + case cmdEnvKeys: + var keys []string + keys, _ = aww.EnvKeys(flag.Arg(1)) + fmt.Println(strings.Join(keys, ` `)) + case awwan.CommandModeEnvSet: err = aww.EnvSet(flag.Arg(1), flag.Arg(2), flag.Arg(3)) case awwan.CommandModeLocal: diff --git a/go.mod b/go.mod index b11769a..7ef491e 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ go 1.20 require ( git.sr.ht/~shulhan/ciigo v0.11.0 github.com/evanw/esbuild v0.19.8 - github.com/shuLhan/share v0.52.0 + github.com/shuLhan/share v0.52.1-0.20240114122306-3f26ff758251 ) require ( diff --git a/go.sum b/go.sum index a236647..df23707 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,8 @@ git.sr.ht/~shulhan/go-x-crypto v0.17.1-0.20231222080754-445dd75cd339 h1:iq2/NVwT git.sr.ht/~shulhan/go-x-crypto v0.17.1-0.20231222080754-445dd75cd339/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= github.com/shuLhan/esbuild v0.19.9-0.20231209212032-2dc984ffc5f1 h1:U4DRlREmugTNkevukauQjjUsz82o3YRjtbxDILoN/Xs= github.com/shuLhan/esbuild v0.19.9-0.20231209212032-2dc984ffc5f1/go.mod h1:D2vIQZqV/vIf/VRHtViaUtViZmG7o+kKmlBfVQuRi48= -github.com/shuLhan/share v0.52.0 h1:xbHez3JUYlt7WsUV8JOBLJOkd65P6dpp6zIq1EFXVvI= -github.com/shuLhan/share v0.52.0/go.mod h1:aNDs/SjnVYXaLEEJzjmfrUFeLD6u0YHWy6pI8o8iqYw= +github.com/shuLhan/share v0.52.1-0.20240114122306-3f26ff758251 h1:BD1mgC/2pKccoMvA/F86jXw/IKAomLzWjKhjdzpLX0U= +github.com/shuLhan/share v0.52.1-0.20240114122306-3f26ff758251/go.mod h1:aNDs/SjnVYXaLEEJzjmfrUFeLD6u0YHWy6pI8o8iqYw= github.com/yuin/goldmark v1.6.0 h1:boZcn2GTjpsynOsC0iJHnBWa4Bi0qzfJjthwauItG68= github.com/yuin/goldmark v1.6.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark-meta v1.1.0 h1:pWw+JLHGZe8Rk0EGsMVssiNb/AaPMHfSRszZeUeiOUc= -- cgit v1.3