diff options
author | kitsunyan | 2018-03-10 21:10:43 +0000 |
---|---|---|
committer | kitsunyan | 2018-03-10 21:10:43 +0000 |
commit | fd397b356e9be1d3572ace965ead57120803a0ac (patch) | |
tree | d9e6fe9f903ab935fa6df2e6862a97515caf926c /src/feature/syncinfo.nim |
Initial commitv0.1
Diffstat (limited to 'src/feature/syncinfo.nim')
-rw-r--r-- | src/feature/syncinfo.nim | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/src/feature/syncinfo.nim b/src/feature/syncinfo.nim new file mode 100644 index 0000000..3c64282 --- /dev/null +++ b/src/feature/syncinfo.nim @@ -0,0 +1,130 @@ +import + future, options, posix, sequtils, strutils, times, + "../args", "../aur", "../common", "../config", "../format", "../package", + "../pacman", "../utils", + "../wrapper/alpm" + +const + pacmanInfoStrings = [ + "Architecture", + "Backup Files", + "Build Date", + "Compressed Size", + "Conflicts With", + "Depends On", + "Description", + "Download Size", + "Groups", + "Install Date", + "Install Reason", + "Install Script", + "Installed Size", + "Licenses", + "MD5 Sum", + "Name", + "Optional Deps", + "Optional For", + "Packager", + "Provides", + "Replaces", + "Repository", + "Required By", + "SHA-256 Sum", + "Signatures", + "URL", + "Validated By", + "Version" + ] + +proc formatDeps(title: string, config: Config, + refs: seq[ArchPackageReference]): PackageLineFormat = + proc formatDep(reference: ArchPackageReference): (string, bool) = + reference.reference.description + .map(d => ($reference.reference & ": " & d, true)) + .get(($reference.reference, false)) + + let values: seq[tuple[title: string, hasDesc: bool]] = refs + .filter(r => r.arch.isNone or r.arch == some(config.arch)) + .map(formatDep) + + if values.len > 0: + (title, values.map(v => v.title), values.map(v => v.hasDesc).foldl(a or b)) + else: + (title, @[], false) + +proc formatDate(date: Option[int64]): seq[string] = + if date.isSome: + var time = (posix.Time) date.unsafeGet + var ltime: Tm + discard localtime_r(time, ltime) + var buffer: array[100, char] + let res = strftime(addr(buffer), buffer.len, "%c", ltime) + if res > 0: @[buffer.toString(none(int))] else: @[] + else: + @[] + +proc handleTarget(config: Config, padding: int, args: seq[Argument], + target: FullPackageTarget[PackageInfo]): int = + if target.foundInfo.isSome: + if isAurTargetFull[PackageInfo](target): + let pkgInfo = target.pkgInfo.unsafeGet + + printPackageInfo(padding, config.color, + (trp"Repository", @["aur"], false), + (trp"Name", @[pkgInfo.name], false), + (trp"Version", @[pkgInfo.version], false), + (trp"Description", toSeq(pkgInfo.description.items), false), + (trp"Architecture", pkgInfo.archs, false), + (trp"URL", toSeq(pkgInfo.url.items), false), + (trp"Licenses", pkgInfo.licenses, false), + (trp"Groups", pkgInfo.groups, false), + formatDeps(trp"Provides", config, pkgInfo.provides), + formatDeps(trp"Depends On", config, pkgInfo.depends), + formatDeps(trp"Optional Deps", config, pkgInfo.optional), + formatDeps(trp"Conflicts With", config, pkgInfo.conflicts), + formatDeps(trp"Replaces", config, pkgInfo.replaces), + (tr"Maintainer", toSeq(pkgInfo.maintainer.items()), false), + (tr"First Submitted", pkgInfo.firstSubmitted.formatDate, false), + (tr"Last Modified", pkgInfo.lastModified.formatDate, false), + (tr"Rating", @[formatPkgRating(pkgInfo.votes, pkgInfo.popularity)], false)) + + 0 + else: + pacmanRun(false, config.color, args & + (target.formatArgument, none(string), ArgumentType.target)) + else: + if target.repo == some("aur"): + printError(config.color, trp("package '%s' was not found\n") % [target.formatArgument]) + 1 + else: + pacmanRun(false, config.color, args & + (target.formatArgument, none(string), ArgumentType.target)) + +proc handleSyncInfo*(args: seq[Argument], config: Config): int = + let (_, callArgs) = checkAndRefresh(config.color, args) + let targets = args.packageTargets + + let (syncTargets, checkAur) = withAlpm(config.root, config.db, + config.dbs, config.arch, handle, dbs, errors): + for e in errors: printError(config.color, e) + findSyncTargets(handle, dbs, targets, false, false) + + let (pkgInfos, aerrors) = getAurPackageInfo(checkAur, none(seq[RpcPackageInfo]), + proc (a: int, b: int) = discard) + for e in aerrors: printError(config.color, e) + + let fullTargets = mapAurTargets[PackageInfo](syncTargets, pkgInfos) + + let code = min(aerrors.len, 1) + if fullTargets.filter(isAurTargetFull[PackageInfo]).len == 0: + if code == 0: + pacmanExec(false, config.color, callArgs) + else: + discard pacmanRun(false, config.color, callArgs) + code + else: + let finalArgs = callArgs.filter(arg => not arg.isTarget) + let padding = pacmanInfoStrings.map(s => s.trp).computeMaxLength + + let codes = code & lc[handleTarget(config, padding, finalArgs, x) | (x <- fullTargets), int] + codes.filter(c => c != 0).optFirst.get(0) |