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

This commit is contained in:
Jef Roosens 2022-04-06 16:52:31 +02:00
parent 09d0a40aae
commit 5b919ceeae
Signed by untrusted user: Jef Roosens
GPG key ID: B75D4F293C7052DB
9 changed files with 167 additions and 65 deletions

37
src/git/cli.v Normal file
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 Normal file
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) ?
}