2022-04-06 16:52:31 +02:00
|
|
|
module git
|
|
|
|
|
|
|
|
import cli
|
|
|
|
import env
|
|
|
|
import net.http
|
2022-04-06 22:41:19 +02:00
|
|
|
import json
|
|
|
|
import git
|
|
|
|
import response
|
2022-04-06 16:52:31 +02:00
|
|
|
|
|
|
|
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'
|
|
|
|
required_args: 2
|
2022-04-06 22:41:19 +02:00
|
|
|
usage: 'url branch 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-06 22:41:19 +02:00
|
|
|
add(conf, cmd.args[0], cmd.args[1], cmd.args[2..]) ?
|
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-06 16:52:31 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:54:20 +02:00
|
|
|
fn get_repos(conf Config) ?map[string]git.GitRepo {
|
2022-04-06 16:52:31 +02:00
|
|
|
mut req := http.new_request(http.Method.get, '$conf.address/api/repos', '') ?
|
|
|
|
req.add_custom_header('X-API-Key', conf.api_key) ?
|
|
|
|
|
|
|
|
res := req.do() ?
|
2022-04-06 22:41:19 +02:00
|
|
|
data := json.decode(response.Response<map[string]git.GitRepo>, res.text) ?
|
2022-04-06 16:52:31 +02:00
|
|
|
|
2022-04-07 11:54:20 +02:00
|
|
|
return data.data
|
|
|
|
}
|
|
|
|
|
|
|
|
fn list(conf Config) ? {
|
|
|
|
repos := get_repos(conf) ?
|
|
|
|
|
|
|
|
for id, details in repos {
|
2022-04-06 22:41:19 +02:00
|
|
|
println("${id[..8]}\t$details.url\t$details.branch\t$details.arch")
|
|
|
|
}
|
2022-04-06 16:52:31 +02:00
|
|
|
}
|
2022-04-06 17:16:27 +02:00
|
|
|
|
2022-04-06 22:41:19 +02:00
|
|
|
fn add(conf Config, url string, branch string, arch []string) ? {
|
|
|
|
mut req := http.new_request(http.Method.post, '$conf.address/api/repos?url=$url&branch=$branch&arch=${arch.join(',')}',
|
2022-04-06 17:51:06 +02:00
|
|
|
'') ?
|
2022-04-06 17:16:27 +02:00
|
|
|
req.add_custom_header('X-API-Key', conf.api_key) ?
|
|
|
|
|
|
|
|
res := req.do() ?
|
|
|
|
|
|
|
|
println(res.text)
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:54:20 +02:00
|
|
|
fn remove(conf Config, id_prefix string) ? {
|
|
|
|
repos := get_repos(conf) ?
|
|
|
|
|
|
|
|
mut to_remove := []string{}
|
|
|
|
|
|
|
|
for id, _ in repos {
|
|
|
|
if id.starts_with(id_prefix) {
|
|
|
|
to_remove << id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if to_remove.len == 0 {
|
|
|
|
eprintln("No repo found for given prefix.")
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if to_remove.len > 1 {
|
|
|
|
eprintln("Multiple repos found for given prefix.")
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
mut req := http.new_request(http.Method.delete, '$conf.address/api/repos/${to_remove[0]}',
|
2022-04-06 17:51:06 +02:00
|
|
|
'') ?
|
2022-04-06 17:16:27 +02:00
|
|
|
req.add_custom_header('X-API-Key', conf.api_key) ?
|
|
|
|
|
|
|
|
res := req.do() ?
|
|
|
|
|
|
|
|
println(res.text)
|
|
|
|
}
|