From 5e81dadce3bfd9a4a43c979507f0bd9be553a55c Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 18 May 2022 08:22:13 +0200 Subject: [PATCH] feat: partially added filters to GitRepo CLI --- src/client/git.v | 8 +++++--- src/console/git/git.v | 46 ++++++++++++++++++++++++++++++++++++++++--- src/db/filter.v | 1 + src/db/git.v | 1 + src/util/util.v | 10 ++++++++++ 5 files changed, 60 insertions(+), 6 deletions(-) diff --git a/src/client/git.v b/src/client/git.v index 280caab..c21565a 100644 --- a/src/client/git.v +++ b/src/client/git.v @@ -1,12 +1,14 @@ module client -import db { GitRepo } +import db { GitRepo, GitRepoFilter } import net.http { Method } import response { Response } +import util // get_git_repos returns the current list of repos. -pub fn (c &Client) get_git_repos() ?[]GitRepo { - data := c.send_request<[]GitRepo>(Method.get, '/api/repos', {})? +pub fn (c &Client) get_git_repos(filter GitRepoFilter) ?[]GitRepo { + params := util.struct_to_map(filter) + data := c.send_request<[]GitRepo>(Method.get, '/api/repos', params)? return data.data } diff --git a/src/console/git/git.v b/src/console/git/git.v index 06d5f80..861bfb3 100644 --- a/src/console/git/git.v +++ b/src/console/git/git.v @@ -5,6 +5,7 @@ import env import cron.expression { parse_expression } import client import console +import db { GitRepoFilter } struct Config { address string [required] @@ -21,11 +22,50 @@ pub fn cmd() cli.Command { cli.Command{ name: 'list' description: 'List the current repos.' + flags: [ + cli.Flag{ + 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 Git repos that publish to this repo.' + flag: cli.FlagType.string + }, + cli.Flag{ + name: 'arch' + description: 'Only return repos enabled for this architecture.' + flag: cli.FlagType.string + }, + ] execute: fn (cmd cli.Command) ? { config_file := cmd.flags.get_string('config-file')? conf := env.load(config_file)? - list(conf)? + mut filter := GitRepoFilter{} + + if limit := cmd.flags.get_int('limit') { + println('limit = $limit') + filter.limit = u64(limit) + } + + if offset := cmd.flags.get_int('offset') { + filter.limit = u64(offset) + } + + if repo := cmd.flags.get_string('repo') { + filter.repo = repo + } + + dump(filter) + + list(conf, filter)? } }, cli.Command{ @@ -133,9 +173,9 @@ pub fn cmd() cli.Command { // ID. If multiple or none are found, an error is raised. // list prints out a list of all repositories. -fn list(conf Config) ? { +fn list(conf Config, filter GitRepoFilter) ? { c := client.new(conf.address, conf.api_key) - repos := c.get_git_repos()? + repos := c.get_git_repos(filter)? data := repos.map([it.id.str(), it.url, it.branch, it.repo]) println(console.pretty_table(['id', 'url', 'branch', 'repo'], data)?) diff --git a/src/db/filter.v b/src/db/filter.v index 79e281b..07c890a 100644 --- a/src/db/filter.v +++ b/src/db/filter.v @@ -1,5 +1,6 @@ module db +[params] pub struct GitRepoFilter { pub mut: limit u64 = 25 diff --git a/src/db/git.v b/src/db/git.v index cb31bf0..7308346 100644 --- a/src/db/git.v +++ b/src/db/git.v @@ -78,6 +78,7 @@ pub fn git_repo_from_params(params map[string]string) ?GitRepo { // get_git_repos returns all GitRepo's in the database. pub fn (db &VieterDb) get_git_repos(filter GitRepoFilter) []GitRepo { + println(filter) // This seems to currently be blocked by a bug in the ORM, I'll have to ask // around. if filter.repo != '' { diff --git a/src/util/util.v b/src/util/util.v index 266bcb5..cbb4072 100644 --- a/src/util/util.v +++ b/src/util/util.v @@ -64,3 +64,13 @@ pub fn pretty_bytes(bytes int) string { return '${n:.2}${util.prefixes[i]}' } + +pub fn struct_to_map(o T) map[string]string { + mut m := map[string]string{} + + $for field in T.fields { + m[field.name] = o.$(field.name).str() + } + + return m +}