2022-04-06 16:52:31 +02:00
|
|
|
module git
|
|
|
|
|
|
|
|
import cli
|
|
|
|
import env
|
|
|
|
|
|
|
|
struct Config {
|
|
|
|
address string [required]
|
|
|
|
api_key string [required]
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:57:05 +02:00
|
|
|
// cmd returns the cli submodule that handles the repos API interaction
|
2022-04-06 16:52:31 +02:00
|
|
|
pub fn cmd() cli.Command {
|
|
|
|
return cli.Command{
|
|
|
|
name: 'repos'
|
|
|
|
description: 'Interact with the repos API.'
|
|
|
|
commands: [
|
2022-04-06 16:57:27 +02:00
|
|
|
cli.Command{
|
|
|
|
name: 'list'
|
|
|
|
description: 'List the current repos.'
|
|
|
|
execute: fn (cmd cli.Command) ? {
|
2022-04-06 17:51:06 +02:00
|
|
|
config_file := cmd.flags.get_string('config-file') ?
|
|
|
|
conf := env.load<Config>(config_file) ?
|
2022-04-06 16:52:31 +02:00
|
|
|
|
2022-04-06 16:57:27 +02:00
|
|
|
list(conf) ?
|
|
|
|
}
|
|
|
|
},
|
2022-04-06 17:16:27 +02:00
|
|
|
cli.Command{
|
|
|
|
name: 'add'
|
2022-04-07 14:28:21 +02:00
|
|
|
required_args: 4
|
|
|
|
usage: 'url branch repo arch...'
|
2022-04-06 17:16:27 +02:00
|
|
|
description: 'Add a new repository.'
|
|
|
|
execute: fn (cmd cli.Command) ? {
|
2022-04-06 17:51:06 +02:00
|
|
|
config_file := cmd.flags.get_string('config-file') ?
|
|
|
|
conf := env.load<Config>(config_file) ?
|
2022-04-06 17:16:27 +02:00
|
|
|
|
2022-04-07 14:28:21 +02:00
|
|
|
add(conf, cmd.args[0], cmd.args[1], cmd.args[2], cmd.args[3..]) ?
|
2022-04-06 17:16:27 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
cli.Command{
|
|
|
|
name: 'remove'
|
2022-04-07 11:54:20 +02:00
|
|
|
required_args: 1
|
|
|
|
usage: 'id'
|
|
|
|
description: 'Remove a repository that matches the given ID prefix.'
|
2022-04-06 17:16:27 +02:00
|
|
|
execute: fn (cmd cli.Command) ? {
|
2022-04-06 17:51:06 +02:00
|
|
|
config_file := cmd.flags.get_string('config-file') ?
|
|
|
|
conf := env.load<Config>(config_file) ?
|
2022-04-06 17:16:27 +02:00
|
|
|
|
2022-04-07 11:54:20 +02:00
|
|
|
remove(conf, cmd.args[0]) ?
|
2022-04-06 17:16:27 +02:00
|
|
|
}
|
|
|
|
},
|
2022-04-07 15:08:27 +02:00
|
|
|
cli.Command{
|
|
|
|
name: 'edit'
|
|
|
|
required_args: 1
|
|
|
|
usage: 'id'
|
|
|
|
description: 'Edit the repository that matches the given ID prefix.'
|
|
|
|
flags: [
|
|
|
|
cli.Flag{
|
|
|
|
name: 'url'
|
|
|
|
description: 'URL of the Git repository.'
|
|
|
|
flag: cli.FlagType.string
|
|
|
|
},
|
|
|
|
cli.Flag{
|
|
|
|
name: 'branch'
|
|
|
|
description: 'Branch of the Git repository.'
|
|
|
|
flag: cli.FlagType.string
|
|
|
|
},
|
|
|
|
cli.Flag{
|
|
|
|
name: 'repo'
|
|
|
|
description: 'Repo to publish builds to.'
|
|
|
|
flag: cli.FlagType.string
|
|
|
|
},
|
|
|
|
cli.Flag{
|
|
|
|
name: 'arch'
|
|
|
|
description: 'Comma-separated list of architectures to build on.'
|
|
|
|
flag: cli.FlagType.string
|
|
|
|
},
|
|
|
|
]
|
|
|
|
execute: fn (cmd cli.Command) ? {
|
|
|
|
config_file := cmd.flags.get_string('config-file') ?
|
|
|
|
conf := env.load<Config>(config_file) ?
|
|
|
|
|
|
|
|
found := cmd.flags.get_all_found()
|
|
|
|
|
|
|
|
mut params := map[string]string{}
|
|
|
|
|
|
|
|
for f in found {
|
|
|
|
if f.name != 'config-file' {
|
|
|
|
params[f.name] = f.get_string() ?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
patch(conf, cmd.args[0], params) ?
|
|
|
|
}
|
|
|
|
},
|
2022-04-06 16:52:31 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 15:08:27 +02:00
|
|
|
fn get_repo_id_by_prefix(conf Config, id_prefix string) ?string {
|
|
|
|
repos := get_repos(conf.address, conf.api_key) ?
|
|
|
|
|
|
|
|
mut res := []string{}
|
|
|
|
|
|
|
|
for id, _ in repos {
|
|
|
|
if id.starts_with(id_prefix) {
|
|
|
|
res << id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.len == 0 {
|
2022-04-07 15:12:48 +02:00
|
|
|
return error('No repo found for given prefix.')
|
2022-04-07 15:08:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if res.len > 1 {
|
2022-04-07 15:12:48 +02:00
|
|
|
return error('Multiple repos found for given prefix.')
|
2022-04-07 15:08:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res[0]
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:54:20 +02:00
|
|
|
fn list(conf Config) ? {
|
2022-04-07 12:07:56 +02:00
|
|
|
repos := get_repos(conf.address, conf.api_key) ?
|
2022-04-07 11:54:20 +02:00
|
|
|
|
|
|
|
for id, details in repos {
|
2022-04-07 14:28:21 +02:00
|
|
|
println('${id[..8]}\t$details.url\t$details.branch\t$details.repo\t$details.arch')
|
2022-04-06 22:41:19 +02:00
|
|
|
}
|
2022-04-06 16:52:31 +02:00
|
|
|
}
|
2022-04-06 17:16:27 +02:00
|
|
|
|
2022-04-07 14:28:21 +02:00
|
|
|
fn add(conf Config, url string, branch string, repo string, arch []string) ? {
|
|
|
|
res := add_repo(conf.address, conf.api_key, url, branch, repo, arch) ?
|
2022-04-06 17:16:27 +02:00
|
|
|
|
2022-04-07 12:07:56 +02:00
|
|
|
println(res.message)
|
2022-04-06 17:16:27 +02:00
|
|
|
}
|
|
|
|
|
2022-04-07 11:54:20 +02:00
|
|
|
fn remove(conf Config, id_prefix string) ? {
|
2022-04-07 15:08:27 +02:00
|
|
|
id := get_repo_id_by_prefix(conf, id_prefix) ?
|
|
|
|
res := remove_repo(conf.address, conf.api_key, id) ?
|
2022-04-07 11:54:20 +02:00
|
|
|
|
2022-04-07 15:08:27 +02:00
|
|
|
println(res.message)
|
|
|
|
}
|
2022-04-07 11:54:20 +02:00
|
|
|
|
2022-04-07 15:08:27 +02:00
|
|
|
fn patch(conf Config, id_prefix string, params map[string]string) ? {
|
|
|
|
id := get_repo_id_by_prefix(conf, id_prefix) ?
|
|
|
|
res := patch_repo(conf.address, conf.api_key, id, params) ?
|
2022-04-06 17:16:27 +02:00
|
|
|
|
2022-04-07 12:07:56 +02:00
|
|
|
println(res.message)
|
2022-04-06 17:16:27 +02:00
|
|
|
}
|