Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/am.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ am.options = initialize_options(get_default_options())

---@param cmd string|string[]|AmiCli
---@param args string[] | nil
---@return AmiCli, string[]
local function get_interface(cmd, args)
local interface = cmd
if util.is_array(cmd) then
Expand All @@ -51,7 +52,7 @@ local function get_interface(cmd, args)
local commands = table.get(am, { "__interface", "commands" }, {})
interface = commands[cmd] or interface
end
return interface, args
return interface --[[@as AmiCli]], args or {}
end

---#DES am.execute
Expand All @@ -66,6 +67,24 @@ function am.execute(cmd, args)
return cli.process(interface, args)
end

---#DES am.execute_action
---
---Executes action of specifed cmd with specified options, command and args
---@param cmd string|string[]|AmiCli
---@param options table<string, any>?
---@param command string?
---@param args string[]?
function am.execute_action(cmd, options, command, args)
local interface = get_interface(cmd)
ami_assert(type(interface) == "table", "no valid command provided", EXIT_CLI_CMD_UNKNOWN)
local action = interface.action
if type(action) ~= "function" then
ami_error("no valid action provided", EXIT_CLI_CMD_UNKNOWN)
return -- just to make linter happy
end
return action(options, command, args, interface)
end

---@type string[]
am.__args = {}

Expand Down Expand Up @@ -192,3 +211,11 @@ am.execute_extension = exec.native_action
---@param inject_args ExternalActionOptions?
---@return integer
am.execute_external = exec.external_action


---#DES am.unpack_app()
---
---Unpacks application from zip archive
---@diagnostic disable-next-line: undefined-doc-param
---@param source string
am.unpack_app = am.app.unpack
19 changes: 12 additions & 7 deletions src/ami.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ if parsed_options.path then
else
log_error("Option 'path' provided, but chdir not supported.")
log_info("HINT: Run ami without path parameter from path you supplied to 'path' option.")
os.exit(1)
return os.exit(1)
end
end

Expand Down Expand Up @@ -77,31 +77,36 @@ end
if parsed_options["base"] then
if type(parsed_options["base"]) ~= "string" then
log_error("Invalid base interface: " .. tostring(parsed_options["base"]))
os.exit(EXIT_INVALID_AMI_BASE_INTERFACE)
return os.exit(EXIT_INVALID_AMI_BASE_INTERFACE)
end
am.options.BASE_INTERFACE = parsed_options["base"] --[[@as string]]
end

local unpack_path = parsed_options["unpack"]
if type(unpack_path) == "string" and unpack_path ~= "" then
am.unpack_app(unpack_path)
return os.exit(0)
end

-- expose default options
if parsed_options.version then
print(am.VERSION)
os.exit(0)
return os.exit(0)
end

if parsed_options["is-app-installed"] then
local is_installed = am.app.is_installed()
print(is_installed)
os.exit(is_installed and 0 or EXIT_NOT_INSTALLED)
return os.exit(is_installed and 0 or EXIT_NOT_INSTALLED)
end
if parsed_options.about then
print(am.ABOUT)
os.exit(0)
return os.exit(0)
end
if parsed_options["erase-cache"] then
am.cache.erase()
log_success("Cache succesfully erased.")
os.exit(0)
return os.exit(0)
end

if parsed_options["dry-run"] then
Expand All @@ -114,7 +119,7 @@ if parsed_options["dry-run"] then
end
end
am.execute_extension(tostring(remaining_args[1].value), ...)
os.exit(0)
return os.exit(0)
end

am.__reload_interface(am.options.SHALLOW)
Expand Down
Loading
Loading