MCPcopy
hub / github.com/micro-editor/micro / ReplaceHome

Function ReplaceHome

internal/util/util.go:400–424  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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 '~'.
400func 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

Callers 5

CdCmdMethod · 0.92
backupDirMethod · 0.92
saveToFileMethod · 0.92
FileCompleteFunction · 0.92

Calls 2

ReplaceMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected