commit 471da19d4174da00f74e4fd54b2121134684d4cf
parent b1d78ee89e45ac863a585fc6b287dbe8b1662135
Author: Hugo Soucy <hugo@soucy.cc>
Date: Mon, 5 Dec 2022 08:05:59 -0500
Add a reset command from a new reset function
Diffstat:
2 files changed, 64 insertions(+), 16 deletions(-)
diff --git a/satelito/dirtree.lua b/satelito/dirtree.lua
@@ -2,6 +2,7 @@
local dirtree = {}
--
local lfs = require 'lfs' -- luafilesystem
+local inspect = require 'inspect'
function dirtree.get(dir)
assert(dir and dir ~= '', 'directory parameter is missing or empty')
@@ -31,4 +32,38 @@ function dirtree.get(dir)
return coroutine.wrap(function() yieldtree(dir) end)
end
+-- Reset the public_html/ directory
+-- Recursive function
+function dirtree.reset(dir)
+ local publicdir = _G.Satelito.publicdir
+ local _dir = dir
+
+ -- Removes slash if is one
+ if string.sub(dir, -1) == '/' then
+ _dir = string.sub(_dir, 1, -2)
+ end
+
+ for entry in lfs.dir(_dir) do
+ local type
+ if entry ~= '.'
+ and entry ~= '..'
+ and entry ~= '.gitignore'
+ then
+ entry = _dir..'/'..entry
+ type = lfs.attributes(entry).mode
+
+ if type == 'file' then
+ os.remove(entry)
+ print('❌ '.. entry)
+ elseif type == 'directory' then
+ dirtree.reset(entry)
+ end
+ end
+ end
+
+ if dir ~= publicdir then
+ lfs.rmdir(dir)
+ end
+end
+
return dirtree
diff --git a/satelito/init.lua b/satelito/init.lua
@@ -14,7 +14,7 @@ local init
local pipe
local make
local exec
-local purge
+local reset
local parser = argparse()
:name 'satelito'
@@ -33,13 +33,13 @@ make:flag('-e --export', 'Export the outputed HTML in the *config.paths.public_h
-- Set the exec command
exec = parser:command('exec', 'Execute a script from the bin directory.')
exec:argument 'bin name'
--- Set the purge command
-purge = parser:command('purge', 'Delete everything in the public_html directory.')
+-- Set the reset command
+reset = parser:command('reset', 'Delete everything in the public_html directory.')
args = parser:parse()
-- Started
-print('=> Satelito is here ...')
+print('=> ☾ Satelito is here ☽ ...')
-------------
-- GLOBALS --
@@ -48,6 +48,7 @@ print('=> Satelito is here ...')
-- Set the Satelito global table
_G.Satelito = {}
_G.Satelito.timestart = os.time()
+_G.Satelito.args = args
-- Put config.lua in a table
print('=> Fetching the configuration file content ...')
@@ -67,11 +68,10 @@ _G.Satelito.contentdir = lfs.currentdir()..'/'.._G.Satelito.config.paths.content
_G.Satelito.publicdir = lfs.currentdir()..'/'.._G.Satelito.config.paths.public_html
-- Get the list of templates
-print('=> Fetching the templates ...')
-_G.Satelito.templates = lume.array(dirtree.get(lfs.currentdir() .. '/' .. _G.Satelito.config.paths.templates))
-
--- Get the arguments
-_G.Satelito.args = args
+if args['make'] or args['pipe'] then
+ print('=> Fetching the templates ...')
+ _G.Satelito.templates = lume.array(dirtree.get(lfs.currentdir() .. '/' .. _G.Satelito.config.paths.templates))
+end
--print(inspect(_G.Satelito))
@@ -88,6 +88,7 @@ if args['exec'] then
dofile(lfs.currentdir() .. '/bin/'..args['bin name'])
end
+ return
end
----------
@@ -171,17 +172,29 @@ if args['make'] then
end
-----------
--- PURGE --
+-- RESET --
-----------
-if args['purge'] then
+if args['reset'] then
local publicdir = _G.Satelito.publicdir
+ local yes_or_no
- for filepath in dirtree.get(publicdir) do
- if file.get_basename(filepath) ~= '.gitignore' then
- os.execute('rm -Rv '..filepath)
- end
+ repeat
+ print('=> Reset ...')
+ print('=> Are you sure you want to delete the contents of the "'..publicdir..'" directory?')
+ print('=> Please answer with yes or no ...')
+ io.flush()
+
+ yes_or_no = string.lower(tostring(io.read()))
+ until yes_or_no == 'yes' or yes_or_no == 'no'
+
+ if yes_or_no == 'yes' then
+ dirtree.reset(publicdir)
+
+ print('=> Done')
+ else
+ print('=> Aborted')
end
- print(os.clock())
+ return
end