@[has_globals] module main import os import blip.vlib.blip import blip.vlib.blip.machine import alpm // config_path is the file read at startup, written in blip: a lisp dialect // embedded from V. the file is required, since the config carries parts of // vpac (the repos, for one); a setting no form of it names keeps the // default its __global is declared with below, which reset_config puts // back before every load pub const config_path = '/etc/vpac.d/config.lip' // the settings below were consts until the config existed. root_path and // db_path are joined onto file names directly, so they keep a trailing slash __global root_path = '/' __global db_path = '/var/lib/pacman/' __global arch = 'x86_64' __global parallel_downloads = 3 // the rank form: how many mirrors to probe at once (0 = unset, so the rank // parallelism falls back to the network's parallel) and how long one probe // may take __global rank_parallel = 0 __global rank_timeout = 10 // the pkgbuild form: the command that builds and how deep the clone is. // where it builds is path :dir. the build command is the system's makepkg, // which ships in the pacman package with the libalpm vpac runs on; :makepkg // names another // the build command is trusted text: it is spelled whole into a shell that // runs it in the cloned dir, first as the building user and through the // reviews of the pkgbuild for its content. vpac does not vet it — the config // is code the machine's user writes (like pacman.conf no more and no less), // and whoever controls a user's config already controls their shell __global pkgbuild_makepkg = 'makepkg -f' __global pkgbuild_depth = 1 // the unprivileged user libalpm downloads as, like DownloadUser in pacman.conf __global download_user = 'alpm' // the configured directories and lists. the defaults are the values the // hardcoded vpac used __global cfg_cachedirs = ['/var/cache/pacman/pkg/'] // pacman's default HookDir, beside the system one libalpm adds by itself __global cfg_hookdirs = ['/etc/pacman.d/hooks/'] // where pkgbuild packages are cloned and built (path :build): a relative // dir is taken under the invoking user's home, because makepkg runs as that user __global pkgbuild_dir = '.cache/vpac/pkgbuild' __global cfg_logfile = '' // the keyring signatures are checked against: pacman's GPGDir default, not // moved under a fake root (pacman keeps it as configuration). libalpm has // no default of its own, and without one gpgme falls back on root's own // ~/.gnupg, where no packager's key is __global cfg_gpgdir = '/etc/pacman.d/gnupg/' __global cfg_protocols = ['https'] // the signature level every sync db is registered with and packages are // checked against. libalpm's freshly calloc'd handle verifies nothing, so // vpac defaults to pacman's shipped SigLevel: a package and a database // signature are both verified when they are present, and a mismatching one // fails. override with (network :siglevel [...]) __global cfg_siglevel = alpm.SigLevel.package | .package_optional | .database | .database_optional __global cfg_hold = []string{} __global cfg_ignore = []string{} // the rest of pacman.conf's [options]: groups ignored as cfg_ignore's packages // are, files an upgrade or an install leaves alone, and the download timeout __global cfg_ignore_groups = []string{} __global cfg_noupgrade = []string{} __global cfg_noextract = []string{} __global cfg_disable_timeout = false // pacman.conf's DisableSandboxFilesystem and DisableSandboxSyscalls: libalpm // sandboxes its downloads and extracts, and this turns both parts off __global cfg_disable_sandbox = false // pacman.conf's UseSyslog: libalpm logs to syslog too, (path :syslog true) __global cfg_syslog = false // cfg_repos holds the repos the config defines __global cfg_repos = []CfgRepo{} // CfgRepo is one registered repo: a name, a kind ('sync', or 'pkgbuild' for // a site to build from), and the urls to fetch it from, where a url keeps // the $repo and $arch placeholders struct CfgRepo { name string kind string servers []string // what the db is used for, as pacman.conf's Usage; every use by default usage alpm.DBUsage = alpm.db_usage_all } // QuietConfigHost reads the config file from the real filesystem and throws // away anything it prints: a config is read for its definitions, and its // output has no place in vpac's own. it never reads stdin struct QuietConfigHost {} fn (h &QuietConfigHost) host_print(_ string) {} fn (h &QuietConfigHost) host_write(_ string) {} fn (h &QuietConfigHost) host_read_line() ?string { return none } fn (h &QuietConfigHost) host_load(path string) !string { return os.read_file(path) } // str_of reads one quoted string out of a config value // usage_of reads a repo's :usage, pacman.conf's Usage: one or more of sync, // search, install, upgrade and all fn usage_of(v blip.Value, what string) !alpm.DBUsage { names := match v.tag { .string { [v.as_string()] } else { strs_of(v, what)! } } if names.len == 0 { return error('config: ${what} needs at least one of sync, search, install, upgrade, all') } mut usage := unsafe { alpm.DBUsage(0) } for name in names { usage |= match name.to_lower() { 'sync' { alpm.DBUsage.sync } 'search' { alpm.DBUsage.search } 'install' { alpm.DBUsage.install } 'upgrade' { alpm.DBUsage.upgrade } 'all' { alpm.db_usage_all } else { return error('config: ${what} takes sync, search, install, upgrade and all, not "${name}"') } } } return usage } fn bool_of(v blip.Value, what string) !bool { if v.tag != .boolean { return error('config: ${what} must be true or false') } return v.as_bool() } fn str_of(v blip.Value, what string) !string { if v.tag != .string { return error('config: ${what} must be a quoted string') } return v.as_string() } // strs_of reads a string or an array of strings out of a config value fn strs_of(v blip.Value, what string) ![]string { mut out := []string{} match v.tag { .emptylist {} .string { out << v.as_string() } .vector, .array { for item in v.as_vector().data { out << str_of(item, what)! } } else { return error('config: ${what} must be a string or an array of strings') } } return out } // int_of reads one integer out of a config value fn int_of(v blip.Value, what string) !int { if v.tag != .integer { return error('config: ${what} must be an integer') } return int(v.as_int()) } // float_of reads a number out of a config value, an integer taken as one fn float_of(v blip.Value, what string) !f64 { return match v.tag { .float { v.as_float() } .integer { f64(v.as_int()) } else { error('config: ${what} must be a number') } } } // ratio_of reads a number between 0 and 1: a threshold, a completion fn ratio_of(v blip.Value, what string) !f64 { f := float_of(v, what)! if f < 0.0 || f > 1.0 { return error('config: ${what} must be between 0 and 1') } return f } // count_of reads an integer no smaller than min fn count_of(v blip.Value, what string, min int) !int { n := int_of(v, what)! if n < min { return error('config: ${what} must be at least ${min}') } return n } // siglevel_names are the names (network :siglevel) accepts, mapped onto // libalpm's SigLevel bits, plus what each one means const siglevel_names = { 'package': alpm.SigLevel.package 'package_optional': alpm.SigLevel.package_optional 'package_marginal_ok': alpm.SigLevel.package_marginal_ok 'package_unknown_ok': alpm.SigLevel.package_unknown_ok 'database': alpm.SigLevel.database 'database_optional': alpm.SigLevel.database_optional 'database_marginal_ok': alpm.SigLevel.database_marginal_ok 'database_unknown_ok': alpm.SigLevel.database_unknown_ok } // siglevel_of reads a SigLevel out of a config value: one name or an array // of names, e.g. (network :siglevel ['package' 'database_optional']) fn siglevel_of(v blip.Value, what string) !alpm.SigLevel { names := match v.tag { .string { [v.as_string()] } else { strs_of(v, what)! } } // an empty one verifies nothing: libalpm would take the silent answer // as handing everything over unsigned, which nobody ever means if names.len == 0 { return error('config: ${what} needs at least one of: ${siglevel_names.keys().join(', ')}') } mut level := unsafe { alpm.SigLevel(0) } for name in names { key := name.to_lower() bit := siglevel_names[key] or { return error('config: ${what} must be one or more of: ${siglevel_names.keys().join(', ')}') } level |= bit } return level } // dir_path checks a configured directory: names are joined onto it directly, // so it must not be empty and it has to end with a slash fn dir_path(what string, value string) !string { if value == '' { return error('config: ${what} must not be empty') } return if value.ends_with('/') { value } else { value + '/' } } // keyword_of reads the :key a config form applies to fn keyword_of(v blip.Value, form string) !string { if v.tag != .keyword { return error('config: ${form} expected a :key') } return v.as_string() } // keyword_args walks a config form's arguments as keyword, value pairs fn keyword_args(form string, args []blip.Value) !([]string, []blip.Value) { if args.len % 2 != 0 { return error('config: ${form} keys and values come in pairs') } mut keys := []string{} mut values := []blip.Value{} mut i := 0 for i < args.len { keys << keyword_of(args[i], form)! values << args[i + 1] i += 2 } return keys, values } // prim_path is the config's (path ...) form: root, db, log, gpg, cache, hook, // and :syslog true for pacman.conf's UseSyslog fn prim_path(args []blip.Value) !blip.Value { keys, values := keyword_args('path', args)! mut i := 0 for i < keys.len { match keys[i] { 'root' { root_path = dir_path('path :root', str_of(values[i], 'path :root')!)! } 'db' { db_path = dir_path('path :db', str_of(values[i], 'path :db')!)! } 'log' { cfg_logfile = str_of(values[i], 'path :log')! } 'syslog' { cfg_syslog = bool_of(values[i], 'path :syslog')! } 'gpg' { cfg_gpgdir = dir_path('path :gpg', str_of(values[i], 'path :gpg')!)! } 'cache' { dirs := strs_of(values[i], 'path :cache')! cfg_cachedirs = []string{} for dir in dirs { cfg_cachedirs << dir_path('path :cache', dir)! } } 'hook' { dirs := strs_of(values[i], 'path :hook')! cfg_hookdirs = []string{} for dir in dirs { cfg_hookdirs << dir_path('path :hook', dir)! } } 'build' { d := str_of(values[i], 'path :build')! if d == '' { return error('config: path :build must not be empty') } pkgbuild_dir = d } else { return error('config: path has no key :${keys[i]}') } } i++ } return blip.nil_value() } // prim_pkg is the config's (pkg ...) form: arch, hold, ignore, and pacman.conf's // IgnoreGroup, NoUpgrade and NoExtract as :ignoregroup, :noupgrade and // :noextract, each a list, e.g. (pkg :noextract ["usr/share/doc/*"]) fn prim_pkg(args []blip.Value) !blip.Value { keys, values := keyword_args('pkg', args)! mut i := 0 for i < keys.len { match keys[i] { 'arch' { a := str_of(values[i], 'pkg :arch')! // it fills the $arch of every server url and is the one // architecture libalpm installs for: one plain word if a == '' || a.contains('/') || a.fields().len != 1 || a.fields()[0] != a { return error('config: pkg :arch must be one plain word, such as x86_64') } arch = a } 'hold' { cfg_hold = strs_of(values[i], 'pkg :hold')! } 'ignore' { cfg_ignore = strs_of(values[i], 'pkg :ignore')! } 'ignoregroup' { cfg_ignore_groups = strs_of(values[i], 'pkg :ignoregroup')! } 'noupgrade' { cfg_noupgrade = strs_of(values[i], 'pkg :noupgrade')! } 'noextract' { cfg_noextract = strs_of(values[i], 'pkg :noextract')! } else { return error('config: pkg has no key :${keys[i]}') } } i++ } return blip.nil_value() } // prim_network is the config's (network ...) form: parallel, protocols, user, // siglevel, :disable-timeout true for pacman.conf's DisableDownloadTimeout, and // :disable-sandbox true for its DisableSandbox fn prim_network(args []blip.Value) !blip.Value { keys, values := keyword_args('network', args)! mut i := 0 for i < keys.len { match keys[i] { 'parallel' { n := int_of(values[i], 'network :parallel')! if n < 1 { return error('config: network :parallel must be at least 1') } parallel_downloads = n } 'protocols' { protos := strs_of(values[i], 'network :protocols')! if protos.len == 0 { return error('config: network :protocols needs at least one, https or http') } // a scheme no mirror is listed under would filter every // mirror out without a word for proto in protos { if proto !in ['https', 'http'] { return error('config: network :protocols takes https and http, not "${proto}"') } } cfg_protocols = protos.clone() } 'user' { download_user = str_of(values[i], 'network :user')! } 'siglevel' { cfg_siglevel = siglevel_of(values[i], 'network :siglevel')! } 'disable-timeout' { cfg_disable_timeout = bool_of(values[i], 'network :disable-timeout')! } 'disable-sandbox' { cfg_disable_sandbox = bool_of(values[i], 'network :disable-sandbox')! } else { return error('config: network has no key :${keys[i]}') } } i++ } return blip.nil_value() } // prim_rank is the config's (rank ...) form: parallel and timeout for the // mirror ranking. the parallelism falls back to the network's parallel when // the rank form does not set its own. :top is how many mirrors are answered, // :completion (0 to 1) and :max-delay (seconds) what a mirror must report to // be probed, :candidates how many get the speed test, :retest how many of // the fastest are timed twice, :dir where rank -w writes fn prim_rank(args []blip.Value) !blip.Value { keys, values := keyword_args('rank', args)! mut i := 0 for i < keys.len { match keys[i] { 'parallel' { n := int_of(values[i], 'rank :parallel')! if n < 1 { return error('config: rank :parallel must be at least 1') } rank_parallel = n } 'timeout' { n := int_of(values[i], 'rank :timeout')! if n < 1 { return error('config: rank :timeout must be at least 1') } rank_timeout = n } 'top' { rank_top = count_of(values[i], 'rank :top', 1)! } 'dir' { d := str_of(values[i], 'rank :dir')! if !d.starts_with('/') { return error('config: rank :dir must be an absolute path') } rank_dir = d.trim_right('/') } 'candidates' { rank_speed_candidates = count_of(values[i], 'rank :candidates', 1)! } 'retest' { rank_retest = count_of(values[i], 'rank :retest', 0)! } 'completion' { rank_min_completion = ratio_of(values[i], 'rank :completion')! } 'max-delay' { rank_max_delay = i64(count_of(values[i], 'rank :max-delay', 0)!) } else { return error('config: rank has no key :${keys[i]}') } } i++ } return blip.nil_value() } // prim_repo is the config's (repo ...) form: a repo's name, its type ('sync', // or 'pkgbuild', a site to build from) and the mirrors it is fetched // from, with $repo and $arch left as placeholders // :usage is pacman.conf's Usage: a list of sync, search, install, upgrade // and all; every use when it is left out fn prim_repo(args []blip.Value) !blip.Value { keys, values := keyword_args('repo', args)! mut repo := CfgRepo{ kind: 'sync' } mut i := 0 for i < keys.len { match keys[i] { 'name' { rname := str_of(values[i], 'repo :name')! repo = CfgRepo{ ...repo name: rname } } 'type' { rkind := str_of(values[i], 'repo :type')! if rkind !in ['sync', 'pkgbuild'] { return error("config: repo :type is 'sync' or 'pkgbuild', not '${rkind}'") } repo = CfgRepo{ ...repo kind: rkind } } 'src' { rservers := strs_of(values[i], 'repo :src')! if rservers.len == 0 { return error('config: repo :src needs at least one url') } for server in rservers { if server == '' { return error('config: repo :src has an empty url in it') } } repo = CfgRepo{ ...repo servers: rservers } } 'usage' { rusage := usage_of(values[i], 'repo :usage')! repo = CfgRepo{ ...repo usage: rusage } } else { return error('config: repo has no key :${keys[i]}') } } i++ } if repo.name == '' { return error('config: repo needs a :name') } // libalpm refuses a second db of a name, and 'local' is its own db's: // said here, by name, instead of late and from inside a mode if repo.name == 'local' { return error('config: a repo cannot be named local, the installed packages are') } if cfg_repos.any(it.name == repo.name) { return error('config: repo ${repo.name} is defined twice') } if repo.kind == 'sync' && repo.servers.len == 0 { return error('config: a sync repo takes at least one :src') } if repo.kind == 'pkgbuild' && repo.servers.len != 1 { return error('config: a pkgbuild repo takes one :src, the site to build from') } cfg_repos << repo return blip.nil_value() } // pkgbuild_repos are the configured pkgbuild repos, in config order. the // --pkgbuild flags search and build from all of them, each under its own name fn pkgbuild_repos() []CfgRepo { return cfg_repos.filter(it.kind == 'pkgbuild') } // prim_pkgbuild is the config's (pkgbuild ...) form: makepkg, depth, and // timeout, the seconds one lookup of a pkgbuild repo may take. where the // build happens is path :build fn prim_pkgbuild(args []blip.Value) !blip.Value { keys, values := keyword_args('pkgbuild', args)! mut i := 0 for i < keys.len { match keys[i] { 'makepkg' { m := str_of(values[i], 'pkgbuild :makepkg')! if m == '' { return error('config: pkgbuild :makepkg must not be empty') } pkgbuild_makepkg = m } 'depth' { n := int_of(values[i], 'pkgbuild :depth')! if n < 1 { return error('config: pkgbuild :depth must be at least 1') } pkgbuild_depth = n } 'timeout' { pkgbuild_rpc_timeout = count_of(values[i], 'pkgbuild :timeout', 1)! } else { return error('config: pkgbuild has no key :${keys[i]}') } } i++ } return blip.nil_value() } // option_flags are the mode flags (options ...) can turn on for every run, // by the config's key: each names the field the modes' flag structs keep it // in, so a mode that has no such flag is simply not touched const option_flags = { 'noconfirm': 'noconfirm' 'nopkgbuildconfirm': 'no_pkgbuild_confirm' 'needed': 'needed' 'no-upgrade-deps': 'no_upgrade_deps' 'quiet': 'quiet' 'keep-current': 'keep_current' 'fuzzy': 'fuzzy' 'disable-timeout': 'disable_timeout' } // opt_flags holds the fields of option_flags the config turned on; // opt_color is options :color, empty when the config names none __global opt_flags = map[string]bool{} __global opt_color = '' // prim_options is the config's (options ...) form: root, the command vpac // gets root with (privilege.v); color, the --color a run starts from; // verbose, the verbose log of a run's housekeeping failures (--verbose); // sync-age, the hours after which the sync dbs are called stale (0: never); // suggest, how close (0 to 1) a name must be to be offered for a typo; and // the mode flags of option_flags, true or false fn prim_options(args []blip.Value) !blip.Value { keys, values := keyword_args('options', args)! mut i := 0 for i < keys.len { match keys[i] { 'root' { r := str_of(values[i], 'options :root')! if r.trim_space() == '' { return error('config: options :root must not be empty') } root_provider = r } 'color' { c := str_of(values[i], 'options :color')! if c !in ['auto', 'always', 'never'] { return error('config: options :color takes auto, always or never, not "${c}"') } opt_color = c } 'sync-age' { // hours; 0 never warns sync_max_age = i64(count_of(values[i], 'options :sync-age', 0)!) * 3600 } 'suggest' { suggest_thold = ratio_of(values[i], 'options :suggest')! } 'verbose' { // the verbose log for every run, as --verbose for one cfg_verbose = bool_of(values[i], 'options :verbose')! } else { field := option_flags[keys[i]] or { return error('config: options has no key :${keys[i]}') } // only what is on is held: a false takes back an earlier // true (an --eval over the file) and leaves no entry behind if bool_of(values[i], 'options :${keys[i]}')! { opt_flags[field] = true } else { opt_flags.delete(field) } } } i++ } return blip.nil_value() } // prim_search is the config's (search ...) form: fields, the columns of a // listing when --fields names none, and thold, the fuzzy threshold fn prim_search(args []blip.Value) !blip.Value { keys, values := keyword_args('search', args)! mut i := 0 for i < keys.len { match keys[i] { 'fields' { names := strs_of(values[i], 'search :fields')! if names.len == 0 { return error('config: search :fields needs at least one field') } cfg_search_fields = parse_fields(names.join(','), []string{}) or { return error('config: search :fields: ${err.msg()}') } } 'thold' { cfg_search_thold = ratio_of(values[i], 'search :thold')! } else { return error('config: search has no key :${keys[i]}') } } i++ } return blip.nil_value() } // prim_clean is the config's (clean ...) form: keep, the versions of every // package the cache keeps when -k names none fn prim_clean(args []blip.Value) !blip.Value { keys, values := keyword_args('clean', args)! mut i := 0 for i < keys.len { match keys[i] { 'keep' { cfg_clean_keep = count_of(values[i], 'clean :keep', 0)! } else { return error('config: clean has no key :${keys[i]}') } } i++ } return blip.nil_value() } // prim_tree is the config's (tree ...) form: depth, how deep a tree is // drawn when -d names none fn prim_tree(args []blip.Value) !blip.Value { keys, values := keyword_args('tree', args)! mut i := 0 for i < keys.len { match keys[i] { 'depth' { cfg_tree_depth = count_of(values[i], 'tree :depth', 0)! } else { return error('config: tree has no key :${keys[i]}') } } i++ } return blip.nil_value() } // prim_color is the config's (color ...) form, the theme: each key is a role // of default_theme, each value one style or a list of them out of // style_names, e.g. (color :version "cyan" :warning ["bold" "magenta"]) fn prim_color(args []blip.Value) !blip.Value { keys, values := keyword_args('color', args)! mut i := 0 for i < keys.len { if keys[i] !in theme { return error('config: color has no role :${keys[i]}, the roles are: ${default_theme().keys().join(', ')}') } styles := strs_of(values[i], 'color :${keys[i]}')! for style in styles { if style !in style_names { return error('config: color :${keys[i]} takes ${style_names.join(', ')}, not "${style}"') } } theme[keys[i]] = styles.clone() i++ } return blip.nil_value() } // prim_mirrorlist is the config's (mirrorlist ...) form: an os for the // ranker, new or one of its own redefined. :name is what rank is asked for // (and what /etc/os-release is matched against), :repo the repo whose db the // speed test pulls, and the mirrors come from one of :status, a mirror // status json api as arch publishes, or :list, a Server = mirrorlist at a // url or an absolute path fn prim_mirrorlist(args []blip.Value) !blip.Value { keys, values := keyword_args('mirrorlist', args)! mut name := '' mut status := '' mut list := '' mut repo := '' mut i := 0 for i < keys.len { match keys[i] { 'name' { name = str_of(values[i], 'mirrorlist :name')! } 'status' { status = str_of(values[i], 'mirrorlist :status')! } 'list' { list = str_of(values[i], 'mirrorlist :list')! } 'repo' { repo = str_of(values[i], 'mirrorlist :repo')! } else { return error('config: mirrorlist has no key :${keys[i]}') } } i++ } if name == '' || name.contains('/') || name.starts_with('.') || name.starts_with('-') || name.fields().len != 1 || name.fields()[0] != name { return error('config: mirrorlist needs a :name, one plain word') } if repo == '' { return error('config: mirrorlist ${name} needs a :repo, the repo whose db is timed') } if (status == '') == (list == '') { return error('config: mirrorlist ${name} takes one of :status and :list') } if status != '' && !status.starts_with('https://') && !status.starts_with('http://') { return error('config: mirrorlist ${name} :status must be an http or https url') } if list != '' && !list.starts_with('https://') && !list.starts_with('http://') && !list.starts_with('/') { return error('config: mirrorlist ${name} :list must be a url or an absolute path') } rank_oses[name] = RankOs{ status_json: status mirrorlist: list repo: repo } return blip.nil_value() } // cell_of reads one piece of a bar graphic: text of exactly one rune, or of // at most one when it may be left out fn cell_of(v blip.Value, what string, may_be_empty bool) !string { text := str_of(v, what)! n := text.runes().len if n > 1 || (n == 0 && !may_be_empty) { return error('config: ${what} must be one character') } return text } // prim_progress is the config's (progress ...) form, the bars: style // ('classic' or 'block'); the graphic's pieces start, end, fill, head and // empty; length in cells (0 fills the terminal); percent, eta, elapsed and // rate, true or false; delay, the milliseconds between redraws fn prim_progress(args []blip.Value) !blip.Value { keys, values := keyword_args('progress', args)! mut i := 0 for i < keys.len { what := 'progress :${keys[i]}' match keys[i] { 'style' { s := str_of(values[i], what)! if s !in ['classic', 'block'] { return error('config: ${what} is classic or block, not "${s}"') } cfg_progress.style = s } 'start', 'end' { cfg_progress.parts[keys[i]] = str_of(values[i], what)! } 'fill', 'empty' { cfg_progress.parts[keys[i]] = cell_of(values[i], what, false)! } 'head' { cfg_progress.parts[keys[i]] = cell_of(values[i], what, true)! } 'length' { cfg_progress.length = count_of(values[i], what, 0)! } 'percent' { cfg_progress.percent = bool_of(values[i], what)! } 'eta' { cfg_progress.eta = bool_of(values[i], what)! } 'elapsed' { cfg_progress.elapsed = bool_of(values[i], what)! } 'rate' { cfg_progress.rate = bool_of(values[i], what)! } 'delay' { cfg_progress.delay = count_of(values[i], what, 1)! } else { return error('config: progress has no key :${keys[i]}') } } i++ } return blip.nil_value() } // prim_spinner is the config's (spinner ...) form: frames, a set by name // (spinner_sets) or a list of frames of one's own; interval, the // milliseconds a frame stays; done, what a finished step shows in place of // the animation; elapsed, whether the time taken is shown fn prim_spinner(args []blip.Value) !blip.Value { keys, values := keyword_args('spinner', args)! mut i := 0 for i < keys.len { what := 'spinner :${keys[i]}' match keys[i] { 'frames' { if values[i].tag == .string { name := values[i].as_string() cfg_progress.frames = spinner_frames_named(name) or { return error('config: ${what} knows ${spinner_sets.join(', ')}, not "${name}"; a list gives frames of your own') } } else { frames := strs_of(values[i], what)! if frames.len == 0 { return error('config: ${what} needs at least one frame') } cfg_progress.frames = frames.clone() } } 'interval' { cfg_progress.interval = count_of(values[i], what, 1)! } 'done' { cfg_progress.done = str_of(values[i], what)! } 'elapsed' { cfg_progress.timed = bool_of(values[i], what)! } else { return error('config: spinner has no key :${keys[i]}') } } i++ } return blip.nil_value() } // prim_array_append is (array-append! dst src): every element of src pushed // onto dst. it is std.lip's, provided by the host so the config's mirror lists // are fillable without vpac loading std.lip fn prim_array_append(args []blip.Value) !blip.Value { if args.len != 2 { return error('config: array-append! needs an array and an array of values') } if args[0].tag !in [.vector, .array] { return error('config: array-append! the first argument must be an array') } match args[1].tag { .vector, .array { for item in args[1].as_vector().data { args[0].as_vector().push(item) } } .emptylist {} else { return error('config: array-append! the second argument must be an array') } } return args[0] } // reset_config puts every config global back at its declared default, the // values the hardcoded vpac used. load_config runs it first, so a second // load in one process neither duplicates the repos nor keeps settings the // new file does not name; a failed load can leave values behind, and the // next load starts clean again fn reset_config() { root_path = '/' root_provider = 'sudo' opt_flags = map[string]bool{} opt_color = '' cfg_verbose = false sync_max_age = i64(24 * 60 * 60) suggest_thold = 0.85 pkgbuild_rpc_timeout = 10 rank_min_completion = 1.0 rank_max_delay = i64(86400) rank_speed_candidates = 40 rank_retest = 5 rank_top = 10 cfg_search_fields = search_default_fields.clone() cfg_search_thold = 0.9 cfg_clean_keep = 0 cfg_tree_depth = 0 theme = default_theme() rank_oses = default_rank_oses() rank_dir = '/etc/vpac.d/mirrors' cfg_progress = ProgressCfg{} db_path = '/var/lib/pacman/' arch = 'x86_64' parallel_downloads = 3 rank_parallel = 0 rank_timeout = 10 pkgbuild_makepkg = 'makepkg -f' pkgbuild_depth = 1 download_user = 'alpm' pkgbuild_dir = '.cache/vpac/pkgbuild' cfg_cachedirs = ['/var/cache/pacman/pkg/'] cfg_hookdirs = ['/etc/pacman.d/hooks/'] cfg_logfile = '' cfg_gpgdir = '/etc/pacman.d/gnupg/' cfg_protocols = ['https'] cfg_siglevel = alpm.SigLevel.package | .package_optional | .database | .database_optional cfg_hold = []string{} cfg_ignore = []string{} cfg_ignore_groups = []string{} cfg_noupgrade = []string{} cfg_noextract = []string{} cfg_disable_timeout = false cfg_disable_sandbox = false cfg_syslog = false cfg_repos = []CfgRepo{} } // load_config reads the config file -- the one --config names, else // config_path -- and evaluates it in a machine that provides the config // forms. the file is required: vpac has no defaults to fall back to once its // repos, paths and mirror lists live in the config. the --eval snippets run // after it, in the same machine, so they can extend or override the file for // this run. a file that does not parse, or a form with the wrong keys, stops // vpac, because running on silently could act on the wrong database fn load_config(chosen string, evals []string) ! { reset_config() path := if chosen != '' { chosen } else { config_path } if !os.exists(path) { return error('${path} is required: write it, starting with (require ...) for each part') } mut m := machine.new_standalone(&QuietConfigHost{}) m.prims['path'] = prim_path m.prims['pkg'] = prim_pkg m.prims['network'] = prim_network m.prims['rank'] = prim_rank m.prims['pkgbuild'] = prim_pkgbuild m.prims['options'] = prim_options m.prims['search'] = prim_search m.prims['clean'] = prim_clean m.prims['tree'] = prim_tree m.prims['color'] = prim_color m.prims['mirrorlist'] = prim_mirrorlist m.prims['progress'] = prim_progress m.prims['spinner'] = prim_spinner m.prims['repo'] = prim_repo m.prims['array-append!'] = prim_array_append m.load(path) or { return error('config: ${err.msg()}') } for code in evals { m.run_str(code) or { return error('config --eval: ${err.msg()}') } } } // check_hold refuses to remove a package the config holds, as pacman does for // HoldPkg. libalpm has no option for it, so it is a frontend check fn check_hold(names []string) ! { for name in names { if name in cfg_hold { return error('${name} is held by the config: remove it from pkg :hold first') } } }