~shulhan/awwan

ed7f73739ea5ad270071872a8e373dd03f2e2935 — Shulhan 10 months ago 7e2dafc
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.
5 files changed, 112 insertions(+), 3 deletions(-)

M awwan.go
M awwan_env_test.go
M cmd/awwan/main.go
M go.mod
M go.sum
M awwan.go => awwan.go +38 -0
@@ 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 "<section>:<sub>:<name>" format.

M awwan_env_test.go => awwan_env_test.go +56 -0
@@ 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()

M cmd/awwan/main.go => cmd/awwan/main.go +15 -0
@@ 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:

M go.mod => go.mod +1 -1
@@ 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 (

M go.sum => go.sum +2 -2
@@ 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=