Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to search Maven #34

Merged
merged 2 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ commands = [
title: "YouTube"
urlTemplate: "https://www.youtube.com/results?search_query={query}"
commandNames: ["y" "youtube"]
}
},
{type: "SearchMavenCommand"}
]

aliases = {
Expand Down
25 changes: 14 additions & 11 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@ lazy val core = module("core")
.settings(
resolvers := Resolvers,
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % Version.zio,
"dev.zio" %% "zio-streams" % Version.zio,
"dev.zio" %% "zio-process" % "0.0.6",
"dev.zio" %% "zio-logging" % "0.3.2",
"io.circe" %% "circe-config" % "0.8.0",
"org.scala-lang" % "scala-reflect" % "2.13.3",
"io.circe" %% "circe-core" % Version.circe,
"io.circe" %% "circe-parser" % Version.circe,
"com.monovore" %% "decline" % "1.2.0",
"com.lihaoyi" %% "fansi" % "0.2.9",
"com.beachape" %% "enumeratum" % "1.6.1"
"dev.zio" %% "zio" % Version.zio,
"dev.zio" %% "zio-streams" % Version.zio,
"dev.zio" %% "zio-process" % "0.0.6",
"dev.zio" %% "zio-logging" % "0.3.2",
"io.circe" %% "circe-config" % "0.8.0",
"org.scala-lang" % "scala-reflect" % "2.13.3",
"io.circe" %% "circe-core" % Version.circe,
"io.circe" %% "circe-parser" % Version.circe,
"com.monovore" %% "decline" % "1.2.0",
"com.lihaoyi" %% "fansi" % "0.2.9",
"com.beachape" %% "enumeratum" % "1.6.1",
"com.softwaremill.sttp.client" %% "core" % Version.sttp,
"com.softwaremill.sttp.client" %% "circe" % Version.sttp,
"com.softwaremill.sttp.client" %% "async-http-client-backend-zio" % Version.sttp
)
)

Expand Down
5 changes: 3 additions & 2 deletions core/src/main/scala/commandcenter/CCRuntime.scala
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package commandcenter

import commandcenter.CCRuntime.Env
import sttp.client.asynchttpclient.zio.{ AsyncHttpClientZioBackend, SttpClient }
import zio.internal.Platform
import zio.logging.Logging
import zio.{ Runtime, ZEnv }

trait CCRuntime extends Runtime[Env] {
lazy val runtime: Runtime.Managed[Env] = Runtime.unsafeFromLayer {
ZEnv.live ++ (ZEnv.live >>> Logging.console((_, logEntry) => logEntry))
ZEnv.live ++ (ZEnv.live >>> Logging.console((_, logEntry) => logEntry)) ++ AsyncHttpClientZioBackend.layer()
}

lazy val environment: Env = runtime.environment
lazy val platform: Platform = runtime.platform
}

object CCRuntime {
type Env = ZEnv with Logging
type Env = ZEnv with Logging with SttpClient

lazy val default: CCRuntime = new CCRuntime {}
}
1 change: 1 addition & 0 deletions core/src/main/scala/commandcenter/command/Command.scala
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ object Command {
case CommandType.ResizeCommand => config.as[ResizeCommand]
case CommandType.ReloadCommand => config.as[ReloadCommand]
case CommandType.SearchUrlCommand => config.as[SearchUrlCommand]
case CommandType.SearchMavenCommand => config.as[SearchMavenCommand]
case CommandType.TemperatureCommand => config.as[TemperatureCommand]
case CommandType.TerminalCommand => config.as[TerminalCommand]
case CommandType.TimerCommand => config.as[TimerCommand]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ object CommandType extends Enum[CommandType] {
case object ReloadCommand extends CommandType
case object ResizeCommand extends CommandType
case object SearchUrlCommand extends CommandType
case object SearchMavenCommand extends CommandType
case object TemperatureCommand extends CommandType
case object TerminalCommand extends CommandType
case object TimerCommand extends CommandType
Expand Down
70 changes: 70 additions & 0 deletions core/src/main/scala/commandcenter/command/SearchMavenCommand.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package commandcenter.command

import commandcenter.CommandContext
import commandcenter.command.CommandError.NotApplicable
import commandcenter.command.SearchMavenCommand.MavenArtifact
import commandcenter.util.ProcessUtil
import commandcenter.view.DefaultView
import io.circe.{ Decoder, Json }
import sttp.client._
import sttp.client.asynchttpclient.zio._
import sttp.client.circe._
import zio.{ IO, ZIO }

final case class SearchMavenCommand() extends Command[String] {
val command = "mvn"
val commandType: CommandType = CommandType.SearchMavenCommand
val commandNames: List[String] = List(command)
val title: String = "Maven"

override def prefixPreview(
prefix: String,
rest: String,
context: CommandContext
): ZIO[SttpClient, CommandError, List[PreviewResult[String]]] =
if (rest.isEmpty) {
IO.fail(NotApplicable)
} else {
val request = basicRequest
.get(uri"https://search.maven.org/solrsearch/select?q=$rest&rows=10&wt=json")
.response(asJson[Json])
for {
response <- SttpClient
.send(request)
.map(_.body)
.absolve
.mapError(CommandError.UnexpectedException)
artifacts <- IO
.fromEither(response.hcursor.downField("response").downField("docs").as[List[MavenArtifact]])
.mapError(CommandError.UnexpectedException)
} yield {
artifacts.map { artifact =>
Preview(artifact.toString)
.onRun(ProcessUtil.copyToClipboard(artifact.version))
.score(Scores.high(context))
.view(
DefaultView(
title,
fansi.Str.join(
fansi.Str("Group: "),
fansi.Color.Magenta(artifact.groupId),
fansi.Str(" Artifact: "),
fansi.Color.Magenta(artifact.artifactId),
fansi.Str(" Version: "),
fansi.Color.Magenta(artifact.version)
)
)
)
}
}
}
}

object SearchMavenCommand extends CommandPlugin[SearchMavenCommand] {
final case class MavenArtifact(groupId: String, artifactId: String, version: String)

implicit val artifactDecoder: Decoder[MavenArtifact] =
Decoder.forProduct3("g", "a", "latestVersion")(MavenArtifact.apply)

implicit val decoder: Decoder[SearchMavenCommand] = Decoder.const(SearchMavenCommand())
}
1 change: 1 addition & 0 deletions project/CommandCenterBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ object CommandCenterBuild {
object Version {
val zio = "1.0.0-RC21-2"
val circe = "0.13.0"
val sttp = "2.2.3"

// If you set this to None you can test with your locally installed version of Graal. Otherwise it will run in Docker
// and build a Linux image (e.g. setting it to "20.1.0-java11").
Expand Down