module main import alpm // Target is a target as the command line spells it, parted the way pacman's // process_target parts one: 'repo/name' names the db to look in, and a bare // name looks everywhere. the name may be a dependency string ('bash>=5') or // something a package provides ('sh') struct Target { repo string name string } // parse_target splits the db off a target. a slash that leads is no prefix, // as in pacman; one that ends the target leaves an empty name, which // nothing is called fn parse_target(target string) Target { slash := target.index('/') or { -1 } if slash <= 0 { return Target{ name: target } } return Target{ repo: target[..slash] name: target[slash + 1..] } } // local_target_name is a target of a mode that reads the installed packages // alone: pacman's -R and -Q take 'local/' off and nothing else fn local_target_name(target string) string { if target.starts_with('local/') && target.len > 6 { return target[6..] } return target } // scope_dbs is the sync dbs a target is looked up in: all of them, or the // one its prefix names. the prefix is a scope, not decoration: a name the // config does not carry is pacman's 'database not found', never a search of // everything with the prefix dropped fn scope_dbs(dbs []&alpm.DB, repo string) ![]&alpm.DB { if repo == '' { return dbs } scoped := dbs.filter(it.get_name() == repo) if scoped.len == 0 { return error('database not found: ${repo}') } return scoped } // has_constraint is whether a target asks for a version: a dependency // string with one of libalpm's operators fn has_constraint(name string) bool { return name.contains_any('<>=') } // find_local_target finds an installed package by name. a version // constraint is answered by what satisfies it; providers says a bare name // may be something a package provides too, which the modes that only read // take and the ones that write do not: 'vpac r sh' must not come to mean // bash fn find_local_target(pkgcache &alpm.List, name string, providers bool) ?&alpm.Package { if p := pkgcache.pkg_find(name) { return p } if providers || has_constraint(name) { return pkgcache.find_satisfier(name) } return none } // find_sync_target finds a target in the dbs given, already scoped: the // package of that name, or else what libalpm resolves the dependency string // to, as pacman's process_targname asks alpm_find_dbs_satisfier — a version // constraint, or a provider, with libalpm's own questions about ignored // packages and several providers. named says the db was named with the // target, which makes it one to install from for this lookup whatever its // usage, as in pacman fn find_sync_target(handle &alpm.Handle, dbs []&alpm.DB, name string, named bool) ?&alpm.Package { if name == '' { return none } if p := find_repo_pkg(dbs, name) { return p } mut list := unsafe { &alpm.List(nil) } for db in dbs { list = list.add(db) } defer { list.free() } if !named { return handle.find_dbs_satisfier(list, name) } mut restore := map[int]alpm.DBUsage{} for i, db in dbs { usage := db.get_usage() or { continue } if !usage.has(.install) { db.set_usage(usage | .install) or { continue } restore[i] = usage } } found := handle.find_dbs_satisfier(list, name) for i, usage in restore { dbs[i].set_usage(usage) or { warn('could not put the usage of ${dbs[i].get_name()} back: ${err.msg()}') } } return found } // GroupMember is one package of a sync group, with the db that carries it struct GroupMember { repo string pkg &alpm.Package } // sync_group_members are the packages of a group across the dbs, in db // order; a name a later db repeats is the earlier db's, as libalpm's // alpm_find_group_pkgs has it. found says a db had the group at all fn sync_group_members(dbs []&alpm.DB, name string) (bool, []GroupMember) { mut found := false mut out := []GroupMember{} mut seen := map[string]bool{} for db in dbs { group := db.get_group(name) or { continue } found = true for pkg in group.packages.pkgs() { if seen[pkg.get_name()] { continue } seen[pkg.get_name()] = true out << GroupMember{ repo: db.get_name() pkg: pkg } } } return found, out } // select_members asks which members of a group go in, pacman's // multiselect: the members are listed numbered, on stderr with the question // they lead into (a frontend gets them as a listing), and the answer picks // among them. a --print preview lists and asks nothing: every member fn select_members(mut ui TransUI, group string, members []GroupMember, ask bool) []GroupMember { if !ask || members.len == 0 { return members } ui.pause() if ipc_on() { mut listing := Listing{ fields: ['source', 'name', 'version'] } for m in members { listing.add_member(m.pkg, m.repo, group) } listing.print() } else { count := if members.len == 1 { '1 member' } else { '${members.len} members' } eprintln(erole('heading', '${count} in group ${group}:')) mut rows := []Row{} for i, m in members { rows << row('${i + 1})', m.repo, m.pkg.get_name(), m.pkg.get_version()) } eprintln(table_text(rows, ' ', ' ')) } picked := ui.pick('enter a selection', members.len) mut out := []GroupMember{} for i, m in members { if picked[i] { out << m } } return out }