Skip to content

Commit

Permalink
candidate env var sources
Browse files Browse the repository at this point in the history
  • Loading branch information
d-led committed Dec 30, 2023
1 parent 18816b8 commit db1c3b8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
35 changes: 35 additions & 0 deletions common/known_paths.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
16 changes: 16 additions & 0 deletions common/known_paths_test.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit db1c3b8

Please sign in to comment.