| 1 | module main |
| 2 | |
| 3 | import nspkg |
| 4 | |
| 5 | // Target is a target as the command line spells it, parted the way pacman's |
| 6 | // process_target parts one: 'repo/name' names the db to look in, and a bare |
| 7 | // name looks everywhere. the name may be a dependency string ('bash>=5') or |
| 8 | // something a package provides ('sh') |
| 9 | struct Target { |
| 10 | repo string |
| 11 | name string |
| 12 | } |
| 13 | |
| 14 | // parse_target splits the db off a target. a slash that leads is no prefix, |
| 15 | // as in pacman; one that ends the target leaves an empty name, which |
| 16 | // nothing is called |
| 17 | fn parse_target(target string) Target { |
| 18 | slash := target.index('/') or { -1 } |
| 19 | if slash <= 0 { |
| 20 | return Target{ |
| 21 | name: target |
| 22 | } |
| 23 | } |
| 24 | return Target{ |
| 25 | repo: target[..slash] |
| 26 | name: target[slash + 1..] |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // local_target_name is a target of a mode that reads the installed packages |
| 31 | // alone: pacman's -R and -Q take 'local/' off and nothing else |
| 32 | fn local_target_name(target string) string { |
| 33 | if target.starts_with('local/') && target.len > 6 { |
| 34 | return target[6..] |
| 35 | } |
| 36 | return target |
| 37 | } |
| 38 | |
| 39 | // scope_dbs is the sync dbs a target is looked up in: all of them, or the |
| 40 | // one its prefix names. the prefix is a scope, not decoration: a name the |
| 41 | // config does not carry is pacman's 'database not found', never a search of |
| 42 | // everything with the prefix dropped |
| 43 | fn scope_dbs(dbs []&nspkg.DB, repo string) ![]&nspkg.DB { |
| 44 | if repo == '' { |
| 45 | return dbs |
| 46 | } |
| 47 | scoped := dbs.filter(it.get_name() == repo) |
| 48 | if scoped.len == 0 { |
| 49 | return error('database not found: ${repo}') |
| 50 | } |
| 51 | return scoped |
| 52 | } |
| 53 | |
| 54 | // has_constraint is whether a target asks for a version: a dependency |
| 55 | // string with one of libalpm's operators |
| 56 | fn has_constraint(name string) bool { |
| 57 | return name.contains_any('<>=') |
| 58 | } |
| 59 | |
| 60 | // find_local_target finds an installed package by name. a version |
| 61 | // constraint is answered by what satisfies it; providers says a bare name |
| 62 | // may be something a package provides too, which the modes that only read |
| 63 | // take and the ones that write do not: 'vpac r sh' must not come to mean |
| 64 | // bash |
| 65 | fn find_local_target(pkgcache &nspkg.List, name string, providers bool) ?&nspkg.Package { |
| 66 | if p := pkgcache.pkg_find(name) { |
| 67 | return p |
| 68 | } |
| 69 | if providers || has_constraint(name) { |
| 70 | return pkgcache.find_satisfier(name) |
| 71 | } |
| 72 | return none |
| 73 | } |
| 74 | |
| 75 | // find_sync_target finds a target in the dbs given, already scoped: the |
| 76 | // package of that name, or else what libalpm resolves the dependency string |
| 77 | // to, as pacman's process_targname asks alpm_find_dbs_satisfier — a version |
| 78 | // constraint, or a provider, with libalpm's own questions about ignored |
| 79 | // packages and several providers. named says the db was named with the |
| 80 | // target, which makes it one to install from for this lookup whatever its |
| 81 | // usage, as in pacman |
| 82 | fn find_sync_target(handle &nspkg.Handle, dbs []&nspkg.DB, name string, named bool) ?&nspkg.Package { |
| 83 | if name == '' { |
| 84 | return none |
| 85 | } |
| 86 | if p := find_repo_pkg(dbs, name) { |
| 87 | return p |
| 88 | } |
| 89 | mut list := unsafe { &nspkg.List(nil) } |
| 90 | for db in dbs { |
| 91 | list = list.add(db) |
| 92 | } |
| 93 | defer { list.free() } |
| 94 | if !named { |
| 95 | return handle.find_dbs_satisfier(list, name) |
| 96 | } |
| 97 | mut restore := map[int]nspkg.DBUsage{} |
| 98 | for i, db in dbs { |
| 99 | usage := db.get_usage() or { continue } |
| 100 | if !usage.has(.install) { |
| 101 | db.set_usage(usage | .install) or { continue } |
| 102 | restore[i] = usage |
| 103 | } |
| 104 | } |
| 105 | found := handle.find_dbs_satisfier(list, name) |
| 106 | for i, usage in restore { |
| 107 | dbs[i].set_usage(usage) or { |
| 108 | warn('could not put the usage of ${dbs[i].get_name()} back: ${err.msg()}') |
| 109 | } |
| 110 | } |
| 111 | return found |
| 112 | } |
| 113 | |
| 114 | // GroupMember is one package of a sync group, with the db that carries it |
| 115 | struct GroupMember { |
| 116 | repo string |
| 117 | pkg &nspkg.Package |
| 118 | } |
| 119 | |
| 120 | // sync_group_members are the packages of a group across the dbs, in db |
| 121 | // order; a name a later db repeats is the earlier db's, as libalpm's |
| 122 | // alpm_find_group_pkgs has it. found says a db had the group at all |
| 123 | fn sync_group_members(dbs []&nspkg.DB, name string) (bool, []GroupMember) { |
| 124 | mut found := false |
| 125 | mut out := []GroupMember{} |
| 126 | mut seen := map[string]bool{} |
| 127 | for db in dbs { |
| 128 | group := db.get_group(name) or { continue } |
| 129 | found = true |
| 130 | for pkg in group.packages.pkgs() { |
| 131 | if seen[pkg.get_name()] { |
| 132 | continue |
| 133 | } |
| 134 | seen[pkg.get_name()] = true |
| 135 | out << GroupMember{ |
| 136 | repo: db.get_name() |
| 137 | pkg: pkg |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | return found, out |
| 142 | } |
| 143 | |
| 144 | // select_members asks which members of a group go in, pacman's |
| 145 | // multiselect: the members are listed numbered, on stderr with the question |
| 146 | // they lead into (a frontend gets them as a listing), and the answer picks |
| 147 | // among them. a --print preview lists and asks nothing: every member |
| 148 | fn select_members(mut ui TransUI, group string, members []GroupMember, ask bool) []GroupMember { |
| 149 | if !ask || members.len == 0 { |
| 150 | return members |
| 151 | } |
| 152 | ui.pause() |
| 153 | if ipc_on() { |
| 154 | mut listing := Listing{ |
| 155 | fields: ['source', 'name', 'version'] |
| 156 | } |
| 157 | for m in members { |
| 158 | listing.add_member(m.pkg, m.repo, group) |
| 159 | } |
| 160 | listing.print() |
| 161 | } else { |
| 162 | count := if members.len == 1 { '1 member' } else { '${members.len} members' } |
| 163 | eprintln(erole('heading', '${count} in group ${group}:')) |
| 164 | mut rows := []Row{} |
| 165 | for i, m in members { |
| 166 | rows << row('${i + 1})', m.repo, m.pkg.get_name(), m.pkg.get_version()) |
| 167 | } |
| 168 | eprintln(table_text(rows, ' ', ' ')) |
| 169 | } |
| 170 | picked := ui.pick('enter a selection', members.len) |
| 171 | mut out := []GroupMember{} |
| 172 | for i, m in members { |
| 173 | if picked[i] { |
| 174 | out << m |
| 175 | } |
| 176 | } |
| 177 | return out |
| 178 | } |
| 179 | |