ReplaceHome takes a path as input and replaces ~ at the start of the path with the user's home directory. Does nothing if the path does not start with '~'.
(path string)
| 398 | // ReplaceHome takes a path as input and replaces ~ at the start of the path with the user's |
| 399 | // home directory. Does nothing if the path does not start with '~'. |
| 400 | func ReplaceHome(path string) (string, error) { |
| 401 | if !strings.HasPrefix(path, "~") { |
| 402 | return path, nil |
| 403 | } |
| 404 | |
| 405 | var userData *user.User |
| 406 | var err error |
| 407 | |
| 408 | homeString := strings.Split(path, "/")[0] |
| 409 | if homeString == "~" { |
| 410 | userData, err = user.Current() |
| 411 | if err != nil { |
| 412 | return "", errors.New("Could not find user: " + err.Error()) |
| 413 | } |
| 414 | } else { |
| 415 | userData, err = user.Lookup(homeString[1:]) |
| 416 | if err != nil { |
| 417 | return "", errors.New("Could not find user: " + err.Error()) |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | home := userData.HomeDir |
| 422 | |
| 423 | return strings.Replace(path, homeString, home, 1), nil |
| 424 | } |
| 425 | |
| 426 | // GetPathAndCursorPosition returns a filename without everything following a `:` |
| 427 | // This is used for opening files like util.go:10:5 to specify a line and column |
no test coverage detected