From 453ba9f8ad018f5289ea64144c97a0233021ae67 Mon Sep 17 00:00:00 2001 From: TorchedSammy <38820196+TorchedSammy@users.noreply.github.com> Date: Mon, 20 Jun 2022 16:47:16 -0400 Subject: [PATCH] feat(fs): add some functions and properties (closes #168) --- CHANGELOG.md | 7 ++++++ golibs/fs/fs.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2d28d7..d775885 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,13 @@ includes git commit, branch, and (new!!) release name. foreground and backgrounds a job respectively. - Friendlier functions to the `hilbish.runner` interface, which also allow having and using multiple runners. +- A few new functions to the `fs` module: + - `fs.basename(path)` gets the basename of path + - `fs.dir(path)` gets the directory part of path + - `fs.glob(pattern)` globs files and directories based on patterns +- .. and 2 properties + - `fs.pathSep` is the separator for filesystem paths and directories + - `fs.pathListSep` is the separator for $PATH env entries ### Changed - **Breaking Change:** Upgraded to Lua 5.4. diff --git a/golibs/fs/fs.go b/golibs/fs/fs.go index 950e966..88a04c5 100644 --- a/golibs/fs/fs.go +++ b/golibs/fs/fs.go @@ -24,9 +24,14 @@ func loaderFunc(rtm *rt.Runtime) (rt.Value, func()) { "stat": util.LuaExport{fstat, 1, false}, "readdir": util.LuaExport{freaddir, 1, false}, "abs": util.LuaExport{fabs, 1, false}, + "basename": util.LuaExport{fbasename, 1, false}, + "dir": util.LuaExport{fdir, 1, false}, + "glob": util.LuaExport{fglob, 1, false}, } mod := rt.NewTable() util.SetExports(rtm, mod, exports) + mod.Set(rt.StringValue("pathSep"), rt.StringValue(string(os.PathSeparator))) + mod.Set(rt.StringValue("pathListSep"), rt.StringValue(string(os.PathListSeparator))) util.Document(mod, `The fs module provides easy and simple access to filesystem functions and other things, and acts an @@ -155,3 +160,59 @@ func fabs(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { return c.PushingNext1(t.Runtime, rt.StringValue(abspath)), nil } + +// basename(path) +// Gives the basename of `path`. For the rules, +// see Go's filepath.Base +func fbasename(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { + if err := c.Check1Arg(); err != nil { + return nil, err + } + path, err := c.StringArg(0) + if err != nil { + return nil, err + } + + return c.PushingNext(t.Runtime, rt.StringValue(filepath.Base(path))), nil +} + +// dir(path) +// Returns the directory part of `path`. For the rules, see Go's +// filepath.Dir +func fdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { + if err := c.Check1Arg(); err != nil { + return nil, err + } + path, err := c.StringArg(0) + if err != nil { + return nil, err + } + + return c.PushingNext(t.Runtime, rt.StringValue(filepath.Dir(path))), nil +} + +// glob(pattern) +// Glob all files and directories that match the pattern. +// For the rules, see Go's filepath.Glob +func fglob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { + if err := c.Check1Arg(); err != nil { + return nil, err + } + pattern, err := c.StringArg(0) + if err != nil { + return nil, err + } + + matches, err := filepath.Glob(pattern) + if err != nil { + return nil, err + } + + luaMatches := rt.NewTable() + + for i, match := range matches { + luaMatches.Set(rt.IntValue(int64(i + 1)), rt.StringValue(match)) + } + + return c.PushingNext(t.Runtime, rt.TableValue(luaMatches)), nil +}