aboutsummaryrefslogtreecommitdiff
path: root/src/main.nim
blob: 71717163f6a633f67caae9b092aba84555598c66 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import
  future, options, os, posix, re, sequtils, strutils,
  args, config, format, pacman, utils

import
  "feature/syncinfo",
  "feature/syncinstall",
  "feature/syncsearch",
  "feature/syncsource",
  "feature/localquery"

proc passValidation(args: seq[Argument], config: Config,
  nonRootArgs: openArray[OptionPair], rootArgs: openArray[OptionPair],
  opts: varargs[seq[CommandOption]]): int =
  let checkArgs = args.filterOptions(true, false, true, opts)

  if checkArgs.len == 0:
    let needRoot = (nonRootArgs.len == 0 and args.check(rootArgs)) or
      (nonRootArgs.len > 0 and (not args.check(nonRootArgs) or args.check(rootArgs)))
    return pacmanExec(needRoot, config.color, args.filterExtensions(true, true, opts))
  else:
    let extensions = args.filterExtensions(false, false, opts)
    if extensions.len == 0:
      return pacmanExec(false, config.color, args)
    else:
      let arg = extensions[0]
      if arg.isShort:
        raise commandError(trp("invalid option '-%c'\n").strip
          .replace("%c", "$#") % arg.key)
      else:
        raise commandError(trp("invalid option '--%s'\n").strip
          .replace("%s", "$#") % arg.key)

proc handleDatabase(args: seq[Argument], config: Config): int =
  let nonRootArgs = [
    %%%"check"
  ]

  passValidation(args, config, nonRootArgs, [],
    commonOptions, databaseOptions)

proc handleFiles(args: seq[Argument], config: Config): int =
  let rootArgs = [
    %%%"refresh"
  ]

  passValidation(args, config, [], rootArgs,
    commonOptions, filesOptions)

proc handleQuery(args: seq[Argument], config: Config): int =
  let queryArgs = args.removeMatchOptions(commonOptions)

  if queryArgs.checkOpGroup(OpGroup.localQuery) and
    not queryArgs.check(%%%"explicit") and
    queryArgs.check(%%%"deps") and
    queryArgs.count(%%%"unrequired") >= 3:
    handleQueryOrphans(args, config)
  else:
    passValidation(args, config, [], [],
      commonOptions, queryOptions)

proc handleRemove(args: seq[Argument], config: Config): int =
  let nonRootArgs = [
    %%%"print",
    %%%"print-format"
  ]

  passValidation(args, config, nonRootArgs, [],
    commonOptions, transactionOptions, removeOptions)

proc handleSync(args: seq[Argument], config: Config): int =
  let syncArgs = args.removeMatchOptions(commonOptions, transactionOptions, upgradeOptions)
  let conflict = args.checkConflicts(syncConflictingOptions)

  if conflict.isSome:
    let (left, right) = conflict.unsafeGet
    printError(config.color, trp("invalid option: '%s' and '%s' may not be used together\n") %
      ["--" & left, "--" & right])
    1
  elif syncArgs.check(%%%"info") and
    syncArgs.checkOpGroup(OpGroup.syncQuery):
    handleSyncInfo(args, config)
  elif syncArgs.check(%%%"search") and
    syncArgs.checkOpGroup(OpGroup.syncSearch):
    handleSyncSearch(args, config)
  elif syncArgs.check(%%%"source"):
    handleSyncSource(args, config)
  elif syncArgs.checkOpGroup(OpGroup.syncInstall) and
    (args.check(%%%"sysupgrade") or args.targets.len > 0):
    let printMode = args.check(%%%"print") or args.check(%%%"print-format")

    if currentUser.uid != 0 and config.sudoExec and not printMode:
      let collectedArgs = sudoPrefix & getAppFilename() &
        lc[x | (y <- args, x <- y.collectArg), string]
      execResult(collectedArgs)
    else:
      let isNonDefaultRoot = not config.isRootDefault
      let isRootNoDrop = currentUser.uid == 0 and not canDropPrivileges()

      let build = args.check(%%%"build")
      let noaur = args.check(%%%"noaur")

      let noBuild = isNonDefaultRoot or isRootNoDrop

      if not printMode and build and noBuild:
        if isNonDefaultRoot:
          printError(config.color, tr"non-default root path is specified" & " -- " &
            tr"building is not allowed")
        elif isRootNoDrop:
          printError(config.color, tr"running as root" & " -- " &
            tr"building is not allowed")
        1
      else:
        let noaurAdd = not printMode and noBuild and not noaur

        if noaurAdd:
          if isNonDefaultRoot:
            printWarning(config.color, tr"non-default root path is specified" & " -- " &
              tr"'$#' is assumed" % ["--noaur"])
          elif isRootNoDrop:
            printWarning(config.color, tr"running as root" & " -- " &
              tr"'$#' is assumed" % ["--noaur"])

        if noaurAdd:
          handleSyncInstall(args & ("noaur", none(string), ArgumentType.long), config)
        else:
          handleSyncInstall(args, config)
  else:
    let nonRootArgs = [
      %%%"print",
      %%%"print-format",
      %%%"groups",
      %%%"info",
      %%%"list",
      %%%"search"
    ]

    let rootArgs = [
      %%%"refresh"
    ]

    passValidation(args, config, nonRootArgs, rootArgs,
      commonOptions, transactionOptions, upgradeOptions, syncOptions)

