From 7b59277931172997759572ac5a00930d677a4d67 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Mon, 5 Sep 2022 10:10:02 +0200 Subject: [PATCH 1/7] feat: adding target returns id of added entry --- src/client/targets.v | 4 ++-- src/console/targets/targets.v | 2 +- src/db/targets.v | 6 +++++- src/server/api_targets.v | 6 +++--- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/client/targets.v b/src/client/targets.v index f5258a43..c5e44fea 100644 --- a/src/client/targets.v +++ b/src/client/targets.v @@ -49,9 +49,9 @@ pub struct NewTarget { } // add_target adds a new target to the server. -pub fn (c &Client) add_target(t NewTarget) ?Response { +pub fn (c &Client) add_target(t NewTarget) ?Response { params := models.params_from(t) - data := c.send_request(Method.post, '/api/v1/targets', params)? + data := c.send_request(Method.post, '/api/v1/targets', params)? return data } diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index 66d48fb3..a5559360 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -206,7 +206,7 @@ fn add(conf Config, t &NewTarget) ? { c := client.new(conf.address, conf.api_key) res := c.add_target(t)? - println(res.message) + println("Target added with id $res.data") } // remove removes a repository from the server's list. diff --git a/src/db/targets.v b/src/db/targets.v index 91020337..a705ebb5 100644 --- a/src/db/targets.v +++ b/src/db/targets.v @@ -38,10 +38,14 @@ pub fn (db &VieterDb) get_target(target_id int) ?Target { } // add_target inserts the given target into the database. -pub fn (db &VieterDb) add_target(repo Target) { +pub fn (db &VieterDb) add_target(repo Target) int { sql db.conn { insert repo into Target } + + inserted_id := db.conn.last_id() as int + + return inserted_id } // delete_target deletes the target with the given id from the database. diff --git a/src/server/api_targets.v b/src/server/api_targets.v index c9e7963d..5757d76b 100644 --- a/src/server/api_targets.v +++ b/src/server/api_targets.v @@ -2,7 +2,7 @@ module server import web import net.http -import web.response { new_data_response, new_response } +import web.response { new_data_response, new_response, new_full_response } import db import models { Target, TargetArch, TargetFilter } @@ -45,9 +45,9 @@ fn (mut app App) v1_post_target() web.Result { return app.json(http.Status.bad_request, new_response('Invalid kind.')) } - app.db.add_target(new_repo) + id := app.db.add_target(new_repo) - return app.json(http.Status.ok, new_response('Repo added successfully.')) + return app.json(http.Status.ok, new_data_response(id)) } // v1_delete_target removes a given target from the server's list. From 210508f1ee251b3cb61ab5bf146805f10b600c1c Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sun, 11 Sep 2022 20:34:02 +0200 Subject: [PATCH 2/7] feat: logs api now also returns id --- CHANGELOG.md | 1 + src/client/logs.v | 4 ++-- src/console/targets/targets.v | 2 +- src/db/logs.v | 6 +++++- src/server/api_logs.v | 9 +++++---- src/server/api_targets.v | 10 +++++----- 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5aa0e434..00a3539c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 repository will be cloned with the default branch * Build containers now explicitely set the PATH variable * Refactor of web framework +* API endpoints now return id of newly created entries ### Removed diff --git a/src/client/logs.v b/src/client/logs.v index b52c3d09..b4142450 100644 --- a/src/client/logs.v +++ b/src/client/logs.v @@ -39,7 +39,7 @@ pub fn (c &Client) get_build_log_content(id int) ?string { } // add_build_log adds a new build log to the server. -pub fn (c &Client) add_build_log(target_id int, start_time time.Time, end_time time.Time, arch string, exit_code int, content string) ?Response { +pub fn (c &Client) add_build_log(target_id int, start_time time.Time, end_time time.Time, arch string, exit_code int, content string) ?Response { params := { 'target': target_id.str() 'startTime': start_time.unix_time().str() @@ -48,7 +48,7 @@ pub fn (c &Client) add_build_log(target_id int, start_time time.Time, end_time t 'exitCode': exit_code.str() } - data := c.send_request_with_body(Method.post, '/api/v1/logs', params, content)? + data := c.send_request_with_body(Method.post, '/api/v1/logs', params, content)? return data } diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index a5559360..2784dbc3 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -206,7 +206,7 @@ fn add(conf Config, t &NewTarget) ? { c := client.new(conf.address, conf.api_key) res := c.add_target(t)? - println("Target added with id $res.data") + println('Target added with id $res.data') } // remove removes a repository from the server's list. diff --git a/src/db/logs.v b/src/db/logs.v index af5f53c3..923dde2d 100644 --- a/src/db/logs.v +++ b/src/db/logs.v @@ -79,10 +79,14 @@ pub fn (db &VieterDb) get_build_log(id int) ?BuildLog { } // add_build_log inserts the given BuildLog into the database. -pub fn (db &VieterDb) add_build_log(log BuildLog) { +pub fn (db &VieterDb) add_build_log(log BuildLog) int { sql db.conn { insert log into BuildLog } + + inserted_id := db.conn.last_id() as int + + return inserted_id } // delete_build_log delete the BuildLog with the given ID from the database. diff --git a/src/server/api_logs.v b/src/server/api_logs.v index 021c1aca..287755a9 100644 --- a/src/server/api_logs.v +++ b/src/server/api_logs.v @@ -19,7 +19,7 @@ fn (mut app App) v1_get_logs() web.Result { } logs := app.db.get_build_logs(filter) - return app.json(http.Status.ok, new_data_response(logs)) + return app.json(.ok, new_data_response(logs)) } // v1_get_single_log returns the build log with the given id. @@ -27,7 +27,7 @@ fn (mut app App) v1_get_logs() web.Result { fn (mut app App) v1_get_single_log(id int) web.Result { log := app.db.get_build_log(id) or { return app.not_found() } - return app.json(http.Status.ok, new_data_response(log)) + return app.json(.ok, new_data_response(log)) } // v1_get_log_content returns the actual build log file for the given id. @@ -95,7 +95,8 @@ fn (mut app App) v1_post_log() web.Result { exit_code: exit_code } - app.db.add_build_log(log) + // id of newly created log + log_id := app.db.add_build_log(log) repo_logs_dir := os.join_path(app.conf.data_dir, logs_dir_name, target_id.str(), arch) @@ -122,5 +123,5 @@ fn (mut app App) v1_post_log() web.Result { return app.status(http.Status.length_required) } - return app.json(http.Status.ok, new_response('Logs added successfully.')) + return app.json(.ok, new_data_response(log_id)) } diff --git a/src/server/api_targets.v b/src/server/api_targets.v index 5757d76b..6f284af6 100644 --- a/src/server/api_targets.v +++ b/src/server/api_targets.v @@ -2,7 +2,7 @@ module server import web import net.http -import web.response { new_data_response, new_response, new_full_response } +import web.response { new_data_response, new_response } import db import models { Target, TargetArch, TargetFilter } @@ -14,7 +14,7 @@ fn (mut app App) v1_get_targets() web.Result { } repos := app.db.get_targets(filter) - return app.json(http.Status.ok, new_data_response(repos)) + return app.json(.ok, new_data_response(repos)) } // v1_get_single_target returns the information for a single target. @@ -22,7 +22,7 @@ fn (mut app App) v1_get_targets() web.Result { fn (mut app App) v1_get_single_target(id int) web.Result { repo := app.db.get_target(id) or { return app.not_found() } - return app.json(http.Status.ok, new_data_response(repo)) + return app.json(.ok, new_data_response(repo)) } // v1_post_target creates a new target from the provided query string. @@ -55,7 +55,7 @@ fn (mut app App) v1_post_target() web.Result { fn (mut app App) v1_delete_target(id int) web.Result { app.db.delete_target(id) - return app.json(http.Status.ok, new_response('Repo removed successfully.')) + return app.status(.ok) } // v1_patch_target updates a target's data with the given query params. @@ -69,5 +69,5 @@ fn (mut app App) v1_patch_target(id int) web.Result { app.db.update_target_archs(id, arch_objs) } - return app.json(http.Status.ok, new_response('Repo updated successfully.')) + return app.status(.ok) } From b6cd2f0bc280dfddfa85a91188ebe05433eabbf8 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sun, 11 Sep 2022 21:24:29 +0200 Subject: [PATCH 3/7] feat(server): repo POST requests now return information --- CHANGELOG.md | 1 + src/repo/add.v | 43 +++++++++++++++++++------------------------ src/server/repo.v | 21 ++++++--------------- 3 files changed, 26 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00a3539c..c875f2e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Build containers now explicitely set the PATH variable * Refactor of web framework * API endpoints now return id of newly created entries +* Repo POST requests now return information on published package ### Removed diff --git a/src/repo/add.v b/src/repo/add.v index 0985c7a6..608ca501 100644 --- a/src/repo/add.v +++ b/src/repo/add.v @@ -23,8 +23,9 @@ pub: pub struct RepoAddResult { pub: - added bool [required] - pkg &package.Pkg [required] + name string + version string + archs []string } // new creates a new RepoGroupManager & creates the directories as needed @@ -53,10 +54,10 @@ pub fn (r &RepoGroupManager) add_pkg_from_path(repo string, pkg_path string) ?Re return error('Failed to read package file: $err.msg()') } - added := r.add_pkg_in_repo(repo, pkg)? + archs := r.add_pkg_in_repo(repo, pkg)? // If the add was successful, we move the file to the packages directory - for arch in added { + for arch in archs { repo_pkg_path := os.real_path(os.join_path(r.pkg_dir, repo, arch)) dest_path := os.join_path_single(repo_pkg_path, pkg.filename()) @@ -71,8 +72,9 @@ pub fn (r &RepoGroupManager) add_pkg_from_path(repo string, pkg_path string) ?Re os.rm(pkg_path)? return RepoAddResult{ - added: added.len > 0 - pkg: &pkg + name: pkg.info.name + version: pkg.info.version + archs: archs } } @@ -87,11 +89,9 @@ fn (r &RepoGroupManager) add_pkg_in_repo(repo string, pkg &package.Pkg) ?[]strin // A package not of arch 'any' can be handled easily by adding it to the // respective repo if pkg.info.arch != 'any' { - if r.add_pkg_in_arch_repo(repo, pkg.info.arch, pkg)? { - return [pkg.info.arch] - } else { - return [] - } + r.add_pkg_in_arch_repo(repo, pkg.info.arch, pkg)? + + return [pkg.info.arch] } mut arch_repos := []string{} @@ -113,25 +113,22 @@ fn (r &RepoGroupManager) add_pkg_in_repo(repo string, pkg &package.Pkg) ?[]strin arch_repos << r.default_arch } - mut added := []string{} - - // We add the package to each repository. If any of the repositories - // return true, the result of the function is also true. + // Add the package to each found architecture + // NOTE: if any of these fail, the function fails. This means the user does + // not know which arch-repositories did succeed in adding the package, if + // any. for arch in arch_repos { - if r.add_pkg_in_arch_repo(repo, arch, pkg)? { - added << arch - } + r.add_pkg_in_arch_repo(repo, arch, pkg)? } - return added + return arch_repos } // add_pkg_in_arch_repo is the function that actually adds a package to a given // arch-repo. It records the package's data in the arch-repo's desc & files // files, and afterwards updates the db & files archives to reflect these -// changes. The function returns false if the package was already present in -// the repo, and true otherwise. -fn (r &RepoGroupManager) add_pkg_in_arch_repo(repo string, arch string, pkg &package.Pkg) ?bool { +// changes. +fn (r &RepoGroupManager) add_pkg_in_arch_repo(repo string, arch string, pkg &package.Pkg) ? { pkg_dir := os.join_path(r.repos_dir, repo, arch, '$pkg.info.name-$pkg.info.version') // Remove the previous version of the package, if present @@ -151,6 +148,4 @@ fn (r &RepoGroupManager) add_pkg_in_arch_repo(repo string, arch string, pkg &pac } r.sync(repo, arch)? - - return true } diff --git a/src/server/repo.v b/src/server/repo.v index 5ed5d15c..526d4e76 100644 --- a/src/server/repo.v +++ b/src/server/repo.v @@ -6,8 +6,7 @@ import repo import time import rand import util -import net.http -import web.response { new_response } +import web.response { new_data_response, new_response } // healthcheck just returns a string, but can be used to quickly check if the // server is still responsive. @@ -65,7 +64,7 @@ fn (mut app App) put_package(repo string) web.Result { util.reader_to_file(mut app.reader, length.int(), pkg_path) or { app.lwarn("Failed to upload '$pkg_path'") - return app.json(http.Status.internal_server_error, new_response('Failed to upload file.')) + return app.status(.internal_server_error) } sw.stop() @@ -74,7 +73,7 @@ fn (mut app App) put_package(repo string) web.Result { app.lwarn('Tried to upload package without specifying a Content-Length.') // length required - return app.status(http.Status.length_required) + return app.status(.length_required) } res := app.repo.add_pkg_from_path(repo, pkg_path) or { @@ -82,18 +81,10 @@ fn (mut app App) put_package(repo string) web.Result { os.rm(pkg_path) or { app.lerror("Failed to remove download '$pkg_path': $err.msg()") } - return app.json(http.Status.internal_server_error, new_response('Failed to add package.')) + return app.status(.internal_server_error) } - if !res.added { - os.rm(pkg_path) or { app.lerror("Failed to remove download '$pkg_path': $err.msg()") } + app.linfo("Added '$res.name-$res.version' to '$repo (${res.archs.join(',')})'.") - app.lwarn("Duplicate package '$res.pkg.full_name()' in repo '$repo'.") - - return app.json(http.Status.bad_request, new_response('File already exists.')) - } - - app.linfo("Added '$res.pkg.full_name()' to repo '$repo ($res.pkg.info.arch)'.") - - return app.json(http.Status.ok, new_response('Package added successfully.')) + return app.json(.ok, new_data_response(res)) } From cf67b46df0cbd69444f2e6046937c74583021d84 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sun, 11 Sep 2022 21:28:37 +0200 Subject: [PATCH 4/7] feat(server): less verbose repo DELETE responses --- src/server/repo_remove.v | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/server/repo_remove.v b/src/server/repo_remove.v index fdc40e87..694f0854 100644 --- a/src/server/repo_remove.v +++ b/src/server/repo_remove.v @@ -1,8 +1,6 @@ module server import web -import net.http -import web.response { new_response } // delete_package tries to remove the given package. ['/:repo/:arch/:pkg'; auth; delete] @@ -10,17 +8,17 @@ fn (mut app App) delete_package(repo string, arch string, pkg string) web.Result res := app.repo.remove_pkg_from_arch_repo(repo, arch, pkg, true) or { app.lerror('Error while deleting package: $err.msg()') - return app.json(http.Status.internal_server_error, new_response('Failed to delete package.')) + return app.status(.internal_server_error) } if res { app.linfo("Removed package '$pkg' from '$repo/$arch'") - return app.json(http.Status.ok, new_response('Package removed.')) + return app.status(.ok) } else { app.linfo("Tried removing package '$pkg' from '$repo/$arch', but it doesn't exist.") - return app.json(http.Status.not_found, new_response('Package not found.')) + return app.status(.not_found) } } @@ -30,17 +28,17 @@ fn (mut app App) delete_arch_repo(repo string, arch string) web.Result { res := app.repo.remove_arch_repo(repo, arch) or { app.lerror('Error while deleting arch-repo: $err.msg()') - return app.json(http.Status.internal_server_error, new_response('Failed to delete arch-repo.')) + return app.status(.internal_server_error) } if res { - app.linfo("Removed '$repo/$arch'") + app.linfo("Removed arch-repo '$repo/$arch'") - return app.json(http.Status.ok, new_response('Arch-repo removed.')) + return app.status(.ok) } else { app.linfo("Tried removing '$repo/$arch', but it doesn't exist.") - return app.json(http.Status.not_found, new_response('Arch-repo not found.')) + return app.status(.not_found) } } @@ -50,16 +48,16 @@ fn (mut app App) delete_repo(repo string) web.Result { res := app.repo.remove_repo(repo) or { app.lerror('Error while deleting repo: $err.msg()') - return app.json(http.Status.internal_server_error, new_response('Failed to delete repo.')) + return app.status(.internal_server_error) } if res { - app.linfo("Removed '$repo'") + app.linfo("Removed repo '$repo'") - return app.json(http.Status.ok, new_response('Repo removed.')) + return app.status(.ok) } else { app.linfo("Tried removing '$repo', but it doesn't exist.") - return app.json(http.Status.not_found, new_response('Repo not found.')) + return app.status(.not_found) } } From 8a08788935bb9514007f41d17d18cb2d1b879ecf Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sun, 11 Sep 2022 21:50:29 +0200 Subject: [PATCH 5/7] feat(console): tabled outputs now optionally return without decorations --- src/console/console.v | 5 +++++ src/console/logs/logs.v | 20 +++++++++++++------- src/console/targets/targets.v | 12 +++++++++--- src/main.v | 7 +++++++ 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/console/console.v b/src/console/console.v index 7d782bac..caf4cca7 100644 --- a/src/console/console.v +++ b/src/console/console.v @@ -5,6 +5,11 @@ import strings import cli import os +// tabbed_table returns a simple textual table, with tabs as separators. +pub fn tabbed_table(data [][]string) string { + return data.map(it.join('\t')).join('\n') +} + // 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 diff --git a/src/console/logs/logs.v b/src/console/logs/logs.v index 0f023bc7..41830c25 100644 --- a/src/console/logs/logs.v +++ b/src/console/logs/logs.v @@ -133,7 +133,9 @@ pub fn cmd() cli.Command { ] } - list(conf, filter)? + raw := cmd.flags.get_bool('raw')? + + list(conf, filter, raw)? } }, cli.Command{ @@ -167,27 +169,31 @@ pub fn cmd() cli.Command { } // print_log_list prints a list of logs. -fn print_log_list(logs []BuildLog) ? { +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()]) - println(console.pretty_table(['id', 'target', 'start time', 'exit code'], data)?) + if raw { + println(console.tabbed_table(data)) + } else { + 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) ? { +fn list(conf Config, filter BuildLogFilter, raw bool) ? { c := client.new(conf.address, conf.api_key) logs := c.get_build_logs(filter)?.data - print_log_list(logs)? + 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) ? { +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 - print_log_list(logs)? + print_log_list(logs, raw)? } // info print the detailed info for a given build log. diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index 2784dbc3..198e0626 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -60,7 +60,9 @@ pub fn cmd() cli.Command { filter.repo = repo } - list(conf, filter)? + raw := cmd.flags.get_bool('raw')? + + list(conf, filter, raw)? } }, cli.Command{ @@ -193,12 +195,16 @@ 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) ? { +fn list(conf Config, filter TargetFilter, raw bool) ? { c := client.new(conf.address, conf.api_key) repos := c.get_targets(filter)? data := repos.map([it.id.str(), it.kind, it.url, it.repo]) - println(console.pretty_table(['id', 'kind', 'url', 'repo'], data)?) + if raw { + println(console.tabbed_table(data)) + } else { + println(console.pretty_table(['id', 'kind', 'url', 'repo'], data)?) + } } // add adds a new repository to the server's list. diff --git a/src/main.v b/src/main.v index 4ade9305..0e98bd26 100644 --- a/src/main.v +++ b/src/main.v @@ -24,6 +24,13 @@ fn main() { global: true default_value: [os.expand_tilde_to_home('~/.vieterrc')] }, + cli.Flag{ + flag: cli.FlagType.bool + name: 'raw' + abbrev: 'r' + description: 'Only output minimal information (no formatted tables, etc.)' + global: true + }, ] commands: [ server.cmd(), From fab8ca20b82e7f088d95efdc945a600050fefd6b Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 1 Oct 2022 16:02:43 +0200 Subject: [PATCH 6/7] cli: targets add now supports raw flag --- src/console/targets/targets.v | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index 198e0626..56400117 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -94,7 +94,9 @@ pub fn cmd() cli.Command { branch: cmd.flags.get_string('branch') or { '' } } - add(conf, t)? + raw := cmd.flags.get_bool('raw')? + + add(conf, t, raw)? } }, cli.Command{ @@ -208,11 +210,15 @@ fn list(conf Config, filter TargetFilter, raw bool) ? { } // add adds a new repository to the server's list. -fn add(conf Config, t &NewTarget) ? { +fn add(conf Config, t &NewTarget, raw bool) ? { c := client.new(conf.address, conf.api_key) res := c.add_target(t)? - println('Target added with id $res.data') + if raw { + println(res.data) + } else { + println('Target added with id $res.data') + } } // remove removes a repository from the server's list. From 851a446a954e619e474251533960b00a4b64bef3 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 1 Oct 2022 16:21:49 +0200 Subject: [PATCH 7/7] chore: updated changelog [CI SKIP] --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c875f2e7..60b70b60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * CLI commands for searching the AUR & directly adding packages * HTTP routes for removing packages, arch-repos & repos * All endpoints serving files now support HTTP byte range requests +* Better CLI UX + * When adding targets, the ID of the created target is returned + * The `-r` flag only shows raw data of action + * When adding a target, only ID is shown and not surrounding text + * Tabled output returns a tab-separated list (easy to script using + `cut`) ### Changed