aboutsummaryrefslogtreecommitdiff
path: root/src/feature/syncinfo.nim
blob: fc1510b20cb2b8964e8910b3544fc460a90a49ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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[PackageReference]): PackageLineFormat =
  let values: seq[tuple[title: string, hasDesc: bool]] = refs
    .map(r => r.description
      .map(d => ($r & ": " & d, true))
      .get(($r, false)))

  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.foundInfos.len > 0:
    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
    elif target.reference.constraint.isSome:
      # pacman doesn't support constraints for --info queries
      pacmanRun(false, config.color, args & target.foundInfos.map(i =>
        (i.repo & "/" & target.reference.name, none(string), ArgumentType.target)))
    else:
      pacmanRun(false, config.color, args &
        ($target, none(string), ArgumentType.target))
  else:
    if target.repo == some("aur"):
      printError(config.color, trp("package '%s' was not found\n") % [$target])
      1
    else:
      pacmanRun(false, config.color, args & ($target, none(string), ArgumentType.target))

proc handleSyncInfo*(args: seq[Argument], config: Config): int =
  let (_, callArgs) = checkAndRefresh(config.color, args)
  let targets = args.packageTargets(false)

  let (syncTargets, checkAurNames) = withAlpmConfig(config, true, handle, dbs, errors):
    for e in errors: printError(config.color, e)
    findSyncTargets(handle, dbs, targets, false, false)

  let (pkgInfos, _, aerrors) = getAurPackageInfos(checkAurNames, config.arch)
  for e in aerrors: printError(config.color, e)

  let fullTargets = mapAurTargets[PackageInfo](syncTargets, pkgInfos)

  let code = min(aerrors.len, 1)
  if fullTargets.filter(t => isAurTargetFull[PackageInfo](t) or t.repo == some("aur")).len == 0 and
    fullTargets.filter(t => t.reference.constraint.isSome).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)