You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nimf/files.go

57 lines
1.1 KiB

package main
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"
)
var inlinedFiles map[string]bool = map[string]bool{}
func readFile(path string) (string, error) {
b, err := ioutil.ReadFile(ExpandedAbsFilepath(path))
if err != nil {
return "", fmt.Errorf("Unable to open file %s\n", path)
}
return string(b), nil
}
func ExpandedAbsFilepath(p string) string {
if strings.HasPrefix(p, "~") {
if p == "~" || strings.HasPrefix(p, "~/") {
homedir, _ := os.UserHomeDir()
if len(p) <= 2 {
p = homedir
} else if len(p) > 2 {
p = filepath.Join(homedir, p[2:])
}
} else {
i := strings.IndexRune(p, '/')
var u string
var remainder string
if i < 0 {
u = p[1:]
remainder = ""
} else {
u = p[1:i]
remainder = p[i:]
}
usr, err := user.Lookup(u)
if err != nil {
p = filepath.Join("/home", u, remainder)
} else {
p = filepath.Join(usr.HomeDir, remainder)
}
}
} else if !strings.HasPrefix(p, "/") {
wd, _ := os.Getwd()
p = filepath.Join(wd, p)
}
path, _ := filepath.Abs(p)
return path
}