Skip to content

Commit 94f1581

Browse files
committed
add log
fix config add verbose flag
1 parent 5923695 commit 94f1581

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

scm.lua

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ scm.config = {
1919
["libraryDirectory"] = "libs/",
2020
["configDirectory"] = "config/",
2121
["configFile"] = "scm-config.json",
22-
["scriptFile"] = "scm-scripts.json" -- will be saved in configDirectory as well
22+
["scriptFile"] = "scm-scripts.json", -- will be saved in configDirectory as well
23+
["verbose"] = true,
24+
["printPrefix"] = "[scm] ",
25+
["logDate"] = false,
26+
["writeLogFile"] = false,
27+
["logFilePath"] = "logs/scm-log.txt"
2328
}
2429
----------------
2530

@@ -106,13 +111,13 @@ $ list
106111
if args[3] then
107112
scm:updateConfig(args[2], args[3])
108113
elseif args[2] then
109-
if scm.config[args[2]] then
110-
print (args[2], tostring(scm.config[args[2]]))
114+
if scm.config[args[2]] ~= nil then
115+
print(args[2], tostring(scm.config[args[2]]))
111116
end
112117
else
113118
print ("You can currently configure the following variables:")
114119
for cname, cvalue in pairs(scm.config) do
115-
print (cname, tostring(cvalue))
120+
textutils.pagedPrint(cname .. "\t" .. tostring(cvalue))
116121
end
117122
end
118123
end,
@@ -147,6 +152,19 @@ $ help <name>
147152
}
148153
}
149154

155+
---@param message string
156+
function scm:log (message)
157+
local datetime = ""
158+
if self.config["logDate"] then datetime = os.date("[%Y-%m-%d %H:%M:%S] ") end
159+
if self.config["verbose"] then print (self.config["printPrefix"] .. message) end
160+
161+
if self.config["writeLogFile"] then
162+
local file = fs.open(self.config["logFilePath"], "a")
163+
file.write (datetime .. message .. "\n")
164+
file.close()
165+
end
166+
end
167+
150168
---@param str string
151169
---@return string | nil
152170
---@return string | nil
@@ -167,6 +185,7 @@ end
167185
---@param updateObj table | nil
168186
---@return boolean
169187
function scm:download (target, fileType, updateObj)
188+
scm:log("Downloading " .. fileType .. " " .. target .. "...")
170189
if target == nil then
171190
--@TODO: Error handling
172191
return false
@@ -362,6 +381,7 @@ end
362381
---@return boolean
363382
function scm:addScript (sourceObject, success)
364383
if not success or not sourceObject then return false end
384+
scm:log("Adding script " .. sourceObject.name .. "...")
365385
local scriptExists = false
366386

367387
-- Check if script already exists, then update
@@ -378,7 +398,10 @@ function scm:addScript (sourceObject, success)
378398
end
379399

380400
if not scriptExists then
401+
scm:log("Script added: " .. sourceObject.name)
381402
table.insert(self.scripts, sourceObject)
403+
else
404+
scm:log("Script already exists.")
382405
end
383406

384407
self:saveScripts()
@@ -413,6 +436,7 @@ end
413436

414437
---@param name string
415438
function scm:removeScript (name, keepScriptConfig)
439+
scm:log("Removing script: " .. name)
416440
local o = {}
417441
local scriptType = nil
418442

@@ -488,6 +512,7 @@ function scm:updateAllScripts ()
488512
end
489513

490514
function scm:updateSCM ()
515+
scm:log("Updating scm...")
491516
shell.run("pastebin", "run", self.config.installScript)
492517
end
493518

@@ -547,22 +572,20 @@ function scm:updateConfig (name, value)
547572
self:saveConfig()
548573
end
549574
else
550-
print ("You can currently configure the following variables:")
575+
scm:log("You can currently configure the following variables:")
551576
for cname, cvalue in pairs(self.config) do
552-
print (cname, tostring(cvalue))
577+
scm:log(cname, tostring(cvalue))
553578
end
554579
end
555580
end
556581

557582
---@param name string
558583
function scm:checkRequirements(name)
559-
print ("Checking requirements of " .. name .. "...")
584+
scm:log("Checking requirements of " .. name .. "...")
560585
local file
561586
if fs.exists("./" .. self.config["libraryDirectory"] .. name .. self.config["librarySuffix"] .. "/" .. name .. ".lua") then
562-
print("1","./" .. self.config["libraryDirectory"] .. name)
563587
file = fs.open("./" .. self.config["libraryDirectory"] .. name .. self.config["librarySuffix"] .. "/" .. name .. ".lua", "r")
564588
else
565-
print("2", "./" .. self.config["libraryDirectory"] .. name)
566589
file = fs.open("./" .. self.config["libraryDirectory"] .. name .. ".lua", "r")
567590
end
568591

@@ -592,7 +615,7 @@ function scm:checkRequirements(name)
592615
end
593616
if scriptExists then
594617
-- requirement already satisfied!
595-
print ("Requirement already satisfied! (" .. scriptName .. ")")
618+
scm:log("Requirement already satisfied! (" .. scriptName .. ")")
596619
else
597620
requires[#requires + 1] = scriptName
598621
end
@@ -603,7 +626,7 @@ function scm:checkRequirements(name)
603626
-- Install missing requirements
604627
for i = 1, #requires do
605628
local n = requires[i]
606-
print("Trying to install " .. n .. "...")
629+
scm:log("Trying to install " .. n .. "...")
607630
self:download(n, "library")
608631
local tmpName, tmpCode = self:splitNameCode(n)
609632
if tmpCode then n = tmpName end
@@ -614,6 +637,7 @@ end
614637
---@param name string
615638
---@return any
616639
function scm:load(name)
640+
scm:log("Loading " .. name .. "...")
617641
local scriptExists = false
618642
for i = 1, #self.scripts, 1 do
619643
if self.scripts[i].name == name then
@@ -636,11 +660,11 @@ function scm:load(name)
636660
self:checkRequirements(name)
637661
local path = "./" .. self.config["libraryDirectory"] .. name
638662
local script = require(path)
663+
scm:log("Done")
639664
return script
640-
else
641-
-- error installing failed
642665
end
643-
666+
667+
scm:log("Done")
644668
return nil
645669
end
646670

0 commit comments

Comments
 (0)