feat(cli): added commands for interacting with build logs

main
Jef Roosens 2022-05-07 21:50:20 +02:00
parent fa6603bd45
commit 5b016df85d
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
7 changed files with 145 additions and 11 deletions

View File

@ -18,15 +18,7 @@ pub fn new(address string, api_key string) Client {
}
}
// send_request<T> just calls send_request_with_body<T> with an empty body.
fn (c &Client) send_request<T>(method Method, url string, params map[string]string) ?Response<T> {
return c.send_request_with_body<T>(method, url, params, '')
}
// send_request_with_body<T> is a convenience method for sending requests to
// the repos API. It mostly does string manipulation to create a query string
// containing the provided params.
fn (c &Client) send_request_with_body<T>(method Method, url string, params map[string]string, body string) ?Response<T> {
fn (c &Client) send_request_raw(method Method, url string, params map[string]string, body string) ?http.Response {
mut full_url := '$c.address$url'
if params.len > 0 {
@ -46,7 +38,27 @@ fn (c &Client) send_request_with_body<T>(method Method, url string, params map[s
req.add_custom_header('X-Api-Key', c.api_key) ?
res := req.do() ?
data := json.decode(Response<T>, res.text) ?
return res
}
// send_request<T> just calls send_request_with_body<T> with an empty body.
fn (c &Client) send_request<T>(method Method, url string, params map[string]string) ?Response<T> {
return c.send_request_with_body<T>(method, url, params, '')
}
// send_request_with_body<T> is a convenience method for sending requests to
// the repos API. It mostly does string manipulation to create a query string
// containing the provided params.
fn (c &Client) send_request_with_body<T>(method Method, url string, params map[string]string, body string) ?Response<T> {
res_text := c.send_request_raw_response(method, url, params, body) ?
data := json.decode(Response<T>, res_text) ?
return data
}
fn (c &Client) send_request_raw_response(method Method, url string, params map[string]string, body string) ?string {
res := c.send_request_raw(method, url, params, body) ?
return res.text
}

View File

@ -27,6 +27,12 @@ pub fn (c &Client) get_build_log(id int) ?Response<BuildLog> {
return data
}
pub fn (c &Client) get_build_log_content(id int) ?string {
data := c.send_request_raw_response(Method.get, '/api/logs/$id/content', {}, '') ?
return data
}
pub fn (c &Client) add_build_log(repo_id int, start_time time.Time, end_time time.Time, arch string, exit_code int, content string) ?Response<string> {
params := {
'repo': repo_id.str()

View File

@ -0,0 +1 @@
module console

View File

@ -0,0 +1,99 @@
module logs
import cli
import env
import client
import db
struct Config {
address string [required]
api_key string [required]
}
pub fn cmd() cli.Command {
return cli.Command{
name: 'logs'
description: 'Interact with the build logs API.'
commands: [
cli.Command{
name: 'list'
description: 'List the build logs. If a repo ID is provided, only list the build logs for that repo.'
flags: [
cli.Flag{
name: 'repo'
description: 'ID of the Git repo to restrict list to.'
flag: cli.FlagType.int
},
]
execute: fn (cmd cli.Command) ? {
config_file := cmd.flags.get_string('config-file') ?
conf := env.load<Config>(config_file) ?
repo_id := cmd.flags.get_int('repo') ?
if repo_id == 0 { list(conf) ? } else { list_for_repo(conf, repo_id) ? }
}
},
cli.Command{
name: 'info'
required_args: 1
usage: 'id'
description: 'Show all info for a specific build log.'
execute: fn (cmd cli.Command) ? {
config_file := cmd.flags.get_string('config-file') ?
conf := env.load<Config>(config_file) ?
id := cmd.args[0].int()
info(conf, id) ?
}
},
cli.Command{
name: 'content'
required_args: 1
usage: 'id'
description: 'Output the content of a build log to stdout.'
execute: fn (cmd cli.Command) ? {
config_file := cmd.flags.get_string('config-file') ?
conf := env.load<Config>(config_file) ?
id := cmd.args[0].int()
content(conf, id) ?
}
},
]
}
}
fn print_log_list(logs []db.BuildLog) {
for log in logs {
println('$log.id\t$log.start_time\t$log.exit_code')
}
}
fn list(conf Config) ? {
c := client.new(conf.address, conf.api_key)
logs := c.get_build_logs() ?.data
print_log_list(logs)
}
fn list_for_repo(conf Config, repo_id int) ? {
c := client.new(conf.address, conf.api_key)
logs := c.get_build_logs_for_repo(repo_id) ?.data
print_log_list(logs)
}
fn info(conf Config, id int) ? {
c := client.new(conf.address, conf.api_key)
log := c.get_build_log(id) ?.data
print(log)
}
fn content(conf Config, id int) ? {
c := client.new(conf.address, conf.api_key)
content := c.get_build_log_content(id) ?
println(content)
}

View File

@ -12,6 +12,20 @@ pub:
exit_code int [nonull]
}
pub fn (bl &BuildLog) str() string {
mut parts := [
'id: $bl.id',
'repo id: $bl.repo_id',
'start time: $bl.start_time',
'end time: $bl.end_time',
'arch: $bl.arch',
'exit code: $bl.exit_code',
]
str := parts.join('\n')
return str
}
// get_build_logs returns all BuildLog's in the database.
pub fn (db &VieterDb) get_build_logs() []BuildLog {
res := sql db.conn {

View File

@ -4,7 +4,8 @@ import os
import server
import cli
import build
import git
import console.git
import console.logs
import cron
fn main() {
@ -27,6 +28,7 @@ fn main() {
build.cmd(),
git.cmd(),
cron.cmd(),
logs.cmd(),
]
}