proc handleDeptest(args: seq[Argument], config: Config): int =
  passValidation(args, config, [], [], commonOptions)

proc handleUpgrade(args: seq[Argument], config: Config): int =
  let nonRootArgs = [
    %%%"print",
    %%%"print-format"
  ]

  passValidation(args, config, nonRootArgs, [],
    commonOptions, transactionOptions, upgradeOptions)

proc handleHelp(operation: OperationType) =
  proc printHelp(pair: OptionPair, arg: Option[string], text: string) =
    let shortPrefix = pair.short.map(s => "  -" & s & ", ").get(' '.repeat(6))
    let longValue = arg.map(a => pair.long & " <" & a & ">").get(pair.long)

    if longValue.len > 14:
      echo(shortPrefix, "--", longValue)
      echo(' '.repeat(23), text)
    else:
      echo(shortPrefix, "--", longValue, ' '.repeat(15 - longValue.len), text)

  let operationArgs = operations
    .filter(o => o.otype == operation)
    .map(o => @["-" & o.pair.short.get])
    .optFirst.get(@[]) & @["-h"]

  let (lines, _) = forkWaitRedirect(() => execResult(pacmanCmd & operationArgs))

  for line in lines:
    echo(line.replace(re"\bpacman\b", "pakku"))

  if lines.len > 0:
    case operation:
      of OperationType.sync:
        printHelp(%%%"build", none(string), tr"build targets from source")
        printHelp(%%%"keyserver", some("name"), tr"use name as keyserver to receive keys from")
        printHelp(%%%"noaur", none(string), tr"disable all AUR operations")
        printHelp(%%%"source", none(string), tr"retrieve PKGBUILD source")
      else:
        discard

const
  version = getEnv("PROG_VERSION")
  copyright = getEnv("PROG_COPYRIGHT")

proc handleVersion(): int =
  echo()
  echo(' '.repeat(23), "Pakku v", version)
  echo(' '.repeat(23), "Copyright (C) ", copyright)
  pacmanExec(false, false, ("V", none(string), ArgumentType.short))

discard setlocale(LC_ALL, "")

template withErrorHandler(propColor: Option[bool], T: typedesc, body: untyped):
  tuple[success: Option[T], code: int] =
  try:
    (some(body), 0)
  except HaltError:
    let e = (ref HaltError) getCurrentException()
    (none(T), e.code)
  except CommandError:
    let e = (ref CommandError) getCurrentException()
    if e.error:
      printError(e.color.orElse(propColor).get(false), e.msg)
    else:
      stderr.writeLine(e.msg)
    (none(T), 1)

let init = withErrorHandler(none(bool),
  tuple[parsedArgs: seq[Argument], config: Config]):
  let parsedArgs = splitArgs(commandLineParams(), optionsWithParameter,
    (operations.map(o => o.pair.long) & allOptions.map(o => o.pair.long)).deduplicate)
  let operation = getOperation(parsedArgs)
  if operation != OperationType.invalid and
    parsedArgs.check(%%%"help"):
    handleHelp(operation)
    raise haltError(0)
  elif operation != OperationType.invalid and
    parsedArgs.check((some("V"), "version")):
    let code = handleVersion()
    raise haltError(code)
  else:
    let pacmanConfig = obtainPacmanConfig(parsedArgs)
    let config = obtainConfig(pacmanConfig)
    (parsedArgs, config)

proc run(parsedArgs: seq[Argument], config: Config):
  tuple[success: Option[int], code: int] =
  withErrorHandler(some(config.color), int):
    case getOperation(parsedArgs):
      of OperationType.database:
        handleDatabase(parsedArgs, config)
      of OperationType.files:
        handleFiles(parsedArgs, config)
      of OperationType.query:
        handleQuery(parsedArgs, config)
      of OperationType.remove:
        handleRemove(parsedArgs, config)
      of OperationType.sync:
        handleSync(parsedArgs, config)
      of OperationType.deptest:
        handleDeptest(parsedArgs, config)
      of OperationType.upgrade:
        handleUpgrade(parsedArgs, config)
      else:
        passValidation(parsedArgs, config, [], [], allOptions)

let runResult = if init.success.isSome:
    run(init.success.unsafeGet.parsedArgs, init.success.unsafeGet.config)
  else:
    (none(int), init.code)

programResult = if runResult.success.isSome:
    runResult.success.unsafeGet
  else:
    runResult.code