refactor: compile on V 0.3.2

This commit is contained in:
Jef Roosens 2022-11-01 21:10:45 +01:00
parent ed29102717
commit 22fd6e395b
Signed by untrusted user: Jef Roosens
GPG key ID: B75D4F293C7052DB
23 changed files with 205 additions and 203 deletions

View file

@ -21,12 +21,12 @@ pub fn cmd() cli.Command {
name: 'search'
description: 'Search for packages.'
required_args: 1
execute: fn (cmd cli.Command) ? {
execute: fn (cmd cli.Command) ! {
c := aur.new()
pkgs := c.search(cmd.args[0])?
pkgs := c.search(cmd.args[0])!
data := pkgs.map([it.name, it.description])
println(console.pretty_table(['name', 'description'], data)?)
println(console.pretty_table(['name', 'description'], data)!)
}
},
cli.Command{
@ -34,12 +34,12 @@ pub fn cmd() cli.Command {
usage: 'repo pkg-name [pkg-name...]'
description: 'Add the given AUR package(s) to Vieter. Non-existent packages will be silently ignored.'
required_args: 2
execute: fn (cmd cli.Command) ? {
config_file := cmd.flags.get_string('config-file')?
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)?
execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
c := aur.new()
pkgs := c.info(cmd.args[1..])?
pkgs := c.info(cmd.args[1..])!
vc := client.new(conf.address, conf.api_key)

View file

@ -13,7 +13,7 @@ pub fn tabbed_table(data [][]string) string {
// pretty_table converts a list of string data into a pretty table. Many thanks
// to @hungrybluedev in the Vlang Discord for providing this code!
// https://ptb.discord.com/channels/592103645835821068/592106336838352923/970278787143045192
pub fn pretty_table(header []string, data [][]string) ?string {
pub fn pretty_table(header []string, data [][]string) !string {
column_count := header.len
mut column_widths := []int{len: column_count, init: header[it].len}
@ -26,7 +26,7 @@ pub fn pretty_table(header []string, data [][]string) ?string {
}
}
single_line_length := arrays.sum(column_widths)? + (column_count + 1) * 3 - 4
single_line_length := arrays.sum(column_widths)! + (column_count + 1) * 3 - 4
horizontal_line := '+' + strings.repeat(`-`, single_line_length) + '+'
mut buffer := strings.new_builder(data.len * single_line_length)
@ -64,12 +64,12 @@ pub fn pretty_table(header []string, data [][]string) ?string {
// export_man_pages recursively generates all man pages for the given
// cli.Command & writes them to the given directory.
pub fn export_man_pages(cmd cli.Command, path string) ? {
pub fn export_man_pages(cmd cli.Command, path string) ! {
man := cmd.manpage()
os.write_file(os.join_path_single(path, cmd.full_name().replace(' ', '-') + '.1'),
man)?
man)!
for sub_cmd in cmd.commands {
export_man_pages(sub_cmd, path)?
export_man_pages(sub_cmd, path)!
}
}

View file

@ -63,30 +63,30 @@ pub fn cmd() cli.Command {
flag: cli.FlagType.string
},
]
execute: fn (cmd cli.Command) ? {
config_file := cmd.flags.get_string('config-file')?
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)?
execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
mut filter := BuildLogFilter{}
limit := cmd.flags.get_int('limit')?
limit := cmd.flags.get_int('limit')!
if limit != 0 {
filter.limit = u64(limit)
}
offset := cmd.flags.get_int('offset')?
offset := cmd.flags.get_int('offset')!
if offset != 0 {
filter.offset = u64(offset)
}
target_id := cmd.flags.get_int('target')?
target_id := cmd.flags.get_int('target')!
if target_id != 0 {
filter.target = target_id
}
tz_offset := time.offset()
if cmd.flags.get_bool('today')? {
if cmd.flags.get_bool('today')! {
today := time.now()
filter.after = time.new_time(time.Time{
@ -98,12 +98,12 @@ pub fn cmd() cli.Command {
}
// The -today flag overwrites any of the other date flags.
else {
day_str := cmd.flags.get_string('day')?
before_str := cmd.flags.get_string('before')?
after_str := cmd.flags.get_string('after')?
day_str := cmd.flags.get_string('day')!
before_str := cmd.flags.get_string('before')!
after_str := cmd.flags.get_string('after')!
if day_str != '' {
day := time.parse_rfc3339(day_str)?
day := time.parse_rfc3339(day_str)!
day_utc := time.new_time(time.Time{
year: day.year
month: day.month
@ -118,24 +118,24 @@ pub fn cmd() cli.Command {
filter.before = day_utc.add_days(1)
} else {
if before_str != '' {
filter.before = time.parse(before_str)?.add_seconds(-tz_offset)
filter.before = time.parse(before_str)!.add_seconds(-tz_offset)
}
if after_str != '' {
filter.after = time.parse(after_str)?.add_seconds(-tz_offset)
filter.after = time.parse(after_str)!.add_seconds(-tz_offset)
}
}
}
if cmd.flags.get_bool('failed')? {
if cmd.flags.get_bool('failed')! {
filter.exit_codes = [
'!0',
]
}
raw := cmd.flags.get_bool('raw')?
raw := cmd.flags.get_bool('raw')!
list(conf, filter, raw)?
list(conf, filter, raw)!
}
},
cli.Command{
@ -143,12 +143,12 @@ pub fn cmd() cli.Command {
required_args: 1
usage: 'id'
description: 'Show all info for a specific build log.'
execute: fn (cmd cli.Command) ? {
config_file := cmd.flags.get_string('config-file')?
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)?
execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
id := cmd.args[0].int()
info(conf, id)?
info(conf, id)!
}
},
cli.Command{
@ -156,12 +156,12 @@ pub fn cmd() cli.Command {
required_args: 1
usage: 'id'
description: 'Output the content of a build log to stdout.'
execute: fn (cmd cli.Command) ? {
config_file := cmd.flags.get_string('config-file')?
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)?
execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
id := cmd.args[0].int()
content(conf, id)?
content(conf, id)!
}
},
]
@ -169,46 +169,46 @@ pub fn cmd() cli.Command {
}
// print_log_list prints a list of logs.
fn print_log_list(logs []BuildLog, raw bool) ? {
fn print_log_list(logs []BuildLog, raw bool) ! {
data := logs.map([it.id.str(), it.target_id.str(), it.start_time.local().str(),
it.exit_code.str()])
if raw {
println(console.tabbed_table(data))
} else {
println(console.pretty_table(['id', 'target', 'start time', 'exit code'], data)?)
println(console.pretty_table(['id', 'target', 'start time', 'exit code'], data)!)
}
}
// list prints a list of all build logs.
fn list(conf Config, filter BuildLogFilter, raw bool) ? {
fn list(conf Config, filter BuildLogFilter, raw bool) ! {
c := client.new(conf.address, conf.api_key)
logs := c.get_build_logs(filter)?.data
logs := c.get_build_logs(filter)!.data
print_log_list(logs, raw)?
print_log_list(logs, raw)!
}
// list prints a list of all build logs for a given target.
fn list_for_target(conf Config, target_id int, raw bool) ? {
fn list_for_target(conf Config, target_id int, raw bool) ! {
c := client.new(conf.address, conf.api_key)
logs := c.get_build_logs_for_target(target_id)?.data
logs := c.get_build_logs_for_target(target_id)!.data
print_log_list(logs, raw)?
print_log_list(logs, raw)!
}
// info print the detailed info for a given build log.
fn info(conf Config, id int) ? {
fn info(conf Config, id int) ! {
c := client.new(conf.address, conf.api_key)
log := c.get_build_log(id)?.data
log := c.get_build_log(id)!.data
print(log)
}
// content outputs the contents of the log file for a given build log to
// stdout.
fn content(conf Config, id int) ? {
fn content(conf Config, id int) ! {
c := client.new(conf.address, conf.api_key)
content := c.get_build_log_content(id)?
content := c.get_build_log_content(id)!
println(content)
}

View file

@ -11,11 +11,11 @@ pub fn cmd() cli.Command {
description: 'Generate all man pages & save them in the given directory.'
usage: 'dir'
required_args: 1
execute: fn (cmd cli.Command) ? {
execute: fn (cmd cli.Command) ! {
root := cmd.root()
os.mkdir_all(cmd.args[0])?
os.mkdir_all(cmd.args[0])!
console.export_man_pages(root, cmd.args[0])?
console.export_man_pages(root, cmd.args[0])!
}
}
}

View file

@ -18,11 +18,11 @@ pub fn cmd() cli.Command {
default_value: ['5']
},
]
execute: fn (cmd cli.Command) ? {
ce := parse_expression(cmd.args.join(' '))?
count := cmd.flags.get_int('count')?
execute: fn (cmd cli.Command) ! {
ce := parse_expression(cmd.args.join(' '))!
count := cmd.flags.get_int('count')!
for t in ce.next_n(time.now(), count)? {
for t in ce.next_n(time.now(), count)! {
println(t)
}
}

View file

@ -6,29 +6,29 @@ import os
import build
// build locally builds the target with the given id.
fn build(conf Config, target_id int) ? {
fn build(conf Config, target_id int) ! {
c := client.new(conf.address, conf.api_key)
target := c.get_target(target_id)?
target := c.get_target(target_id)!
build_arch := os.uname().machine
println('Creating base image...')
image_id := build.create_build_image(conf.base_image)?
image_id := build.create_build_image(conf.base_image)!
println('Running build...')
res := build.build_target(conf.address, conf.api_key, image_id, target)?
res := build.build_target(conf.address, conf.api_key, image_id, target)!
println('Removing build image...')
mut dd := docker.new_conn()?
mut dd := docker.new_conn()!
defer {
dd.close() or {}
}
dd.remove_image(image_id)?
dd.remove_image(image_id)!
println('Uploading logs to Vieter...')
c.add_build_log(target.id, res.start_time, res.end_time, build_arch, res.exit_code,
res.logs)?
res.logs)!
}

View file

@ -39,30 +39,30 @@ pub fn cmd() cli.Command {
flag: cli.FlagType.string
},
]
execute: fn (cmd cli.Command) ? {
config_file := cmd.flags.get_string('config-file')?
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)?
execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
mut filter := TargetFilter{}
limit := cmd.flags.get_int('limit')?
limit := cmd.flags.get_int('limit')!
if limit != 0 {
filter.limit = u64(limit)
}
offset := cmd.flags.get_int('offset')?
offset := cmd.flags.get_int('offset')!
if offset != 0 {
filter.offset = u64(offset)
}
repo := cmd.flags.get_string('repo')?
repo := cmd.flags.get_string('repo')!
if repo != '' {
filter.repo = repo
}
raw := cmd.flags.get_bool('raw')?
raw := cmd.flags.get_bool('raw')!
list(conf, filter, raw)?
list(conf, filter, raw)!
}
},
cli.Command{
@ -83,20 +83,20 @@ pub fn cmd() cli.Command {
flag: cli.FlagType.string
},
]
execute: fn (cmd cli.Command) ? {
config_file := cmd.flags.get_string('config-file')?
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)?
execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
t := NewTarget{
kind: cmd.flags.get_string('kind')?
kind: cmd.flags.get_string('kind')!
url: cmd.args[0]
repo: cmd.args[1]
branch: cmd.flags.get_string('branch') or { '' }
}
raw := cmd.flags.get_bool('raw')?
raw := cmd.flags.get_bool('raw')!
add(conf, t, raw)?
add(conf, t, raw)!
}
},
cli.Command{
@ -104,11 +104,11 @@ pub fn cmd() cli.Command {
required_args: 1
usage: 'id'
description: 'Remove a target that matches the given id.'
execute: fn (cmd cli.Command) ? {
config_file := cmd.flags.get_string('config-file')?
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)?
execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
remove(conf, cmd.args[0])?
remove(conf, cmd.args[0])!
}
},
cli.Command{
@ -116,11 +116,11 @@ pub fn cmd() cli.Command {
required_args: 1
usage: 'id'
description: 'Show detailed information for the target matching the id.'
execute: fn (cmd cli.Command) ? {
config_file := cmd.flags.get_string('config-file')?
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)?
execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
info(conf, cmd.args[0])?
info(conf, cmd.args[0])!
}
},
cli.Command{
@ -160,9 +160,9 @@ pub fn cmd() cli.Command {
flag: cli.FlagType.string
},
]
execute: fn (cmd cli.Command) ? {
config_file := cmd.flags.get_string('config-file')?
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)?
execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
found := cmd.flags.get_all_found()
@ -170,11 +170,11 @@ pub fn cmd() cli.Command {
for f in found {
if f.name != 'config-file' {
params[f.name] = f.get_string()?
params[f.name] = f.get_string()!
}
}
patch(conf, cmd.args[0], params)?
patch(conf, cmd.args[0], params)!
}
},
cli.Command{
@ -182,11 +182,11 @@ pub fn cmd() cli.Command {
required_args: 1
usage: 'id'
description: 'Build the target with the given id & publish it.'
execute: fn (cmd cli.Command) ? {
config_file := cmd.flags.get_string('config-file')?
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)?
execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
build(conf, cmd.args[0].int())?
build(conf, cmd.args[0].int())!
}
},
]
@ -197,22 +197,22 @@ 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, filter TargetFilter, raw bool) ? {
fn list(conf Config, filter TargetFilter, raw bool) ! {
c := client.new(conf.address, conf.api_key)
repos := c.get_targets(filter)?
repos := c.get_targets(filter)!
data := repos.map([it.id.str(), it.kind, it.url, it.repo])
if raw {
println(console.tabbed_table(data))
} else {
println(console.pretty_table(['id', 'kind', 'url', 'repo'], data)?)
println(console.pretty_table(['id', 'kind', 'url', 'repo'], data)!)
}
}
// add adds a new repository to the server's list.
fn add(conf Config, t &NewTarget, raw bool) ? {
fn add(conf Config, t &NewTarget, raw bool) ! {
c := client.new(conf.address, conf.api_key)
res := c.add_target(t)?
res := c.add_target(t)!
if raw {
println(res.data)
@ -222,18 +222,18 @@ fn add(conf Config, t &NewTarget, raw bool) ? {
}
// remove removes a repository from the server's list.
fn remove(conf Config, id string) ? {
fn remove(conf Config, id string) ! {
id_int := id.int()
if id_int != 0 {
c := client.new(conf.address, conf.api_key)
res := c.remove_target(id_int)?
res := c.remove_target(id_int)!
println(res.message)
}
}
// patch patches a given repository with the provided params.
fn patch(conf Config, id string, params map[string]string) ? {
fn patch(conf Config, id string, params map[string]string) ! {
// We check the cron expression first because it's useless to send an
// invalid one to the server.
if 'schedule' in params && params['schedule'] != '' {
@ -245,14 +245,14 @@ fn patch(conf Config, id string, params map[string]string) ? {
id_int := id.int()
if id_int != 0 {
c := client.new(conf.address, conf.api_key)
res := c.patch_target(id_int, params)?
res := c.patch_target(id_int, params)!
println(res.message)
}
}
// info shows detailed information for a given repo.
fn info(conf Config, id string) ? {
fn info(conf Config, id string) ! {
id_int := id.int()
if id_int == 0 {
@ -260,6 +260,6 @@ fn info(conf Config, id string) ? {
}
c := client.new(conf.address, conf.api_key)
repo := c.get_target(id_int)?
repo := c.get_target(id_int)!
println(repo)
}