Skip to content

Commit 7392b0b

Browse files
Merge branch 'release/v1.2.0'
2 parents d1d4cbb + 7414e24 commit 7392b0b

File tree

4 files changed

+78
-4
lines changed

4 files changed

+78
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
target/
33
project/boot/
44
project/plugins/project/
5+
project/local.sbt

build.sbt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name := "whatismyip"
22

3-
version := "1.1.0"
3+
version := "1.2.0"
44

55
scalaVersion := "2.13.5"
66

@@ -14,4 +14,5 @@ libraryDependencies ++= Seq(
1414
"com.typesafe.akka" %% "akka-http-spray-json" % AkkaHttpVersion,
1515
"ch.qos.logback" % "logback-classic" % "1.2.3",
1616
"net.logstash.logback" % "logstash-logback-encoder" % "6.6",
17+
"com.github.scopt" %% "scopt" % "4.0.1"
1718
)

src/main/scala/com/github/invis1ble/whatismyip/App.scala

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import akka.actor.typed.ActorSystem
44
import akka.actor.typed.scaladsl.Behaviors
55
import akka.event.Logging
66
import akka.stream.Attributes
7+
import ch.qos.logback.classic.LoggerContext
78
import com.github.invis1ble.whatismyip.info.providers.MyipcomInfoProvider
89
import org.slf4j.LoggerFactory
10+
import scopt.{DefaultOEffectSetup, DefaultOParserSetup, OParser}
911

12+
import java.io.{File, PrintWriter}
1013
import scala.concurrent.ExecutionContextExecutor
1114
import scala.concurrent.duration.DurationInt
1215

@@ -16,6 +19,11 @@ object App {
1619
def main(args: Array[String]): Unit = {
1720
logger.info("App started")
1821

22+
val config = parseArgs(args) match {
23+
case Some(config) => config
24+
case _ => terminate(); sys.exit(1)
25+
}
26+
1927
implicit val system: ActorSystem[Nothing] = ActorSystem(Behaviors.empty, "WhatIsMyIpAddress")
2028
implicit val ec: ExecutionContextExecutor = system.executionContext
2129

@@ -25,11 +33,70 @@ object App {
2533
onFinish = Logging.InfoLevel,
2634
onFailure = Logging.ErrorLevel,
2735
))
28-
.runForeach {
36+
.map {
2937
case Some(info) =>
3038
val ccLabel = info.location.flatMap(_.countryCode).getOrElse("n/a")
31-
println(s"[$ccLabel] ${info.address}")
32-
case _ => println("n/a")
39+
s"[$ccLabel] ${info.address}"
40+
case _ => "n/a"
3341
}
42+
.runForeach(writeln(_, config.file))
43+
}
44+
45+
private def writeln(str: String, file: Option[File] = None): Unit = {
46+
file match {
47+
case Some(file) =>
48+
val writer = new PrintWriter(file)
49+
writer.println(str)
50+
writer.close()
51+
case None => println(str)
52+
}
53+
}
54+
55+
private def parseArgs(args: Array[String]): Option[Config] = {
56+
OParser.runParser(createParser, args, Config(), new DefaultOParserSetup {
57+
override def showUsageOnError: Some[Boolean] = Some(true)
58+
}) match {
59+
case (result, effects) =>
60+
OParser.runEffects(effects, new DefaultOEffectSetup {
61+
override def terminate(exitState: Either[String, Unit]): Unit = {
62+
App.terminate()
63+
super.terminate(exitState)
64+
}
65+
})
66+
result
67+
}
68+
}
69+
70+
private def createParser: OParser[_, Config] = {
71+
val builder = OParser.builder[Config]
72+
import builder._
73+
74+
OParser.sequence(
75+
programName("whatismyip"),
76+
head("whatismyip", "1.2.0"),
77+
opt[Option[File]]('f', "file")
78+
.action((x, c) => c.copy(file = x))
79+
.validate {
80+
case Some(r) =>
81+
if (r.exists) {
82+
if (r.isFile) {
83+
if (!r.canWrite) failure(s"""Path "${r.getAbsolutePath}" must be writable by current user""")
84+
} else {
85+
failure(s"""Path "${r.getAbsolutePath}" is not a file""")
86+
}
87+
}
88+
success
89+
case None => success
90+
}
91+
.text("Output file (will be created if not exists)"),
92+
help("help").text("prints this usage text"),
93+
version("version")
94+
)
95+
}
96+
97+
private def terminate(): Unit = {
98+
LoggerFactory.getILoggerFactory match {
99+
case ctx: LoggerContext => ctx.stop()
100+
}
34101
}
35102
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.github.invis1ble.whatismyip
2+
3+
import java.io.File
4+
5+
final case class Config(file: Option[File] = None)

0 commit comments

Comments
 (0)