-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd: add cross-platform GetHomePath() (#552)
- Loading branch information
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,42 @@ | ||
package local | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"runtime" | ||
) | ||
|
||
const ( | ||
// EnvSSHPassphrase is the key used to fetch PEM key passphrases | ||
EnvSSHPassphrase = "PEM_PASSPHRASE" | ||
) | ||
|
||
// GetHomePath returns the homepath based on the operating system used | ||
func GetHomePath() (string, error) { | ||
if runtime.GOOS == "windows" { | ||
// Performs check for HOME env variable first | ||
if home := os.Getenv("HOME"); home != "" { | ||
return home, nil | ||
} | ||
|
||
// Performs check for USERPROFILE as backup | ||
if home := os.Getenv("USERPROFILE"); home != "" { | ||
return home, nil | ||
} | ||
|
||
// Builds HOME from HOMEDRIVE and HOMEPATH as default | ||
var drive = os.Getenv("HOMEDRIVE") | ||
var path = os.Getenv("HOMEPATH") | ||
var home = drive + path | ||
if drive == "" || path == "" { | ||
return "", errors.New("HOMEDRIVE, HOMEPATH, or USERPROFILE environment variables are blank") | ||
} | ||
|
||
return home, nil | ||
} | ||
// Unix system as default | ||
if home := os.Getenv("HOME"); home != "" { | ||
return home, nil | ||
} | ||
return "", errors.New("HOME environment variable is blank") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package local | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetHomePath(t *testing.T) { | ||
env, err := GetHomePath() | ||
assert.NoError(t, err) | ||
assert.NotEqual(t, env, "") | ||
assert.DirExists(t, env) | ||
} |