Skip to content

Commit 9b01d82

Browse files
committed
add config <name>
fix update script
1 parent 7932750 commit 9b01d82

File tree

2 files changed

+56
-28
lines changed

2 files changed

+56
-28
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ By default, without parameters, `update` updates the SCM script.
3838
Shows all installed scripts.
3939
## config
4040
Shows all available configurations.
41+
- `<name>`: Shows the value of a specific configuration.
4142
- `<name> <value>`: Updates the value of a specific configuration.
4243
## help
4344
Shows all available commands and their description.

scm.lua

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ $ add <name>@<pastebinCode>
6565
end,
6666
description = [[
6767
$ update
68-
Updates this program (SCM)
68+
Updates this program (SCM)
6969
$ update <name>
70-
Updates the script with the given name
70+
Updates the script with the given name
7171
$ update all
72-
Updates all installed programs and libraries
72+
Updates all installed programs and libraries
7373
$ update <name> <srcName>
74-
Updates the script with an specific source
74+
Updates the script with an specific source
7575
]]
7676
},
7777
["remove"] = {
@@ -85,9 +85,9 @@ Updates the script with an specific source
8585
end,
8686
description = [[
8787
$ remove <name>
88-
Removes the given script
88+
Removes the given script
8989
$ remove all
90-
Removes all scripts
90+
Removes all scripts
9191
]]
9292
},
9393
["list"] = {
@@ -97,36 +97,52 @@ Removes all scripts
9797
end,
9898
description = [[
9999
$ list
100-
Lists all installed scripts
100+
Lists all installed scripts
101101
]]
102102
},
103103
["config"] = {
104104
---@param args table
105105
func = function (args)
106-
scm:updateConfig(args[2], args[3])
106+
if args[3] then
107+
scm:updateConfig(args[2], args[3])
108+
elseif args[2] then
109+
if scm.config[args[2]] then
110+
print (args[2], tostring(scm.config[args[2]]))
111+
end
112+
else
113+
print ("You can currently configure the following variables:")
114+
for cname, cvalue in pairs(scm.config) do
115+
print (cname, tostring(cvalue))
116+
end
117+
end
107118
end,
108119
description = [[
109120
$ config
110-
Lists all available configurations
121+
Lists all available configurations
122+
$ config <name>
123+
Shows a specific configuration
111124
$ config <name> <value>
112-
Updates the configuration
125+
Updates the configuration
113126
]]
114127
},
115128
["help"] = {
116129
---@param args table
117130
func = function (args)
118131
if args[2] then
119-
textutils.pagedPrint(args[2] .. "\n" .. scm.commands[args[2]]["description"])
120-
end
121-
for k, v in pairs(scm.commands) do
122-
textutils.pagedPrint("# " .. k .. "\n" .. v.description)
132+
if scm.commands[args[2]] then
133+
textutils.pagedPrint(args[2] .. "\n" .. scm.commands[args[2]]["description"])
134+
end
135+
else
136+
for k, v in pairs(scm.commands) do
137+
textutils.pagedPrint("# " .. k .. "\n" .. v.description)
138+
end
123139
end
124140
end,
125141
description = [[
126142
$ help
127-
Shows all available commands and their description
143+
Shows all available commands and their description
128144
$ help <name>
129-
Shows the description of the given command
145+
Shows the description of the given command
130146
]]
131147
}
132148
}
@@ -282,6 +298,10 @@ function scm:downloadPastebin (sourceObject, code, targetDirectory, updateObj)
282298
end
283299
end
284300

301+
if updateObj then
302+
fs.delete(sourceObject.name)
303+
end
304+
285305
if sourceObject.type == "program" then
286306
shell.run("pastebin", "get", code, sourceObject.name)
287307
else
@@ -342,10 +362,12 @@ end
342362
---@return boolean
343363
function scm:addScript (sourceObject, success)
344364
if not success or not sourceObject then return false end
365+
local scriptExists = false
345366

346367
-- Check if script already exists, then update
347368
for i = 1, #self.scripts, 1 do
348369
if self.scripts[i].name == sourceObject.name and self.scripts[i].type == sourceObject.type then
370+
scriptExists = true
349371
if self.scripts[i].source[sourceObject.sourceName] then
350372
self.scripts[i].source[sourceObject.sourceName] = sourceObject.source[sourceObject.sourceName]
351373
self:saveScripts()
@@ -355,7 +377,10 @@ function scm:addScript (sourceObject, success)
355377
end
356378
end
357379

358-
table.insert(self.scripts, sourceObject)
380+
if not scriptExists then
381+
table.insert(self.scripts, sourceObject)
382+
end
383+
359384
self:saveScripts()
360385

361386
return true
@@ -387,20 +412,22 @@ function scm:listScripts ()
387412
end
388413

389414
---@param name string
390-
function scm:removeScript (name)
415+
function scm:removeScript (name, keepScriptConfig)
391416
local o = {}
392417
local scriptType = nil
393418

394-
for i = 1, #self.scripts, 1 do
395-
if self.scripts[i].name ~= name then
396-
table.insert(o, self.scripts[i])
397-
else
398-
scriptType = self.scripts[i].type
419+
if keepScriptConfig ~= true then
420+
for i = 1, #self.scripts, 1 do
421+
if self.scripts[i].name ~= name then
422+
table.insert(o, self.scripts[i])
423+
else
424+
scriptType = self.scripts[i].type
425+
end
399426
end
400-
end
401427

402-
self.scripts = o
403-
self:saveScripts()
428+
self.scripts = o
429+
self:saveScripts()
430+
end
404431

405432
if scriptType and fs.exists(self.config[scriptType .. "Directory"] .. name .. self.config[scriptType .. "Suffix"]) then
406433
fs.delete(self.config[scriptType .. "Directory"] .. name .. self.config[scriptType .. "Suffix"])
@@ -446,8 +473,8 @@ function scm:updateScript (name, sourceName)
446473
end
447474

448475
if updateObj.source[sourceName] and updateObj.type then
449-
self:removeScript(name)
450-
self:download(updateObj.source[sourceName], updateObj.type)
476+
self:removeScript(name, true)
477+
self:download(updateObj.source[sourceName], updateObj.type, updateObj)
451478
return true
452479
end
453480

0 commit comments

Comments
 (0)