From 7572eb7648058a1ba19e36dd97ff01bcda1bb76c Mon Sep 17 00:00:00 2001 From: surdeus Date: Fri, 17 Feb 2023 08:57:57 +0500 Subject: [PATCH] Fixed absolute paths handling in pathx. --- install.sh | 4 ++-- src/pathx/main.go | 26 +++++++++++++++++++------- src/tool/mk/main.go | 1 - src/tool/mk/parse.go | 6 +++++- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/install.sh b/install.sh index 4887a72..81f48db 100755 --- a/install.sh +++ b/install.sh @@ -2,8 +2,8 @@ wd=`pwd` -cd src/cmd/goblin && go install && cd $wd +cd $wd/src/cmd/goblin && go install && cd $wd mkdir -p $HOME/app/goblin -cp -rf app/* $HOME/app/goblin +cp -rf $wd/app/* $HOME/app/goblin diff --git a/src/pathx/main.go b/src/pathx/main.go index 089395c..ba50d53 100644 --- a/src/pathx/main.go +++ b/src/pathx/main.go @@ -12,6 +12,7 @@ import ( type Value string type Path struct { Values []Value + IsAbs bool } func (p Path) Append(values ...string) Path { @@ -22,7 +23,15 @@ func (p Path) Append(values ...string) Path { } func From(p string) Path { + ret := Path{} + if len(p) == 0 { + return ret + } + p = path.Clean(p) + if p[0] == '/' { + ret.IsAbs = true + } p, _ = strings.CutSuffix(p, "/") svalues := strings.Split(p, "/") @@ -30,10 +39,9 @@ func From(p string) Path { for i, s := range svalues { values[i] = Value(s) } + ret.Values = values - return Path{ - Values: values, - } + return ret } func (v Value) IsValid() bool { @@ -45,12 +53,16 @@ func (v Value) Err() error { } func (p Path) StringValues() []string { - values := make([]string, len(p.Values)) - for i, v := range p.Values { - values[i] = string(v) + ret := []string{} + if p.IsAbs { + ret = append(ret, "/") } - return values + for _, v := range p.Values { + ret = append(ret, string(v)) + } + + return ret } func (p Path) Real() string { diff --git a/src/tool/mk/main.go b/src/tool/mk/main.go index 09d5b49..c832627 100644 --- a/src/tool/mk/main.go +++ b/src/tool/mk/main.go @@ -331,7 +331,6 @@ func Run(args []string) { mkincdir == "" { homeDir, _ := os.UserHomeDir() os.Setenv("MKINCDIR", homeDir + "/app/goblin/mk/inc" ) - fmt.Println("shit:", os.Getenv("MKINCDIR")) } flags := flag.NewFlagSet(arg0, flag.ExitOnError) diff --git a/src/tool/mk/parse.go b/src/tool/mk/parse.go index b44dbfc..c859ed2 100644 --- a/src/tool/mk/parse.go +++ b/src/tool/mk/parse.go @@ -163,7 +163,11 @@ func parseRedirInclude(p *parser, t token) parserStateFun { filename := "" //fmt.Printf("'%v'\n", 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()) if err != nil {