Skip to content

Commit

Permalink
#57 try and be a little more helpful on first boot
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyweston committed Apr 29, 2018
1 parent fff593a commit c5ff582
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/bad/robot/temperature/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object Main extends StreamApp[IO] {
}

Stream
.eval(ConfigFile.load())
.eval(ConfigFile.loadOrWarn())
.flatMap(_.fold(printErrorAndExit, start(_)(requestShutdown)))
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/main/scala/bad/robot/temperature/config/ConfigFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,21 @@ object ConfigFile {
} yield created)
}

def load(resource: KnobsResource = Required(FileResource.unwatched(path))): IO[Either[ConfigurationError, ConfigFile]] = {
def loadOrWarn(resource: KnobsResource = Required(FileResource.unwatched(path))): IO[Either[ConfigurationError, ConfigFile]] = {
for {
exists <- IO(fileExists)
_ <- info(s"""The config file ${path.getAbsoluteFile} doesn't exist, run "temperature-machine --init" to create it.""").unlessA(exists)
config <- load(resource)
} yield config
}

private def load(resource: KnobsResource): IO[Either[ConfigurationError, ConfigFile]] = {
knobs.loadImmutable[IO](List(resource)).attempt.map(_.leftMap(error => {
ConfigurationError(s"There was an error loading config; ${error.getMessage}")
}).map(readConfigFile))
}

// todo maybe use 'lookup' rather than 'require' to avoid a thrown exception?
// todo maybe use 'lookup' rather than 'require' to avoid an exception being thrown?
private val readConfigFile = (config: Config) => new ConfigFile {
def mode: String = config.require[String]("mode")
def hosts: List[String] = config.require[List[String]]("hosts")
Expand Down

0 comments on commit c5ff582

Please sign in to comment.