From 5616e7a4e2ddab8ac7e4d2795b64bd12705c4af0 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 13 Jan 2022 19:20:14 +0100 Subject: [PATCH] Added function to read .PKGINFO into struct --- src/archive/archive.v | 2 +- src/main.v | 18 ++++----- src/repo/pkg.v | 90 +++++++++++++++++++++++++++++++++++++++++++ src/{ => repo}/repo.v | 1 + 4 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 src/repo/pkg.v rename src/{ => repo}/repo.v (98%) diff --git a/src/archive/archive.v b/src/archive/archive.v index ca911c7..011333f 100644 --- a/src/archive/archive.v +++ b/src/archive/archive.v @@ -2,7 +2,7 @@ module archive import os -pub fn get_pkg_info(pkg_path string) ?string { +pub fn pkg_info_string(pkg_path string) ?string { if !os.is_file(pkg_path) { return error("'$pkg_path' doesn't exist or isn't a file.") } diff --git a/src/main.v b/src/main.v index af6b06c..0b27736 100644 --- a/src/main.v +++ b/src/main.v @@ -54,7 +54,7 @@ fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? { } } -fn main() { +fn main2() { // Configure logger log_level_str := os.getenv_opt('LOG_LEVEL') or { 'WARN' } log_level := log.level_from_tag(log_level_str) or { @@ -102,11 +102,11 @@ fn main() { }, port) } -// fn main() { -// // archive.list_filenames() -// info := archive.get_pkg_info('test/jjr-joplin-desktop-2.6.10-4-x86_64.pkg.tar.zst') or { -// eprintln(err.msg) -// return -// } -// println(info) -// } +fn main() { + // archive.list_filenames() + info := repo.get_pkg_info('test/jjr-joplin-desktop-2.6.10-4-x86_64.pkg.tar.zst') or { + eprintln(err.msg) + return + } + println(info) +} diff --git a/src/repo/pkg.v b/src/repo/pkg.v new file mode 100644 index 0000000..b45839b --- /dev/null +++ b/src/repo/pkg.v @@ -0,0 +1,90 @@ +module repo + +import archive +import time + +struct PkgInfo { +mut: + // Single values + name string + base string + version string + description string + size i64 + csize i64 + url string + arch string + build_date i64 + packager string + md5sum string + sha256sum string + pgpsig string + pgpsigsize i64 + + // Array values + groups []string + licenses []string + replaces []string + depends []string + conflicts []string + provides []string + optdepends []string + makedepends []string + checkdepends []string +} + +pub fn get_pkg_info(pkg_path string) ?PkgInfo { + pkg_info_str := archive.pkg_info_string(pkg_path) ? + mut pkg_info := PkgInfo{} + + mut i := 0 + mut j := 0 + + // Iterate over the entire string + for line in pkg_info_str.split_into_lines() { + // Skip any comment lines + if line.starts_with('#') { + continue + } + parts := line.split_nth('=', 2) + + if parts.len < 2 { + return error('Invalid line detected.') + } + + value := parts[1].trim_space() + key := parts[0].trim_space() + + match key { + // Single values + 'pkgname' { pkg_info.name = value } + 'pkgbase' { pkg_info.base = value } + 'pkgver' { pkg_info.version = value } + 'pkgdesc' { pkg_info.description = value } + 'csize' { pkg_info.csize = value.int() } + 'size' { pkg_info.size = value.int() } + 'url' { pkg_info.url = value } + 'arch' { pkg_info.arch = value } + 'builddate' { pkg_info.build_date = value.int() } + 'packager' { pkg_info.packager = value } + 'md5sum' { pkg_info.md5sum = value } + 'sha256sum' { pkg_info.sha256sum = value } + 'pgpsig' { pkg_info.pgpsig = value } + 'pgpsigsize' { pkg_info.pgpsigsize = value.int() } + + // Array values + 'group' { pkg_info.groups << value } + 'license' { pkg_info.licenses << value } + 'replaces' { pkg_info.replaces << value } + 'depend' { pkg_info.depends << value } + 'conflict' { pkg_info.conflicts << value } + 'provides' { pkg_info.provides << value } + 'optdepend' { pkg_info.optdepends << value } + 'makedepend' { pkg_info.makedepends << value } + 'checkdepend' { pkg_info.checkdepends << value } + else { return error("Invalid key '$key'.") } + } + } + + return pkg_info +} diff --git a/src/repo.v b/src/repo/repo.v similarity index 98% rename from src/repo.v rename to src/repo/repo.v index 6b307e8..494f8c9 100644 --- a/src/repo.v +++ b/src/repo/repo.v @@ -1,6 +1,7 @@ module repo import os +import archive const pkgs_subpath = 'pkgs'