Switched to cli module; merged cli & vieter into single binary

main^2
Jef Roosens 2022-04-06 16:52:31 +02:00
parent 09d0a40aae
commit 5b919ceeae
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
9 changed files with 167 additions and 65 deletions

View File

@ -1,4 +1,4 @@
module main module build
import docker import docker
import encoding.base64 import encoding.base64
@ -7,6 +7,8 @@ import json
import server import server
import env import env
import net.http import net.http
import cli
import git
const container_build_dir = '/build' const container_build_dir = '/build'
@ -62,15 +64,13 @@ fn create_build_image() ?string {
return image.id return image.id
} }
fn build() ? { fn build(conf env.BuildConfig) ? {
conf := env.load<env.BuildConfig>() ?
// We get the repos list from the Vieter instance // We get the repos list from the Vieter instance
mut req := http.new_request(http.Method.get, '$conf.address/api/repos', '') ? mut req := http.new_request(http.Method.get, '$conf.address/api/repos', '') ?
req.add_custom_header('X-Api-Key', conf.api_key) ? req.add_custom_header('X-Api-Key', conf.api_key) ?
res := req.do() ? res := req.do() ?
repos := json.decode([]server.GitRepo, res.text) ? repos := json.decode([]git.GitRepo, res.text) ?
// No point in doing work if there's no repos present // No point in doing work if there's no repos present
if repos.len == 0 { if repos.len == 0 {

17
src/build/cli.v 100644
View File

@ -0,0 +1,17 @@
module build
import cli
import env
pub fn cmd() cli.Command {
return cli.Command{
name: 'build'
description: 'Run the build process.'
execute: fn (cmd cli.Command) ? {
conf := env.load<env.BuildConfig>() ?
build(conf) ?
}
}
}

View File

@ -56,15 +56,9 @@ fn get_env_var(field_name string) ?string {
} }
} }
// load<T> attempts to create the given type from environment variables. For fn load_in_place<T>(mut o T) ? {
// each field, the corresponding env var is its name in uppercase prepended
// with the hardcoded prefix. If this one isn't present, it looks for the env
// var with the file_suffix suffix.
pub fn load<T>() ?T {
res := T{}
$for field in T.fields { $for field in T.fields {
res.$(field.name) = get_env_var(field.name) or { o.$(field.name) = get_env_var(field.name) or {
// We use the default instead, if it's present // We use the default instead, if it's present
mut default := '' mut default := ''
@ -82,5 +76,20 @@ pub fn load<T>() ?T {
default default
} }
} }
}
// load<T> attempts to create the given type from environment variables. For
// each field, the corresponding env var is its name in uppercase prepended
// with the hardcoded prefix. If this one isn't present, it looks for the env
// var with the file_suffix suffix.
pub fn load<T>() ?T {
mut res := T{}
load_in_place<T>(mut res) ?
return res return res
} }
pub fn load_with_file<T>(path string) ?T {
}

37
src/git/cli.v 100644
View File

@ -0,0 +1,37 @@
module git
import cli
import env
import net.http
struct Config {
address string [required]
api_key string [required]
}
pub fn cmd() cli.Command {
return cli.Command{
name: 'repos'
description: 'Interact with the repos API.'
commands: [
cli.Command{
name: 'list'
description: 'List the current repos.'
execute: fn (cmd cli.Command) ? {
conf := env.load<Config>() ?
list(conf) ?
}
}
]
}
}
fn list(conf Config) ? {
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() ?
println(res.text)
}

40
src/git/git.v 100644
View File

@ -0,0 +1,40 @@
module git
import os
import json
pub struct GitRepo {
pub:
url string [required]
branch string [required]
}
pub fn read_repos(path string) ?[]GitRepo {
if !os.exists(path) {
mut f := os.create(path) ?
defer {
f.close()
}
f.write_string('[]') ?
return []
}
content := os.read_file(path) ?
res := json.decode([]GitRepo, content) ?
return res
}
pub fn write_repos(path string, repos []GitRepo) ? {
mut f := os.create(path) ?
defer {
f.close()
}
value := json.encode(repos)
f.write_string(value) ?
}

View File

@ -3,15 +3,33 @@ module main
import os import os
import server import server
import util import util
import cli
import env
import build
import git
fn main() { fn main() {
if os.args.len == 1 { mut app := cli.Command{
util.exit_with_message(1, 'No action provided.') name: 'vieter'
description: 'Arch repository server'
version: '0.1.0'
flags: [
cli.Flag{
flag: cli.FlagType.string
name: 'config-file'
abbrev: 'f'
description: 'Location of Vieter config file; defaults to ~/.vieterrc.'
global: true
default_value: [os.expand_tilde_to_home('~/.vieterrc')]
}
]
commands: [
server.cmd(),
build.cmd(),
git.cmd()
]
} }
match os.args[1] { app.setup()
'server' { server.server() ? } app.parse(os.args)
'build' { build() ? }
else { util.exit_with_message(1, 'Unknown action: ${os.args[1]}') }
}
} }

16
src/server/cli.v 100644
View File

@ -0,0 +1,16 @@
module server
import cli
import env
pub fn cmd() cli.Command {
return cli.Command{
name: 'server'
description: 'Start the Vieter server'
execute: fn (cmd cli.Command) ? {
conf := env.load<env.ServerConfig>() ?
server.server(conf) ?
}
}
}

View File

@ -3,44 +3,10 @@ module server
import web import web
import os import os
import json import json
import git
const repos_file = 'repos.json' const repos_file = 'repos.json'
pub struct GitRepo {
pub:
url string [required]
branch string [required]
}
fn read_repos(path string) ?[]GitRepo {
if !os.exists(path) {
mut f := os.create(path) ?
defer {
f.close()
}
f.write_string('[]') ?
return []
}
content := os.read_file(path) ?
res := json.decode([]GitRepo, content) ?
return res
}
fn write_repos(path string, repos []GitRepo) ? {
mut f := os.create(path) ?
defer {
f.close()
}
value := json.encode(repos)
f.write_string(value) ?
}
['/api/repos'; get] ['/api/repos'; get]
fn (mut app App) get_repos() web.Result { fn (mut app App) get_repos() web.Result {
if !app.is_authorized() { if !app.is_authorized() {
@ -48,7 +14,7 @@ fn (mut app App) get_repos() web.Result {
} }
repos := rlock app.git_mutex { repos := rlock app.git_mutex {
read_repos(app.conf.repos_file) or { git.read_repos(app.conf.repos_file) or {
app.lerror('Failed to read repos file.') app.lerror('Failed to read repos file.')
return app.server_error(500) return app.server_error(500)
@ -68,13 +34,13 @@ fn (mut app App) post_repo() web.Result {
return app.server_error(400) return app.server_error(400)
} }
new_repo := GitRepo{ new_repo := git.GitRepo{
url: app.query['url'] url: app.query['url']
branch: app.query['branch'] branch: app.query['branch']
} }
mut repos := rlock app.git_mutex { mut repos := rlock app.git_mutex {
read_repos(app.conf.repos_file) or { git.read_repos(app.conf.repos_file) or {
app.lerror('Failed to read repos file.') app.lerror('Failed to read repos file.')
return app.server_error(500) return app.server_error(500)
@ -91,7 +57,7 @@ fn (mut app App) post_repo() web.Result {
repos << new_repo repos << new_repo
lock app.git_mutex { lock app.git_mutex {
write_repos(app.conf.repos_file, repos) or { return app.server_error(500) } git.write_repos(app.conf.repos_file, repos) or { return app.server_error(500) }
} }
return app.ok('Repo added successfully.') return app.ok('Repo added successfully.')
@ -107,13 +73,13 @@ fn (mut app App) delete_repo() web.Result {
return app.server_error(400) return app.server_error(400)
} }
repo_to_remove := GitRepo{ repo_to_remove := git.GitRepo{
url: app.query['url'] url: app.query['url']
branch: app.query['branch'] branch: app.query['branch']
} }
mut repos := rlock app.git_mutex { mut repos := rlock app.git_mutex {
read_repos(app.conf.repos_file) or { git.read_repos(app.conf.repos_file) or {
app.lerror('Failed to read repos file.') app.lerror('Failed to read repos file.')
return app.server_error(500) return app.server_error(500)
@ -122,7 +88,7 @@ fn (mut app App) delete_repo() web.Result {
filtered := repos.filter(it != repo_to_remove) filtered := repos.filter(it != repo_to_remove)
lock app.git_mutex { lock app.git_mutex {
write_repos(app.conf.repos_file, filtered) or { return app.server_error(500) } git.write_repos(app.conf.repos_file, filtered) or { return app.server_error(500) }
} }
return app.ok('Repo removed successfully.') return app.ok('Repo removed successfully.')

View File

@ -6,6 +6,7 @@ import log
import repo import repo
import env import env
import util import util
import cli
const port = 8000 const port = 8000
@ -20,9 +21,7 @@ pub mut:
} }
// server starts the web server & starts listening for requests // server starts the web server & starts listening for requests
pub fn server() ? { pub fn server(conf env.ServerConfig) ? {
conf := env.load<env.ServerConfig>() ?
// Configure logger // Configure logger
log_level := log.level_from_tag(conf.log_level) or { log_level := log.level_from_tag(conf.log_level) or {
util.exit_with_message(1, 'Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.') util.exit_with_message(1, 'Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.')