Skip to content

Commit

Permalink
add search to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
kitlangton committed Jul 1, 2022
1 parent 947285d commit 8783ea4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 25 deletions.
60 changes: 49 additions & 11 deletions src/main/scala/update/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package update

import tui.TUI
import update.versions.Versions
import view.{VerticalAlignment, View}
import zio._

sealed trait Subcommand extends Product with Serializable
Expand Down Expand Up @@ -29,21 +30,58 @@ object Main extends ZIOAppDefault {
subcommand = Subcommand.parse(args.toList)
_ <- subcommand match {
case Some(Subcommand.Interactive) =>
ZIO
.serviceWithZIO[CLI](_.run)
.provide(
CLI.live,
TUI.live(false),
DependencyUpdater.live,
Versions.live,
Files.live
)
runInteractive
case Some(Subcommand.Search(query)) =>
search.Search().searchCLI(query)
runSearch(query)
case None =>
ZIO.debug("Unknown subcommand")
helpMessage
}
} yield ()
}

lazy val runInteractive =
ZIO
.serviceWithZIO[CLI](_.run)
.provide(
CLI.live,
TUI.live(false),
DependencyUpdater.live,
Versions.live,
Files.live
)

private def runSearch(query: String) =
search.Search().searchCLI(query)

lazy val helpMessage = {
val view =
View
.vertical(
View.text("SCALA UPDATE COMMANDS").blue,
View.text("─────────────────────").blue.dim,
"",
View.horizontal(1, VerticalAlignment.top)(
View.text("1.").dim,
View.vertical(
View.text("scala-update").blue.bold,
View.text("Interactively update your library dependencies.").dim
)
),
"",
View.horizontal(1, VerticalAlignment.top)(
View.text("2.").dim,
View.vertical(
View.horizontal(
View.text("scala-update").blue.dim,
View.text("search").blue.bold,
View.text("<query>").blue
),
View.text("Search for Maven-hosted libraries.").dim
)
)
)
.padding(1)

ZIO.debug(view.render(80, 10))
}
}
8 changes: 4 additions & 4 deletions src/main/scala/update/search/Search.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ object Doc {

final case class Search() {

def search(query: String): ZIO[Any, Throwable, List[SearchResult]] = {
def search(query: String): Task[List[SearchResult]] = {
val urlEncodedQuery = URLEncoder.encode(query, "UTF-8")
val url = s"https://search.maven.org/solrsearch/select?q=$urlEncodedQuery&start=0&rows=60"
for {
Expand All @@ -131,22 +131,22 @@ final case class Search() {
.distinctBy(sr => (sr.group, sr.artifact, sr.latestVersion))
}

def searchCLI(query: String): Task[List[SearchResult]] =
def searchCLI(query: String): Task[Unit] =
for {
results <- search(query)
_ <- ZIO.debug(
View
.vertical(
Chunk(
View.text("LATEST PACKAGES").blue,
View.horizontal(View.text("MAVEN PACKAGES FOR").blue, View.text(query).blue.underlined),
View.text("───────────────").blue.dim
) ++
results.map(_.render): _*
)
.padding(1)
.renderNow
)
} yield results
} yield ()

}

Expand Down
11 changes: 1 addition & 10 deletions src/main/scala/update/versions/VersionsLive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final case class VersionsLive() extends Versions {
Map("scalaVersion" -> scalaVersion) ++ sbtVersion.map("sbtVersion" -> _)
),
cache.fetch,
true
versionsCheckHasModule = true
)
.run
.map(_.left.map(new Error(_)))
Expand All @@ -46,12 +46,3 @@ final case class VersionsLive() extends Versions {
}

}

//object VersionsDemo extends ZIOAppDefault {
// val run = {
// for {
// _ <- VersionsLive().getVersions(Group("dev.zio"), Artifact("zio-json")).debug("Library Versions")
// _ <- VersionsLive().getVersions(Group("com.github.sbt"), Artifact("sbt-native-packager")).debug("Plugin Versions")
// } yield ()
// }
//}

0 comments on commit 8783ea4

Please sign in to comment.