forked from vieter-v/vieter
				
			feat: partially added filters to GitRepo CLI
							parent
							
								
									1e079143cd
								
							
						
					
					
						commit
						5e81dadce3
					
				| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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)?)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,6 @@
 | 
			
		|||
module db
 | 
			
		||||
 | 
			
		||||
[params]
 | 
			
		||||
pub struct GitRepoFilter {
 | 
			
		||||
pub mut:
 | 
			
		||||
	limit  u64 = 25
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 != '' {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue