Due to a scheduled upgrade to version 14.10, GitLab will be unavailabe on Monday 30.05., from 19:00 until 20:00.

ArgParser.scala 3.24 KB
Newer Older
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
1
2
/*
 * Import Process Delete
3
 * Copyright (C) 2021 Memoriav
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
19
20
21
22
23
package ch.memobase

import ch.memobase.models._
import scopt.{OParser, OParserBuilder}

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
24
25
import java.util.Calendar

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
trait ArgParser {
  self: MsgFilter =>

  private val builder: OParserBuilder[Args] = OParser.builder[Args]

  private val parser: OParser[Unit, Args] = {
    import builder._
    OParser.sequence(
      programName("import-process-delete"),
      head("", "0.1.0"),
      opt[String]('c', "record-set-filter")
        .action((v, c) => c.copy(recordSetFilters = c.recordSetFilters :+ v))
        .valueName("<id>")
        .text("record set id filter")
40
        .unbounded()
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
41
42
43
44
45
        .optional(),
      opt[String]('i', "institution-filter")
        .action((v, c) => c.copy(institutionFilters = c.institutionFilters :+ v))
        .valueName("<id>")
        .text("institution id filter")
46
        .unbounded()
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
47
48
49
50
51
        .optional(),
      opt[String]('r', "record-filter")
        .action((v, c) => c.copy(recordFilters = c.recordFilters :+ v))
        .valueName("<id>")
        .text("record id filter")
52
        .unbounded()
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
53
54
55
56
57
        .optional(),
      opt[String]('s', "session-filter")
        .action((v, c) => c.copy(sessionFilters = c.sessionFilters :+ v))
        .valueName("<id>")
        .text("session id filter")
58
        .unbounded()
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
59
60
        .optional(),
      opt[Calendar]('a', "created-after")
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
61
        .action((v, c) => c.copy(createdAfterFilter = Some(v)))
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
62
63
64
65
66
        .valueName("<datetime>")
        .text("retains only records processed after timestamp")
        .maxOccurs(1)
        .optional(),
      opt[Calendar]('b', "created-before")
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
67
        .action((v, c) => c.copy(createdBeforeFilter = Some(v)))
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
68
69
70
71
        .valueName("<datetime>")
        .text("retains only records processed before timestamp")
        .maxOccurs(1)
        .optional(),
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
72
73
74
      opt[Unit]('d', "dry-run")
        .action((_, c) => c.copy(dryRun = true))
        .text("dry-run delete"),
75
      arg[String]("<session-id>")
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
76
77
78
79
80
81
82
        .action((v, c) => c.copy(sessionId = v))
        .text("session id assigned to delete message")
        .required(),
      help("help").text("prints this text")
    )
  }

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
83
  def parse(args: Array[String]): Option[(String, Set[FilterFun], Boolean)] = {
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
84
85
86
    OParser.parse(parser, args, Args()) match {
      case Some(config) => Some(
        config.sessionId,
87
88
89
90
91
        buildFilters(config.createdAfterFilter,
          config.createdBeforeFilter,
          config.institutionFilters,
          config.recordSetFilters,
          config.recordFilters,
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
92
93
94
          config.sessionFilters),
        config.dryRun
      )
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
95
96
97
98
      case None => None
    }
  }
}