2019-01-12 04:58:27 +00:00
|
|
|
package resolvehome
|
|
|
|
|
|
|
|
import (
|
2019-05-09 22:03:45 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-01-12 04:58:27 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
homes = []string{"$HOME", "${HOME}", "~"}
|
|
|
|
)
|
|
|
|
|
|
|
|
func Resolve(s string) (string, error) {
|
|
|
|
for _, home := range homes {
|
|
|
|
if strings.Contains(s, home) {
|
2019-05-09 22:03:45 +00:00
|
|
|
homeDir, err := os.UserHomeDir()
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "determining current user")
|
|
|
|
}
|
|
|
|
s = strings.Replace(s, home, homeDir, -1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
2020-03-26 21:07:15 +00:00
|
|
|
}
|