]> git.lizzy.rs Git - micro.git/commitdiff
Add support for ~username syntax (fix #1033) (#1035)
authorDanielPower <me@danielpower.ca>
Fri, 30 Mar 2018 20:20:51 +0000 (17:50 -0230)
committerZachary Yedidia <zyedidia@gmail.com>
Fri, 30 Mar 2018 20:20:51 +0000 (16:20 -0400)
* Add support for ~username syntax (fix #1033)

* Fixed return string

Also removed non-descriptive variable name `foo`

* moved err declarations outside of if statement

cmd/micro/util.go

index 833562941436689ca668456c41ef51c55c89fe06..a934191ff98e4606f4688c674b71325b0b6695f4 100644 (file)
@@ -2,6 +2,7 @@ package main
 
 import (
        "os"
+       "os/user"
        "path/filepath"
        "reflect"
        "runtime"
@@ -9,9 +10,8 @@ import (
        "strings"
        "time"
        "unicode/utf8"
-
+       
        "github.com/mattn/go-runewidth"
-       homedir "github.com/mitchellh/go-homedir"
 )
 
 // Util.go is a collection of utility functions that are used throughout
@@ -330,12 +330,25 @@ func ReplaceHome(path string) string {
                return path
        }
 
-       home, err := homedir.Dir()
-       if err != nil {
-               messenger.Error("Could not find home directory: ", err)
-               return path
+       var userData *user.User
+       var err error
+
+       homeString := strings.Split(path, "/")[0]
+       if homeString == "~" {
+               userData, err = user.Current()
+               if err != nil {
+                       messenger.Error("Could not find user: ", err)
+               }
+       } else {
+               userData, err = user.Lookup(homeString[1:])
+               if err != nil {
+                       messenger.Error("Could not find user: ", err)
+               }
        }
-       return strings.Replace(path, "~", home, 1)
+
+       home := userData.HomeDir
+
+       return strings.Replace(path, homeString, home, 1)
 }
 
 // GetPath returns a filename without everything following a `:`