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
7 changes: 6 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ asctl status <service-name>
```bash
asctl show <service-name>
```

* #### Show Service Definition File:
```bash
asctl cat <service-name>
```
Replace `<service-name>` with the name of your service.

## Logging
Expand Down Expand Up @@ -116,4 +121,4 @@ ascend --log-level=trace

#### 4. Ascend control commands:

In another terminal u can run the `asctl` commands to check the running service.
In another terminal u can run the `asctl` commands to check the running service.
23 changes: 23 additions & 0 deletions src/ascend/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,29 @@ local methodHandlers = {
return
end

respond({
success = true,
data = result
})
end,
cat = function(request, respond)
if not check_params(request.params, check_is_array_of_strings, respond) then
return
end

if not check_params(request.params, check_manages_just_managed_services, respond) then
return
end

local result, err = services.cat(request.params)
if not result then
respond(nil, {
code = jsonrpc.error_codes.INTERNAL_ERROR,
message = err or "unknown error"
})
return
end

respond({
success = true,
data = result
Expand Down
32 changes: 32 additions & 0 deletions src/ascend/services.lua
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,38 @@ function services.show(names)
return result
end

---@param names string[]
---@return table<string, { source: string, content: string }>?, string?
function services.cat(names)
local result = {}
for _, name in ipairs(names) do
local service_name = name_to_service_module(name)
local service = managed_services[service_name]
if not service then
local msg = string.interpolate("service ${name} not found", { name = service_name })
return nil, msg
end

local source = service.source
if type(source) ~= "string" then
local msg = string.interpolate("service ${name} source not found", { name = service_name })
return nil, msg
end

local content, err = fs.read_file(source)
if not content then
local msg = string.interpolate("failed to read ${source}: ${error}", { source = source, error = err })
return nil, msg
end

result[service_name] = {
source = source,
content = content
}
end
return result
end

---@class StartOptions
---@field manual boolean?
---@field is_boot boolean?
Expand Down
42 changes: 41 additions & 1 deletion src/asctl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,54 @@ local commands = {
end
print(format.status(response.data))
end,
show = function(parameters, options)
show = function(parameters, _)
local response, err = client.execute("show", parameters)
if not response then
log_error(err --[[ @as string ]])
os.exit(1)
end
print(format.show(response.data))
end,
cat = function(parameters, _)
local response, err = client.execute("cat", parameters)
if not response then
log_error(err --[[ @as string ]])
os.exit(1)
end

local output = response.data
local printed = false
local function emit(service_name)
local entry = output[service_name]
if not entry then
return
end
if printed then
io.write("\n")
end
if type(entry.source) == "string" then
print("# " .. entry.source)
end
if type(entry.content) == "string" then
io.write(entry.content)
if entry.content:sub(-1) ~= "\n" then
io.write("\n")
end
end
printed = true
end

if type(parameters) == "table" and #parameters > 0 then
for _, name in ipairs(parameters) do
local service_name = name:match("^(.+):") or name
emit(service_name)
end
else
for service_name, _ in pairs(output) do
emit(service_name)
end
end
end,
logs = function(parameters, options)
local response, err = client.execute("logs", parameters)
if not response then
Expand Down
42 changes: 41 additions & 1 deletion tests/asctl.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local test = TEST or require "u-test"
local new_test_env = require "common.test-env"

DISABLE_CLEANUP = false --- disable to see the tmp directory

test["asctl - list"] = function()
---@type AscendTestEnvOptions
local options = {
Expand Down Expand Up @@ -360,7 +362,7 @@ test["asctl - reload"] = function()
test.assert(result, err)
end

test["asctl - ascend-healh"] = function()
test["asctl - ascend-health"] = function()
---@type AscendTestEnvOptions
local options = {
services = {
Expand Down Expand Up @@ -479,6 +481,44 @@ test["asctl - show"] = function()
test.assert(result, err)
end

test["asctl - cat"] = function()
---@type AscendTestEnvOptions
local options = {
services = {
["date"] = {
source_path = "assets/services/simple-date.hjson",
}
},
assets = {
["scripts/date.lua"] = "assets/scripts/date.lua"
}
}

local result, err = new_test_env(options):run(function(env, ascendOutput)
local startTime = os.time()
while true do -- wait for service started
local line = ascendOutput:read("l", 2)
if line and line:match("date:default started") then
break
end
if os.time() > startTime + 10 then
return false, "Service did not start in time"
end
end

local ok, outputOrError = env:asctl({ "cat", "date" })
if not ok then
return false, outputOrError
end

local output = outputOrError
return output:match("# .+date%.hjson") and
output:match("executable") and
output:match("date%.lua")
end):result()
test.assert(result, err)
end

test["asctl - logs"] = function()
---@type AscendTestEnvOptions
local options = {
Expand Down