2022-06-14 20:38:20 +02:00
|
|
|
module targets
|
2022-04-06 16:52:31 +02:00
|
|
|
|
|
|
|
import cli
|
2022-06-22 09:18:47 +02:00
|
|
|
import vieter_v.conf as vconf
|
2022-05-01 12:52:05 +02:00
|
|
|
import cron.expression { parse_expression }
|
2022-06-17 13:45:21 +02:00
|
|
|
import client { NewTarget }
|
2022-05-09 16:16:25 +02:00
|
|
|
import console
|
2022-06-14 22:25:40 +02:00
|
|
|
import models { TargetFilter }
|
2022-04-06 16:52:31 +02:00
|
|
|
|
|
|
|
struct Config {
|
2022-05-16 17:12:37 +02:00
|
|
|
address string [required]
|
|
|
|
api_key string [required]
|
|
|
|
base_image string = 'archlinux:base-devel'
|
2022-04-06 16:52:31 +02:00
|
|
|
}
|
|
|
|
|
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{
|
2022-06-14 20:38:20 +02:00
|
|
|
name: 'targets'
|
|
|
|
description: 'Interact with the targets API.'
|
2022-04-06 16:52:31 +02:00
|
|
|
commands: [
|
2022-04-06 16:57:27 +02:00
|
|
|
cli.Command{
|
|
|
|
name: 'list'
|
2022-06-14 20:38:20 +02:00
|
|
|
description: 'List the current targets.'
|
2022-05-18 08:22:13 +02:00
|
|
|
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'
|
2022-06-14 20:38:20 +02:00
|
|
|
description: 'Only return targets that publish to this repo.'
|
2022-05-18 08:22:13 +02:00
|
|
|
flag: cli.FlagType.string
|
|
|
|
},
|
|
|
|
]
|
2022-04-06 16:57:27 +02:00
|
|
|
execute: fn (cmd cli.Command) ? {
|
2022-05-14 20:06:08 +02:00
|
|
|
config_file := cmd.flags.get_string('config-file')?
|
2022-06-15 22:54:27 +02:00
|
|
|
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)?
|
2022-04-06 16:52:31 +02:00
|
|
|
|
2022-06-14 22:25:40 +02:00
|
|
|
mut filter := TargetFilter{}
|
2022-05-18 08:22:13 +02:00
|
|
|
|
2022-05-19 07:54:33 +02:00
|
|
|
limit := cmd.flags.get_int('limit')?
|
|
|
|
if limit != 0 {
|
2022-05-18 08:22:13 +02:00
|
|
|
filter.limit = u64(limit)
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:54:33 +02:00
|
|
|
offset := cmd.flags.get_int('offset')?
|
|
|
|
if offset != 0 {
|
|
|
|
filter.offset = u64(offset)
|
2022-05-18 08:22:13 +02:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:54:33 +02:00
|
|
|
repo := cmd.flags.get_string('repo')?
|
|
|
|
if repo != '' {
|
2022-05-18 08:22:13 +02:00
|
|
|
filter.repo = repo
|
|
|
|
}
|
|
|
|
|
|
|
|
list(conf, filter)?
|
2022-04-06 16:57:27 +02:00
|
|
|
}
|
|
|
|
},
|
2022-04-06 17:16:27 +02:00
|
|
|
cli.Command{
|
|
|
|
name: 'add'
|
2022-06-17 13:45:21 +02:00
|
|
|
required_args: 2
|
|
|
|
usage: 'url repo'
|
2022-06-17 13:56:38 +02:00
|
|
|
description: 'Add a new target with the given URL & target repo.'
|
2022-06-17 13:45:21 +02:00
|
|
|
flags: [
|
|
|
|
cli.Flag{
|
|
|
|
name: 'kind'
|
|
|
|
description: "Kind of target to add. Defaults to 'git' if not specified. One of 'git', 'url'."
|
|
|
|
flag: cli.FlagType.string
|
|
|
|
default_value: ['git']
|
|
|
|
},
|
|
|
|
cli.Flag{
|
|
|
|
name: 'branch'
|
|
|
|
description: "Which branch to clone; only applies to kind 'git'."
|
|
|
|
flag: cli.FlagType.string
|
2022-06-17 13:56:38 +02:00
|
|
|
},
|
2022-06-17 13:45:21 +02:00
|
|
|
]
|
2022-04-06 17:16:27 +02:00
|
|
|
execute: fn (cmd cli.Command) ? {
|
2022-05-14 20:06:08 +02:00
|
|
|
config_file := cmd.flags.get_string('config-file')?
|
2022-06-15 22:54:27 +02:00
|
|
|
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)?
|
2022-04-06 17:16:27 +02:00
|
|
|
|
2022-06-17 13:45:21 +02:00
|
|
|
t := NewTarget{
|
|
|
|
kind: cmd.flags.get_string('kind')?
|
|
|
|
url: cmd.args[0]
|
|
|
|
repo: cmd.args[1]
|
|
|
|
branch: cmd.flags.get_string('branch') or { '' }
|
|
|
|
}
|
|
|
|
|
|
|
|
add(conf, t)?
|
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'
|
2022-06-14 20:38:20 +02:00
|
|
|
description: 'Remove a target that matches the given id.'
|
2022-04-06 17:16:27 +02:00
|
|
|
execute: fn (cmd cli.Command) ? {
|
2022-05-14 20:06:08 +02:00
|
|
|
config_file := cmd.flags.get_string('config-file')?
|
2022-06-15 22:54:27 +02:00
|
|
|
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)?
|
2022-04-06 17:16:27 +02:00
|
|
|
|
2022-05-14 20:06:08 +02:00
|
|
|
remove(conf, cmd.args[0])?
|
2022-04-06 17:16:27 +02:00
|
|
|
}
|
|
|
|
},
|
2022-05-01 13:06:57 +02:00
|
|
|
cli.Command{
|
|
|
|
name: 'info'
|
|
|
|
required_args: 1
|
|
|
|
usage: 'id'
|
2022-06-14 20:38:20 +02:00
|
|
|
description: 'Show detailed information for the target matching the id.'
|
2022-05-01 13:06:57 +02:00
|
|
|
execute: fn (cmd cli.Command) ? {
|
2022-05-14 20:06:08 +02:00
|
|
|
config_file := cmd.flags.get_string('config-file')?
|
2022-06-15 22:54:27 +02:00
|
|
|
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)?
|
2022-05-01 13:06:57 +02:00
|
|
|
|
2022-05-14 20:06:08 +02:00
|
|
|
info(conf, cmd.args[0])?
|
2022-05-01 13:06:57 +02:00
|
|
|
}
|
|
|
|
},
|
2022-04-07 15:08:27 +02:00
|
|
|
cli.Command{
|
|
|
|
name: 'edit'
|
|
|
|
required_args: 1
|
|
|
|
usage: 'id'
|
2022-06-17 14:31:34 +02:00
|
|
|
description: 'Edit the target that matches the given id.'
|
2022-04-07 15:08:27 +02:00
|
|
|
flags: [
|
|
|
|
cli.Flag{
|
|
|
|
name: 'url'
|
2022-06-17 14:31:34 +02:00
|
|
|
description: 'URL value. Meaning depends on kind of target.'
|
2022-04-07 15:08:27 +02:00
|
|
|
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
|
|
|
|
},
|
2022-05-01 12:52:05 +02:00
|
|
|
cli.Flag{
|
|
|
|
name: 'schedule'
|
|
|
|
description: 'Cron schedule for repository.'
|
|
|
|
flag: cli.FlagType.string
|
|
|
|
},
|
2022-06-17 13:56:38 +02:00
|
|
|
cli.Flag{
|
|
|
|
name: 'kind'
|
|
|
|
description: 'Kind of target.'
|
|
|
|
flag: cli.FlagType.string
|
|
|
|
},
|
2022-04-07 15:08:27 +02:00
|
|
|
]
|
|
|
|
execute: fn (cmd cli.Command) ? {
|
2022-05-14 20:06:08 +02:00
|
|
|
config_file := cmd.flags.get_string('config-file')?
|
2022-06-15 22:54:27 +02:00
|
|
|
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)?
|
2022-04-07 15:08:27 +02:00
|
|
|
|
|
|
|
found := cmd.flags.get_all_found()
|
|
|
|
|
|
|
|
mut params := map[string]string{}
|
|
|
|
|
|
|
|
for f in found {
|
|
|
|
if f.name != 'config-file' {
|
2022-05-14 20:06:08 +02:00
|
|
|
params[f.name] = f.get_string()?
|
2022-04-07 15:08:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 20:06:08 +02:00
|
|
|
patch(conf, cmd.args[0], params)?
|
2022-04-07 15:08:27 +02:00
|
|
|
}
|
|
|
|
},
|
2022-05-16 17:12:37 +02:00
|
|
|
cli.Command{
|
|
|
|
name: 'build'
|
|
|
|
required_args: 1
|
|
|
|
usage: 'id'
|
2022-06-14 20:38:20 +02:00
|
|
|
description: 'Build the target with the given id & publish it.'
|
2022-05-16 17:12:37 +02:00
|
|
|
execute: fn (cmd cli.Command) ? {
|
|
|
|
config_file := cmd.flags.get_string('config-file')?
|
2022-06-15 22:54:27 +02:00
|
|
|
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)?
|
2022-05-16 17:12:37 +02:00
|
|
|
|
|
|
|
build(conf, cmd.args[0].int())?
|
|
|
|
}
|
|
|
|
},
|
2022-04-06 16:52:31 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-01 13:06:57 +02:00
|
|
|
// get_repo_by_prefix tries to find the repo with the given prefix in its
|
2022-04-30 20:22:03 +02:00
|
|
|
// ID. If multiple or none are found, an error is raised.
|
2022-04-07 15:08:27 +02:00
|
|
|
|
2022-04-30 20:22:03 +02:00
|
|
|
// list prints out a list of all repositories.
|
2022-06-14 22:25:40 +02:00
|
|
|
fn list(conf Config, filter TargetFilter) ? {
|
2022-05-07 16:10:27 +02:00
|
|
|
c := client.new(conf.address, conf.api_key)
|
2022-06-14 20:31:00 +02:00
|
|
|
repos := c.get_targets(filter)?
|
2022-06-17 13:45:21 +02:00
|
|
|
data := repos.map([it.id.str(), it.kind, it.url, it.repo])
|
2022-04-07 11:54:20 +02:00
|
|
|
|
2022-06-17 13:56:38 +02:00
|
|
|
println(console.pretty_table(['id', 'kind', 'url', 'repo'], data)?)
|
2022-04-06 16:52:31 +02:00
|
|
|
}
|
2022-04-06 17:16:27 +02:00
|
|
|
|
2022-04-30 20:22:03 +02:00
|
|
|
// add adds a new repository to the server's list.
|
2022-06-17 13:45:21 +02:00
|
|
|
fn add(conf Config, t &NewTarget) ? {
|
2022-05-07 16:10:27 +02:00
|
|
|
c := client.new(conf.address, conf.api_key)
|
2022-06-17 13:45:21 +02:00
|
|
|
res := c.add_target(t)?
|
2022-04-06 17:16:27 +02:00
|
|
|
|
2022-09-11 20:34:02 +02:00
|
|
|
println('Target added with id $res.data')
|
2022-04-06 17:16:27 +02:00
|
|
|
}
|
|
|
|
|
2022-04-30 20:22:03 +02:00
|
|
|
// remove removes a repository from the server's list.
|
2022-05-03 16:16:56 +02:00
|
|
|
fn remove(conf Config, id string) ? {
|
|
|
|
id_int := id.int()
|
2022-04-07 11:54:20 +02:00
|
|
|
|
2022-05-03 16:16:56 +02:00
|
|
|
if id_int != 0 {
|
2022-05-07 16:10:27 +02:00
|
|
|
c := client.new(conf.address, conf.api_key)
|
2022-06-14 20:31:00 +02:00
|
|
|
res := c.remove_target(id_int)?
|
2022-05-03 16:16:56 +02:00
|
|
|
println(res.message)
|
|
|
|
}
|
2022-04-07 15:08:27 +02:00
|
|
|
}
|
2022-04-07 11:54:20 +02:00
|
|
|
|
2022-04-30 20:22:03 +02:00
|
|
|
// patch patches a given repository with the provided params.
|
2022-05-03 16:16:56 +02:00
|
|
|
fn patch(conf Config, id string, params map[string]string) ? {
|
2022-05-01 12:52:05 +02:00
|
|
|
// We check the cron expression first because it's useless to send an
|
|
|
|
// invalid one to the server.
|
2022-05-01 14:51:52 +02:00
|
|
|
if 'schedule' in params && params['schedule'] != '' {
|
2022-05-01 12:52:05 +02:00
|
|
|
parse_expression(params['schedule']) or {
|
|
|
|
return error('Invalid cron expression: $err.msg()')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-03 16:16:56 +02:00
|
|
|
id_int := id.int()
|
|
|
|
if id_int != 0 {
|
2022-05-07 16:10:27 +02:00
|
|
|
c := client.new(conf.address, conf.api_key)
|
2022-06-14 20:31:00 +02:00
|
|
|
res := c.patch_target(id_int, params)?
|
2022-04-06 17:16:27 +02:00
|
|
|
|
2022-05-03 16:16:56 +02:00
|
|
|
println(res.message)
|
|
|
|
}
|
2022-04-06 17:16:27 +02:00
|
|
|
}
|
2022-05-01 13:06:57 +02:00
|
|
|
|
|
|
|
// info shows detailed information for a given repo.
|
2022-05-03 16:16:56 +02:00
|
|
|
fn info(conf Config, id string) ? {
|
|
|
|
id_int := id.int()
|
2022-05-01 13:06:57 +02:00
|
|
|
|
2022-05-03 16:16:56 +02:00
|
|
|
if id_int == 0 {
|
|
|
|
return
|
2022-05-01 13:06:57 +02:00
|
|
|
}
|
2022-05-03 16:16:56 +02:00
|
|
|
|
2022-05-07 19:38:28 +02:00
|
|
|
c := client.new(conf.address, conf.api_key)
|
2022-06-14 20:31:00 +02:00
|
|
|
repo := c.get_target(id_int)?
|
2022-05-03 16:16:56 +02:00
|
|
|
println(repo)
|
2022-05-01 13:06:57 +02:00
|
|
|
}
|