From bd079645090280eecd5e827ddd03220c8f9f53dd Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Fri, 17 Jun 2022 13:56:38 +0200 Subject: [PATCH] feat(api): prevent invalid kind values --- src/client/targets.v | 8 ++++---- src/console/targets/targets.v | 13 ++++++++----- src/models/targets.v | 5 ++++- src/server/api_targets.v | 5 +++++ 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/client/targets.v b/src/client/targets.v index d7cc416..82c7878 100644 --- a/src/client/targets.v +++ b/src/client/targets.v @@ -41,11 +41,11 @@ pub fn (c &Client) get_target(id int) ?Target { } pub struct NewTarget { - kind string - url string + kind string + url string branch string - repo string - arch []string + repo string + arch []string } // add_target adds a new target to the server. diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index 71a5c92..a9934f1 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -67,7 +67,7 @@ pub fn cmd() cli.Command { name: 'add' required_args: 2 usage: 'url repo' - description: "Add a new target with the given URL & target repo." + description: 'Add a new target with the given URL & target repo.' flags: [ cli.Flag{ name: 'kind' @@ -79,14 +79,12 @@ pub fn cmd() cli.Command { name: 'branch' description: "Which branch to clone; only applies to kind 'git'." flag: cli.FlagType.string - } + }, ] execute: fn (cmd cli.Command) ? { config_file := cmd.flags.get_string('config-file')? conf := vconf.load(prefix: 'VIETER_', default_path: config_file)? - kind := cmd.flags.get_string('kind')? - t := NewTarget{ kind: cmd.flags.get_string('kind')? url: cmd.args[0] @@ -152,6 +150,11 @@ pub fn cmd() cli.Command { description: 'Cron schedule for repository.' flag: cli.FlagType.string }, + cli.Flag{ + name: 'kind' + description: 'Kind of target.' + flag: cli.FlagType.string + }, ] execute: fn (cmd cli.Command) ? { config_file := cmd.flags.get_string('config-file')? @@ -195,7 +198,7 @@ fn list(conf Config, filter TargetFilter) ? { 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)?) + println(console.pretty_table(['id', 'kind', 'url', 'repo'], data)?) } // add adds a new repository to the server's list. diff --git a/src/models/targets.v b/src/models/targets.v index b8fc88a..c8aa535 100644 --- a/src/models/targets.v +++ b/src/models/targets.v @@ -1,5 +1,7 @@ module models +pub const valid_kinds = ['git', 'url'] + pub struct TargetArch { pub: id int [primary; sql: serial] @@ -14,7 +16,7 @@ pub fn (gra &TargetArch) str() string { pub struct Target { pub mut: - id int [primary; sql: serial] + id int [primary; sql: serial] kind string [nonull] // If kind is git: URL of the Git repository // If kind is url: URL to PKGBUILD file @@ -35,6 +37,7 @@ pub mut: pub fn (gr &Target) str() string { mut parts := [ 'id: $gr.id', + 'kind: $gr.kind', 'url: $gr.url', 'branch: $gr.branch', 'repo: $gr.repo', diff --git a/src/server/api_targets.v b/src/server/api_targets.v index f807fe9..3867c94 100644 --- a/src/server/api_targets.v +++ b/src/server/api_targets.v @@ -52,6 +52,11 @@ fn (mut app App) v1_post_target() web.Result { return app.json(http.Status.bad_request, new_response(err.msg())) } + // Ensure someone doesn't submit an invalid kind + if new_repo.kind !in models.valid_kinds { + return app.json(http.Status.bad_request, new_response('Invalid kind.')) + } + app.db.add_target(new_repo) return app.json(http.Status.ok, new_response('Repo added successfully.'))