streamline env gets with defaults

master
nate smith 2019-09-10 16:44:58 -05:00
parent b43e94c2ed
commit defacb9f58
1 changed files with 8 additions and 11 deletions

19
main.go
View File

@ -64,25 +64,23 @@ type tildeData struct {
News []newsEntry // Collection of town news entries News []newsEntry // Collection of town news entries
} }
func homesDir() string { func getEnvDefault(key, def string) string {
hDir := os.Getenv("HOMES_DIR") result := os.Getenv(key)
if hDir == "" { if result == "" {
hDir = "/home" result = def
} }
return result
return hDir
} }
func homesDir() string { return getEnvDefault("HOMES_DIR", "/home") }
func getNews() (entries []newsEntry, err error) { func getNews() (entries []newsEntry, err error) {
inMeta := true inMeta := true
inContent := false inContent := false
current := newsEntry{} current := newsEntry{}
blankLineRe := regexp.MustCompile(`^ *\n$`) blankLineRe := regexp.MustCompile(`^ *\n$`)
newsPath := os.Getenv("NEWS_PATH") newsPath := getEnvDefault("NEWS_PATH", "/town/news.posts")
if newsPath == "" {
newsPath = "/town/news.posts"
}
newsFile, err := os.Open(newsPath) newsFile, err := os.Open(newsPath)
if err != nil { if err != nil {
@ -243,7 +241,6 @@ func (x byMtime) Less(i, j int) bool { return x[i].Mtime > x[j].Mtime } // becau
func (x byMtime) Swap(i, j int) { x[i], x[j] = x[j], x[i] } func (x byMtime) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func getUsers() (users []*user, err error) { func getUsers() (users []*user, err error) {
// TODO sort by mtime
// For the purposes of this program, we discover users via: // For the purposes of this program, we discover users via:
// - presence in /home/ // - presence in /home/
// - absence in systemUsers list (sourced from source code and potentially augmented by an environment variable) // - absence in systemUsers list (sourced from source code and potentially augmented by an environment variable)