diff --git a/modules/ingestor/src/main/scala/cli.scala b/modules/ingestor/src/main/scala/cli.scala index 17d3898..f32323a 100644 --- a/modules/ingestor/src/main/scala/cli.scala +++ b/modules/ingestor/src/main/scala/cli.scala @@ -53,24 +53,21 @@ object cli case opts: IndexOpts => index(opts) case opts: WatchOpts => watch(opts) - import opts.{ All, Single } def index(opts: IndexOpts): IO[Unit] = opts.index match - case All => + case Index.Forum => + forum.run(opts.since, opts.until, opts.dry).compile.drain + case Index.Study => + study.run(opts.since, opts.until, opts.dry).compile.drain + case Index.Game => + game.run(opts.since, opts.until, opts.dry).compile.drain + case Index.Team => + team.run(opts.since, opts.until, opts.dry).compile.drain + case _ => forum.run(opts.since, opts.until, opts.dry).compile.drain *> study.run(opts.since, opts.until, opts.dry).compile.drain *> game.run(opts.since, opts.until, opts.dry).compile.drain *> team.run(opts.since, opts.until, opts.dry).compile.drain - case Single(index) => - index match - case Index.Forum => - forum.run(opts.since, opts.until, opts.dry).compile.drain - case Index.Study => - study.run(opts.since, opts.until, opts.dry).compile.drain - case Index.Game => - game.run(opts.since, opts.until, opts.dry).compile.drain - case Index.Team => - team.run(opts.since, opts.until, opts.dry).compile.drain def watch(opts: WatchOpts): IO[Unit] = opts.index match @@ -79,9 +76,7 @@ object cli case _ => IO.println("We only support game watch for now") object opts: - case class Single(index: Index) - object All - case class IndexOpts(index: Single | All.type, since: Instant, until: Instant, dry: Boolean) + case class IndexOpts(index: Index | Unit, since: Instant, until: Instant, dry: Boolean) case class WatchOpts(index: Index, since: Instant, dry: Boolean) def parse = Opts.subcommand("index", "index documents")(indexOpt) <+> @@ -95,20 +90,20 @@ object opts: short = "i", metavar = "forum|team|study|game" ) - .map(Single.apply) val allIndexOpt = Opts - .flag( - long = "all", - help = "All indexes" - ) - .as(All) + .flag(long = "all", help = "All indexes") + .void - val inputOpts = singleIndexOpt orElse allIndexOpt + val dryOpt = + Opts + .flag(long = "dry", help = "Dry run", short = "d") + .orNone + .map(_.isDefined) val indexOpt = ( - inputOpts, + singleIndexOpt orElse allIndexOpt, Opts.option[Instant]( long = "since", help = "Index all documents since", @@ -123,14 +118,7 @@ object opts: metavar = "time in epoch seconds" ) .orElse(Instant.now.pure[Opts]), - Opts - .flag( - long = "dry", - help = "Dry run", - short = "d" - ) - .orNone - .map(_.isDefined) + dryOpt ).mapN(IndexOpts.apply) .mapValidated(x => if x.until.isAfter(x.since) then Validated.valid(x) @@ -152,14 +140,7 @@ object opts: metavar = "time in epoch seconds" ) .orElse(Instant.now.pure[Opts]), - Opts - .flag( - long = "dry", - help = "Dry run", - short = "d" - ) - .orNone - .map(_.isDefined) + dryOpt ).mapN(WatchOpts.apply) given Argument[Index] = diff --git a/modules/ingestor/src/test/scala/ClITest.scala b/modules/ingestor/src/test/scala/ClITest.scala new file mode 100644 index 0000000..77e9a4e --- /dev/null +++ b/modules/ingestor/src/test/scala/ClITest.scala @@ -0,0 +1,27 @@ +package lila.search +package ingestor + +import cats.syntax.all.* +import com.monovore.decline +import com.monovore.decline.Help + +import java.time.Instant + +import opts.* + +object CLITest extends weaver.FunSuite: + + def testCommand(args: String*): Either[Help, IndexOpts | WatchOpts] = + decline.Command("test", "Test Command")(opts.parse).parse(args) + + test("index command"): + expect( + testCommand("index", "--index", "team", "--since", "0", "--until", "1", "--dry") == + IndexOpts(Index.Team, Instant.ofEpochSecond(0), Instant.ofEpochSecond(1), true).asRight + ) + + test("watch command"): + expect( + testCommand("watch", "--index", "team", "--since", "0", "--dry") == + WatchOpts(Index.Team, Instant.ofEpochSecond(0), true).asRight + )