kovetskiy / mark

Sync your markdown files with Confluence pages.
https://samizdat.dev
Other
988 stars 147 forks source link

os.Getenv("HOME") need not be the homedirectory #352

Closed Skeeve closed 11 months ago

Skeeve commented 11 months ago

In main.go there are three locations where the user's homedirectory is required.

Since I wanted to enhance template inclusion by allowing ~/ I also required the homedirectory. So I searched and found this:

https://stackoverflow.com/questions/7922270/obtain-users-home-directory

Since go 1.12 the recommended way is:

package main
import (
    "os"
    "fmt"
    "log"
)
func main() {
    dirname, err := os.UserHomeDir()
    if err != nil {
        log.Fatal( err )
    }
    fmt.Println( dirname )
}

Shouldn't we change accordingly?

mrueg commented 11 months ago

Good catch, yes please use https://pkg.go.dev/os#UserHomeDir

Skeeve commented 11 months ago

Maybe even better:

func UserConfigDir added in go1.13

func UserConfigDir() (string, error) UserConfigDir returns the default root directory to use for user-specific configuration data. Users should create their own application-specific subdirectory within this one and use that.

On Unix systems, it returns $XDG_CONFIG_HOME as specified by https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html if non-empty, else $HOME/.config. On Darwin, it returns $HOME/Library/Application Support. On Windows, it returns %AppData%. On Plan 9, it returns $home/lib.

If the location cannot be determined (for example, $HOME is not defined), then it will return an error.

Simply because the .config is currently hardcoded and this function would return the OS-Specific directory.

mrueg commented 11 months ago

That sounds great as well! :)