From db1c3b82397e19c6bd3564e0c6fcbfac0beaa1ed Mon Sep 17 00:00:00 2001 From: Dmitry Ledentsov Date: Sat, 30 Dec 2023 23:21:57 +0100 Subject: [PATCH] candidate env var sources --- common/known_paths.go | 35 +++++++++++++++++++++++++++++++++++ common/known_paths_test.go | 16 ++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 common/known_paths.go create mode 100644 common/known_paths_test.go diff --git a/common/known_paths.go b/common/known_paths.go new file mode 100644 index 0000000..41e7c66 --- /dev/null +++ b/common/known_paths.go @@ -0,0 +1,35 @@ +package common + +import ( + "github.com/mitchellh/go-homedir" +) + +var knownPaths = []string{ + // global + "/etc/profile", + "/etc/zprofile", + "/etc/zlogin", + "/etc/zshenv", + // generic + "~/.profile", + // zsh: https://zsh.sourceforge.io/Doc/Release/Files.html#Startup_002fShutdown-Files + "~/.zshenv", + "~/.zprofile", + "~/.zshrc", + "~/.zlogin", + // bash: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html + "~/.bashrc", + "~/.bash_profile", + "~/.bash_login", +} + +// ForEachKnownPath iterates over expanded known paths if they're legitimate +func ForEachKnownPath(fn func(string, string)) { + for _, knownPath := range knownPaths { + expanded, err := homedir.Expand(knownPath) + if err != nil { + continue + } + fn(knownPath, expanded) + } +} diff --git a/common/known_paths_test.go b/common/known_paths_test.go new file mode 100644 index 0000000..d5e18c4 --- /dev/null +++ b/common/known_paths_test.go @@ -0,0 +1,16 @@ +package common + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_all_known_paths_can_be_expanded(t *testing.T) { + count := 0 + ForEachKnownPath(func(_, _ string) { + count++ + }) + assert.Greater(t, count, 0) + assert.Equal(t, count, len(knownPaths)) +}