Fixed absolute paths handling in pathx.

This commit is contained in:
Andrey Parhomenko 2023-02-17 08:57:57 +05:00
parent 8ec37e9279
commit 7572eb7648
4 changed files with 26 additions and 11 deletions

View file

@ -2,8 +2,8 @@
wd=`pwd` wd=`pwd`
cd src/cmd/goblin && go install && cd $wd cd $wd/src/cmd/goblin && go install && cd $wd
mkdir -p $HOME/app/goblin mkdir -p $HOME/app/goblin
cp -rf app/* $HOME/app/goblin cp -rf $wd/app/* $HOME/app/goblin

View file

@ -12,6 +12,7 @@ import (
type Value string type Value string
type Path struct { type Path struct {
Values []Value Values []Value
IsAbs bool
} }
func (p Path) Append(values ...string) Path { func (p Path) Append(values ...string) Path {
@ -22,7 +23,15 @@ func (p Path) Append(values ...string) Path {
} }
func From(p string) Path { func From(p string) Path {
ret := Path{}
if len(p) == 0 {
return ret
}
p = path.Clean(p) p = path.Clean(p)
if p[0] == '/' {
ret.IsAbs = true
}
p, _ = strings.CutSuffix(p, "/") p, _ = strings.CutSuffix(p, "/")
svalues := strings.Split(p, "/") svalues := strings.Split(p, "/")
@ -30,10 +39,9 @@ func From(p string) Path {
for i, s := range svalues { for i, s := range svalues {
values[i] = Value(s) values[i] = Value(s)
} }
ret.Values = values
return Path{ return ret
Values: values,
}
} }
func (v Value) IsValid() bool { func (v Value) IsValid() bool {
@ -45,12 +53,16 @@ func (v Value) Err() error {
} }
func (p Path) StringValues() []string { func (p Path) StringValues() []string {
values := make([]string, len(p.Values)) ret := []string{}
for i, v := range p.Values { if p.IsAbs {
values[i] = string(v) ret = append(ret, "/")
} }
return values for _, v := range p.Values {
ret = append(ret, string(v))
}
return ret
} }
func (p Path) Real() string { func (p Path) Real() string {

View file

@ -331,7 +331,6 @@ func Run(args []string) {
mkincdir == "" { mkincdir == "" {
homeDir, _ := os.UserHomeDir() homeDir, _ := os.UserHomeDir()
os.Setenv("MKINCDIR", homeDir + "/app/goblin/mk/inc" ) os.Setenv("MKINCDIR", homeDir + "/app/goblin/mk/inc" )
fmt.Println("shit:", os.Getenv("MKINCDIR"))
} }
flags := flag.NewFlagSet(arg0, flag.ExitOnError) flags := flag.NewFlagSet(arg0, flag.ExitOnError)

View file

@ -163,7 +163,11 @@ func parseRedirInclude(p *parser, t token) parserStateFun {
filename := "" filename := ""
//fmt.Printf("'%v'\n", p.tokenbuf) //fmt.Printf("'%v'\n", p.tokenbuf)
for i := range p.tokenbuf { for i := range p.tokenbuf {
filename += expand(p.tokenbuf[i].val, p.rules.vars, true)[0] filename += expand(
p.tokenbuf[i].val,
p.rules.vars,
true,
)[0]
} }
file, err := os.Open(pathx.From(filename).Real()) file, err := os.Open(pathx.From(filename).Real())
if err != nil { if err != nil {