Compare commits

...

2 Commits

6 changed files with 101 additions and 9 deletions

View File

@ -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
}

View File

@ -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>(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)?)

26
src/db/filter.v 100644
View File

@ -0,0 +1,26 @@
module db
[params]
pub struct GitRepoFilter {
pub mut:
limit u64 = 25
offset u64
repo string
}
pub fn filter_from_params<T>(params map[string]string) ?T {
mut o := GitRepoFilter{}
$for field in T.fields {
if field.name in params {
val := params[field.name]
$if field.typ is string {
o.$(field.name) = val
} $else $if field.typ is u64 {
o.$(field.name) = val.u64()
}
}
}
return o
}

View File

@ -77,9 +77,20 @@ 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() []GitRepo {
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 != '' {
res := sql db.conn {
select from GitRepo where repo == filter.repo order by id limit filter.limit offset filter.offset
}
return res
}
res := sql db.conn {
select from GitRepo order by id
select from GitRepo order by id limit filter.limit offset filter.offset
}
return res

View File

@ -12,7 +12,10 @@ fn (mut app App) get_repos() web.Result {
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
}
repos := app.db.get_git_repos()
filter := db.filter_from_params<db.GitRepoFilter>(app.query) or {
return app.json(http.Status.bad_request, new_response('Invalid query parameters.'))
}
repos := app.db.get_git_repos(filter)
return app.json(http.Status.ok, new_data_response(repos))
}

View File

@ -64,3 +64,13 @@ pub fn pretty_bytes(bytes int) string {
return '${n:.2}${util.prefixes[i]}'
}
pub fn struct_to_map<T>(o T) map[string]string {
mut m := map[string]string{}
$for field in T.fields {
m[field.name] = o.$(field.name).str()
}
return m
}