From afa0e736bd27f80a199b804e57487be27b83a3d0 Mon Sep 17 00:00:00 2001 From: surdeus Date: Wed, 8 Mar 2023 16:39:50 +0300 Subject: [PATCH] ln implemented. --- app/mk/inc/std/install | 1 + src/cmd/goblin/main.go | 103 +++++++++++++++++++++-------------------- src/pathx/main.go | 27 +++++++---- src/tool/ln/main.go | 35 ++++++++++++++ src/tool/mk/main.go | 16 +++---- src/tool/mk/parse.go | 5 +- 6 files changed, 120 insertions(+), 67 deletions(-) create mode 100644 src/tool/ln/main.go diff --git a/app/mk/inc/std/install b/app/mk/inc/std/install index 6d90ca4..40c1946 100644 --- a/app/mk/inc/std/install +++ b/app/mk/inc/std/install @@ -7,6 +7,7 @@ install-sh:VQ: build fi if test -d app ; then echo Installing application files... + echo "'$APPDIR'" mkdir -p $APPDIR/$PKG_NAME && cp -rf app/* $APPDIR/$PKG_NAME/ echo Done installing application files fi diff --git a/src/cmd/goblin/main.go b/src/cmd/goblin/main.go index 58ea3af..9071aeb 100644 --- a/src/cmd/goblin/main.go +++ b/src/cmd/goblin/main.go @@ -1,66 +1,67 @@ package main -import( - mtool "github.com/surdeus/gomtool/src/multitool" +import ( + "github.com/surdeus/goblin/src/tool/awk" + "github.com/surdeus/goblin/src/tool/basename" "github.com/surdeus/goblin/src/tool/cat" + "github.com/surdeus/goblin/src/tool/date" + "github.com/surdeus/goblin/src/tool/ec" "github.com/surdeus/goblin/src/tool/echo" - "github.com/surdeus/goblin/src/tool/mkdir" - "github.com/surdeus/goblin/src/tool/gtrue" + "github.com/surdeus/goblin/src/tool/ftest" "github.com/surdeus/goblin/src/tool/gfalse" + "github.com/surdeus/goblin/src/tool/grange" + "github.com/surdeus/goblin/src/tool/gtrue" + "github.com/surdeus/goblin/src/tool/in" + "github.com/surdeus/goblin/src/tool/ln" + "github.com/surdeus/goblin/src/tool/ls" + "github.com/surdeus/goblin/src/tool/mergelbl" + "github.com/surdeus/goblin/src/tool/mk" + "github.com/surdeus/goblin/src/tool/mkdir" + "github.com/surdeus/goblin/src/tool/noext" + "github.com/surdeus/goblin/src/tool/path" + "github.com/surdeus/goblin/src/tool/paths" + "github.com/surdeus/goblin/src/tool/quote" + "github.com/surdeus/goblin/src/tool/read" "github.com/surdeus/goblin/src/tool/sort" "github.com/surdeus/goblin/src/tool/tac" - "github.com/surdeus/goblin/src/tool/ls" - "github.com/surdeus/goblin/src/tool/yes" - "github.com/surdeus/goblin/src/tool/date" "github.com/surdeus/goblin/src/tool/uniq" - "github.com/surdeus/goblin/src/tool/quote" "github.com/surdeus/goblin/src/tool/urlprs" - "github.com/surdeus/goblin/src/tool/noext" - "github.com/surdeus/goblin/src/tool/mergelbl" - "github.com/surdeus/goblin/src/tool/basename" - "github.com/surdeus/goblin/src/tool/ec" - "github.com/surdeus/goblin/src/tool/read" - "github.com/surdeus/goblin/src/tool/wc" - "github.com/surdeus/goblin/src/tool/ftest" - "github.com/surdeus/goblin/src/tool/grange" - "github.com/surdeus/goblin/src/tool/in" "github.com/surdeus/goblin/src/tool/useprog" - "github.com/surdeus/goblin/src/tool/path" - "github.com/surdeus/goblin/src/tool/mk" - "github.com/surdeus/goblin/src/tool/awk" - "github.com/surdeus/goblin/src/tool/paths" + "github.com/surdeus/goblin/src/tool/wc" "github.com/surdeus/goblin/src/tool/whoami" + "github.com/surdeus/goblin/src/tool/yes" + mtool "github.com/surdeus/gomtool/src/multitool" ) func main() { - tools := mtool.Tools { - "basename" : mtool.Tool{basename.Run, "get base name of file path"}, - "cat": mtool.Tool{cat.Run, "print file data to the standard output"}, - "mkdir" : mtool.Tool{mkdir.Run, "make new directory"}, - "echo" : mtool.Tool{echo.Run, "print strings to the standard output"}, - "true" : mtool.Tool{gtrue.Run, "exit with true status"}, - "false" : mtool.Tool{gfalse.Run, "exit with false status"}, - "sort" : mtool.Tool{sort.Run, "sort strings inputed from standard input"}, - "tac" : mtool.Tool{tac.Run, "print strings from standard input in reversed order"}, - "ls" :mtool.Tool{ ls.Run, "list directory content"}, - "yes" : mtool.Tool{ yes.Run, "print string infinite/exact amount times"}, - "date" : mtool.Tool{date.Run, "print current date"}, - "uniq" : mtool.Tool{uniq.Run, "filter repeated strings"}, - "quote" : mtool.Tool{quote.Run, "quote words containing space characters"}, - "urlprs" : mtool.Tool{urlprs.Run, "parse URLs"}, - "noext" : mtool.Tool{noext.Run, "print file path without extension"}, - "mergelbl" : mtool.Tool{mergelbl.Run, "merge line by line"}, - "ec" : mtool.Tool{ec.Run, "render escape sequences"}, - "read" : mtool.Tool{read.Run, "read lines and exit"}, - "wc" : mtool.Tool{wc.Run, "count words, bytes, runes etc"}, - "ftest" : mtool.Tool{ftest.Run, "filter files by specified features"}, - "range" : mtool.Tool{grange.Run, "too lazy"}, - "in" : mtool.Tool{in.Run, "filter strings from stdin that aren not in arguments"}, - "useprog" : mtool.Tool{useprog.Run, "print the name of the first existing program in arg list"}, - "path" : mtool.Tool{path.Run, "print cross platform path based on cmd arguments"}, - "mk" : mtool.Tool{mk.Run, "file dependency system, simpler make"}, - "awk" : mtool.Tool{awk.Run, "simple scripting language for working with string templates"}, - "paths" : mtool.Tool{ + tools := mtool.Tools{ + "basename": mtool.Tool{basename.Run, "get base name of file path"}, + "cat": mtool.Tool{cat.Run, "print file data to the standard output"}, + "mkdir": mtool.Tool{mkdir.Run, "make new directory"}, + "echo": mtool.Tool{echo.Run, "print strings to the standard output"}, + "true": mtool.Tool{gtrue.Run, "exit with true status"}, + "false": mtool.Tool{gfalse.Run, "exit with false status"}, + "sort": mtool.Tool{sort.Run, "sort strings inputed from standard input"}, + "tac": mtool.Tool{tac.Run, "print strings from standard input in reversed order"}, + "ls": mtool.Tool{ls.Run, "list directory content"}, + "yes": mtool.Tool{yes.Run, "print string infinite/exact amount times"}, + "date": mtool.Tool{date.Run, "print current date"}, + "uniq": mtool.Tool{uniq.Run, "filter repeated strings"}, + "quote": mtool.Tool{quote.Run, "quote words containing space characters"}, + "urlprs": mtool.Tool{urlprs.Run, "parse URLs"}, + "noext": mtool.Tool{noext.Run, "print file path without extension"}, + "mergelbl": mtool.Tool{mergelbl.Run, "merge line by line"}, + "ec": mtool.Tool{ec.Run, "render escape sequences"}, + "read": mtool.Tool{read.Run, "read lines and exit"}, + "wc": mtool.Tool{wc.Run, "count words, bytes, runes etc"}, + "ftest": mtool.Tool{ftest.Run, "filter files by specified features"}, + "range": mtool.Tool{grange.Run, "too lazy"}, + "in": mtool.Tool{in.Run, "filter strings from stdin that aren not in arguments"}, + "useprog": mtool.Tool{useprog.Run, "print the name of the first existing program in arg list"}, + "path": mtool.Tool{path.Run, "print cross platform path based on cmd arguments"}, + "mk": mtool.Tool{mk.Run, "file dependency system, simpler make"}, + "awk": mtool.Tool{awk.Run, "simple scripting language for working with string templates"}, + "paths": mtool.Tool{ paths.Run, "convert UNIX slash separated paths into the OS compatible ones", }, @@ -68,6 +69,10 @@ func main() { whoami.Run, "print current user name", }, + "ln": mtool.Tool{ + ln.Run, + "link files", + }, } mtool.Main("goblin", tools) diff --git a/src/pathx/main.go b/src/pathx/main.go index ba50d53..ddb0261 100644 --- a/src/pathx/main.go +++ b/src/pathx/main.go @@ -4,15 +4,15 @@ package pathx // paths. import ( - fp "path/filepath" "path" + fp "path/filepath" "strings" ) type Value string type Path struct { Values []Value - IsAbs bool + IsAbs bool } func (p Path) Append(values ...string) Path { @@ -28,10 +28,10 @@ func From(p string) Path { return ret } - p = path.Clean(p) - if p[0] == '/' { - ret.IsAbs = true - } + /* p = path.Clean(p) + if p[0] == '/' { + ret.IsAbs = true + } */ p, _ = strings.CutSuffix(p, "/") svalues := strings.Split(p, "/") @@ -44,6 +44,10 @@ func From(p string) Path { return ret } +func FromReal(p string) Path { + return From(fp.ToSlash(p)) +} + func (v Value) IsValid() bool { return v.Err() == nil } @@ -66,11 +70,17 @@ func (p Path) StringValues() []string { } func (p Path) Real() string { - return fp.Join(p.StringValues()...) + return strings.Join(p.StringValues(), string(fp.Separator)) } func (p Path) String() string { - return path.Join(p.StringValues()...) + ret := "" + if p.IsAbs { + ret = "/" + } + + ret += path.Join(p.StringValues()...) + return ret } func (p Path) IsValid() bool { @@ -92,4 +102,3 @@ func (p Path) Err() error { return nil } - diff --git a/src/tool/ln/main.go b/src/tool/ln/main.go new file mode 100644 index 0000000..662b2c4 --- /dev/null +++ b/src/tool/ln/main.go @@ -0,0 +1,35 @@ +package ln + +import ( + "flag" + "fmt" + "os" +) + +func Run(args []string) { + var lflag bool + flagSet := flag.NewFlagSet(args[0], flag.ExitOnError) + flagSet.BoolVar(&lflag, "s", false, "make a symbolic link, not a hard one") + + flagSet.Parse(args[1:]) + args = flagSet.Args() + + if len(args) != 2 { + flagSet.Usage() + os.Exit(1) + } + + src := args[0] + dst := args[1] + + var err error + if lflag { + err = os.Symlink(src, dst) + } else { + err = os.Link(src, dst) + } + + if err != nil { + fmt.Fprintln(os.Stderr, err) + } +} diff --git a/src/tool/mk/main.go b/src/tool/mk/main.go index c832627..596b93c 100644 --- a/src/tool/mk/main.go +++ b/src/tool/mk/main.go @@ -9,6 +9,7 @@ import ( "path/filepath" "strings" "sync" + "github.com/surdeus/goblin/src/pathx" ) @@ -93,7 +94,6 @@ const ( ) // Build a node's prereqs. Block until completed. -// func mkNodePrereqs(g *graph, u *node, e *edge, prereqs []*node, dryrun bool, required bool) nodeStatus { prereqstat := make(chan nodeStatus) @@ -131,11 +131,11 @@ func mkNodePrereqs(g *graph, u *node, e *edge, prereqs []*node, dryrun bool, // concurrently. // // Args: -// g: Graph in which the node lives. -// u: Node to (possibly) build. -// dryrun: Don't actually build anything, just pretend. -// required: Avoid building this node, unless its prereqs are out of date. // +// g: Graph in which the node lives. +// u: Node to (possibly) build. +// dryrun: Don't actually build anything, just pretend. +// required: Avoid building this node, unless its prereqs are out of date. func mkNode(g *graph, u *node, dryrun bool, required bool) { // try to claim on this node u.mutex.Lock() @@ -327,10 +327,10 @@ func Run(args []string) { arg0 := args[0] args = args[1:] - if mkincdir := os.Getenv("MKINCDIR") ; - mkincdir == "" { + if mkincdir := os.Getenv("MKINCDIR"); mkincdir == "" { homeDir, _ := os.UserHomeDir() - os.Setenv("MKINCDIR", homeDir + "/app/goblin/mk/inc" ) + homeDir = pathx.FromReal(homeDir).String() + os.Setenv("MKINCDIR", homeDir+"/app/goblin/mk/inc") } flags := flag.NewFlagSet(arg0, flag.ExitOnError) diff --git a/src/tool/mk/parse.go b/src/tool/mk/parse.go index c859ed2..552da6a 100644 --- a/src/tool/mk/parse.go +++ b/src/tool/mk/parse.go @@ -10,6 +10,7 @@ import ( "path/filepath" "regexp" "strings" + "github.com/surdeus/goblin/src/pathx" ) @@ -169,9 +170,11 @@ func parseRedirInclude(p *parser, t token) parserStateFun { true, )[0] } - file, err := os.Open(pathx.From(filename).Real()) + pths := pathx.From(filename) + file, err := os.Open(pths.Real()) if err != nil { p.basicWarnAtToken(fmt.Sprintf("cannot open %s", filename), p.tokenbuf[0]) + //fmt.Printf("%q %q %q\n", pths.Values, pths.Real(), pths.String()) //p.basicErrorAtToken(fmt.Sprintf("cannot open %s", filename), p.tokenbuf[0]) } input, _ := io.ReadAll(file)