From 401e0291e39dd0e3f92c98db993ec63e0ab7cc10 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sun, 29 May 2022 21:07:46 +0200 Subject: [PATCH] feat(cli): added some filter flags to GitRepo CLI --- src/client/logs.v | 7 +++-- src/console/logs/logs.v | 67 ++++++++++++++++++++++++++++++++++++----- src/db/logs.v | 4 +-- src/models/logs.v | 12 ++++---- src/models/models.v | 9 ++++-- 5 files changed, 77 insertions(+), 22 deletions(-) diff --git a/src/client/logs.v b/src/client/logs.v index 3484f036..d4d373f7 100644 --- a/src/client/logs.v +++ b/src/client/logs.v @@ -1,13 +1,14 @@ module client -import models { BuildLog } +import models { BuildLog, BuildLogFilter } import net.http { Method } import response { Response } import time // get_build_logs returns all build logs. -pub fn (c &Client) get_build_logs() ?Response<[]BuildLog> { - data := c.send_request<[]BuildLog>(Method.get, '/api/logs', {})? +pub fn (c &Client) get_build_logs(filter BuildLogFilter) ?Response<[]BuildLog> { + params := models.params_from(filter) + data := c.send_request<[]BuildLog>(Method.get, '/api/logs', params)? return data } diff --git a/src/console/logs/logs.v b/src/console/logs/logs.v index 1d0bb556..48b9df96 100644 --- a/src/console/logs/logs.v +++ b/src/console/logs/logs.v @@ -4,7 +4,8 @@ import cli import env import client import console -import models { BuildLog } +import time +import models { BuildLog, BuildLogFilter } struct Config { address string [required] @@ -19,21 +20,71 @@ pub fn cmd() cli.Command { commands: [ cli.Command{ name: 'list' - description: 'List the build logs. If a repo ID is provided, only list the build logs for that repo.' + description: 'List build logs.' flags: [ cli.Flag{ - name: 'repo' - description: 'ID of the Git repo to restrict list to.' + name: 'limit' + description: 'How many results to return.' flag: cli.FlagType.int }, + cli.Flag{ + name: 'offset' + description: 'Minimum index to return.' + flag: cli.FlagType.int + }, + cli.Flag{ + name: 'repo' + description: 'Only return logs for this repo id.' + flag: cli.FlagType.int + }, + cli.Flag{ + name: 'today' + description: 'Only list logs started today (UTC time).' + flag: cli.FlagType.bool + }, + cli.Flag{ + name: 'failed' + description: 'Only list logs with non-zero exit codes.' + flag: cli.FlagType.bool + }, ] execute: fn (cmd cli.Command) ? { config_file := cmd.flags.get_string('config-file')? conf := env.load(config_file)? - repo_id := cmd.flags.get_int('repo')? + mut filter := BuildLogFilter{} - if repo_id == 0 { list(conf)? } else { list_for_repo(conf, repo_id)? } + limit := cmd.flags.get_int('limit')? + if limit != 0 { + filter.limit = u64(limit) + } + + offset := cmd.flags.get_int('offset')? + if offset != 0 { + filter.offset = u64(offset) + } + + repo_id := cmd.flags.get_int('repo')? + if repo_id != 0 { + filter.repo = repo_id + } + + if cmd.flags.get_bool('today')? { + today := time.now() + + filter.after = time.new_time(time.Time{ + year: today.year + month: today.month + day: today.day + }) + filter.before = filter.after.add_days(1) + } + + if cmd.flags.get_bool('failed')? { + filter.exit_codes = ['!0'] + } + + list(conf, filter)? } }, cli.Command{ @@ -75,9 +126,9 @@ fn print_log_list(logs []BuildLog) ? { } // list prints a list of all build logs. -fn list(conf Config) ? { +fn list(conf Config, filter BuildLogFilter) ? { c := client.new(conf.address, conf.api_key) - logs := c.get_build_logs()?.data + logs := c.get_build_logs(filter)?.data print_log_list(logs)? } diff --git a/src/db/logs.v b/src/db/logs.v index 806f3d8f..cac08e7f 100644 --- a/src/db/logs.v +++ b/src/db/logs.v @@ -24,8 +24,6 @@ pub fn (db &VieterDb) get_build_logs(filter BuildLogFilter) []BuildLog { where_parts << "arch == '$filter.arch'" } - println(filter.exit_codes) - mut parts := []string{} for exp in filter.exit_codes { @@ -33,7 +31,7 @@ pub fn (db &VieterDb) get_build_logs(filter BuildLogFilter) []BuildLog { code := exp[1..].int() parts << 'exit_code != $code' - }else { + } else { code := exp.int() parts << 'exit_code == $code' diff --git a/src/models/logs.v b/src/models/logs.v index 5ea9dd57..c92dc070 100644 --- a/src/models/logs.v +++ b/src/models/logs.v @@ -30,11 +30,11 @@ pub fn (bl &BuildLog) str() string { [params] pub struct BuildLogFilter { pub mut: - limit u64 = 25 - offset u64 - repo int - before time.Time - after time.Time - arch string + limit u64 = 25 + offset u64 + repo int + before time.Time + after time.Time + arch string exit_codes []string } diff --git a/src/models/models.v b/src/models/models.v index 0d0395a2..3a127bcc 100644 --- a/src/models/models.v +++ b/src/models/models.v @@ -30,7 +30,6 @@ pub fn patch_from_params(mut o T, params map[string]string) ? { } $else $if field.typ is []string { o.$(field.name) = params[field.name].split(',') } - } else if field.attrs.contains('nonull') { return error('Missing parameter: ${field.name}.') } @@ -42,7 +41,13 @@ pub fn params_from(o &T) map[string]string { mut out := map[string]string{} $for field in T.fields { - out[field.name] = o.$(field.name).str() + $if field.typ is time.Time { + out[field.name] = o.$(field.name).unix_time().str() + } $else $if field.typ is []string { + out[field.name] = o.$(field.name).join(',') + } $else { + out[field.name] = o.$(field.name).str() + } } return out }