vieter/src/repo.v

67 lines
1.6 KiB
Coq
Raw Normal View History

2022-01-09 22:30:07 +01:00
module repo
import os
const pkgs_subpath = 'pkgs'
// Dummy struct to work around the fact that you can only share structs, maps &
// arrays
pub struct Dummy {
x int
}
2022-01-13 21:47:14 +01:00
// This struct manages a single repository.
2022-01-11 14:40:25 +01:00
pub struct Repo {
2022-01-11 16:37:49 +01:00
mut:
mutex shared Dummy
pub:
2022-01-13 21:47:14 +01:00
// Where to store repository files; should exist
repo_dir string [required]
// Where to find packages; packages are expected to all be in the same directory
pkg_dir string [required]
}
2022-01-14 22:46:04 +01:00
// contains returns whether the repository contains the given package.
2022-01-13 21:47:14 +01:00
pub fn (r &Repo) contains(pkg string) bool {
return os.exists(os.join_path(r.repo_dir, 'files', pkg))
}
2022-01-14 22:46:04 +01:00
// add adds the given package to the repo. If false, the package was already
2022-01-13 21:47:14 +01:00
// present in the repository.
pub fn (r &Repo) add(pkg string) ?bool {
return false
}
2022-01-14 22:46:04 +01:00
// generate re-generates the db & files archives.
2022-01-13 21:47:14 +01:00
fn (r &Repo) genenerate() ? {
}
2022-01-14 22:46:04 +01:00
// pkg_path returns path to the given package, prepended with the repo's path.
2022-01-11 16:37:49 +01:00
pub fn (r &Repo) pkg_path(pkg string) string {
2022-01-13 21:47:14 +01:00
return os.join_path_single(r.pkg_dir, pkg)
}
2022-01-14 22:46:04 +01:00
// exists checks whether a package file exists
2022-01-11 16:37:49 +01:00
pub fn (r &Repo) exists(pkg string) bool {
return os.exists(r.pkg_path(pkg))
}
2022-01-14 22:46:04 +01:00
// db_path returns the full path to the database file
2022-01-11 16:37:49 +01:00
pub fn (r &Repo) db_path() string {
2022-01-13 21:47:14 +01:00
return os.join_path_single(r.repo_dir, 'repo.tar.gz')
2022-01-11 14:40:25 +01:00
}
2022-01-14 22:46:04 +01:00
// add_package adds a package to the repository
2022-01-11 16:37:49 +01:00
pub fn (r &Repo) add_package(pkg_path string) ? {
mut res := os.Result{}
lock r.mutex {
res = os.execute("repo-add '$r.db_path()' '$pkg_path'")
}
2022-01-09 22:30:07 +01:00
if res.exit_code != 0 {
println(res.output)
return error('repo-add failed.')
}
}