2022-04-06 16:52:31 +02:00
|
|
|
module git
|
|
|
|
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
|
|
|
|
pub struct GitRepo {
|
2022-04-06 21:03:52 +02:00
|
|
|
pub mut:
|
|
|
|
// URL of the Git repository
|
|
|
|
url string
|
|
|
|
// Branch of the Git repository to use
|
|
|
|
branch string
|
|
|
|
// On which architectures the package is allowed to be built. In reality,
|
|
|
|
// this controls which builders will periodically build the image.
|
|
|
|
arch []string
|
2022-04-06 16:52:31 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 21:08:50 +02:00
|
|
|
// patch_from_params patches a GitRepo from a map[string]string, usually
|
|
|
|
// provided from a web.App's params
|
2022-04-06 21:03:52 +02:00
|
|
|
pub fn (mut r GitRepo) patch_from_params(params map[string]string) {
|
|
|
|
$for field in GitRepo.fields {
|
|
|
|
if field.name in params {
|
|
|
|
$if field.typ is string {
|
|
|
|
r.$(field.name) = params[field.name]
|
|
|
|
// This specific type check is needed for the compiler to ensure
|
|
|
|
// our types are correct
|
|
|
|
} $else $if field.typ is []string {
|
|
|
|
r.$(field.name) = params[field.name].split(',')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 21:08:50 +02:00
|
|
|
// read_repos reads the provided path & parses it into a map of GitRepo's.
|
2022-04-06 21:03:52 +02:00
|
|
|
pub fn read_repos(path string) ?map[string]GitRepo {
|
2022-04-06 16:52:31 +02:00
|
|
|
if !os.exists(path) {
|
|
|
|
mut f := os.create(path) ?
|
|
|
|
|
|
|
|
defer {
|
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
2022-04-06 21:03:52 +02:00
|
|
|
f.write_string('{}') ?
|
2022-04-06 16:52:31 +02:00
|
|
|
|
2022-04-06 21:03:52 +02:00
|
|
|
return {}
|
2022-04-06 16:52:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
content := os.read_file(path) ?
|
2022-04-06 21:03:52 +02:00
|
|
|
res := json.decode(map[string]GitRepo, content) ?
|
|
|
|
|
2022-04-06 16:52:31 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-04-06 21:08:50 +02:00
|
|
|
// write_repos writes a map of GitRepo's back to disk given the provided path.
|
2022-04-06 21:03:52 +02:00
|
|
|
pub fn write_repos(path string, repos &map[string]GitRepo) ? {
|
2022-04-06 16:52:31 +02:00
|
|
|
mut f := os.create(path) ?
|
|
|
|
|
|
|
|
defer {
|
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
value := json.encode(repos)
|
|
|
|
f.write_string(value) ?
|
|
|
|
}
|
2022-04-06 21:03:52 +02:00
|
|
|
|
2022-04-06 21:08:50 +02:00
|
|
|
// repo_from_params creates a GitRepo from a map[string]string, usually
|
|
|
|
// provided from a web.App's params
|
2022-04-06 21:03:52 +02:00
|
|
|
pub fn repo_from_params(params map[string]string) ?GitRepo {
|
|
|
|
mut repo := GitRepo{}
|
|
|
|
|
|
|
|
// If we're creating a new GitRepo, we want all fields to be present before
|
|
|
|
// "patching".
|
|
|
|
$for field in GitRepo.fields {
|
|
|
|
if field.name !in params {
|
|
|
|
return error('Missing parameter: ${field.name}.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
repo.patch_from_params(params)
|
|
|
|
|
|
|
|
return repo
|
|
|
|
}
|