Skip to content

Commit

Permalink
Show a helpful error msg if site dir does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
laurilehmijoki committed Jan 21, 2015
1 parent 16c4975 commit d4766da
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
57 changes: 44 additions & 13 deletions src/main/scala/s3/website/model/Site.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,61 @@ object Site {
def loadSite(implicit yamlConfig: S3_website_yml, cliArgs: CliArgs, workingDirectory: File, logger: Logger): Either[ErrorReport, Site] =
parseConfig.right.flatMap { cfg =>
implicit val config: Config = cfg
val errorOrSiteDir = resolveSiteDir.fold(Left(ErrorReport(noSiteFound)): Either[ErrorReport, File])(Right(_))
errorOrSiteDir.right.map(Site(_, config))
resolveSiteDir.right.map(Site(_, config))
}

val noSiteFound =
"""|Could not find a website.
|Either use the --site=DIR command-line argument or define the location of the site in s3_website.yml.
|
|Here's an example of how you can define the site directory in s3_website.yml:
| site: dist/website""".stripMargin
def noSiteFound(explanation: String) =
s"""|
|$explanation.
|Either use the --site=DIR command-line argument or define the location of the site in s3_website.yml.
|
|Here's an example of how you can define the site directory in s3_website.yml:
| site: dist/website""".stripMargin

def resolveSiteDir(implicit yamlConfig: S3_website_yml, config: Config, cliArgs: CliArgs, workingDirectory: File): Option[File] = {
def resolveSiteDir(implicit yamlConfig: S3_website_yml, config: Config, cliArgs: CliArgs, workingDirectory: File): Either[ErrorReport, File] = {
val siteFromAutoDetect = autodetectSiteDir(workingDirectory)
val siteFromCliArgs = Option(cliArgs.site).map(new File(_))
val errOrSiteFromCliArgs: Either[ErrorReport, Option[File]] = Option(cliArgs.site) match {
case Some(siteDirFromCliArgs) =>
val f = new File(siteDirFromCliArgs)
if (f.exists())
Right(Some(f))
else
Left(ErrorReport(noSiteFound(s"Could not find a site at $siteDirFromCliArgs. Check the --site argument.")))
case None => Right(None)
}

siteFromCliArgs orElse siteFromConfig orElse siteFromAutoDetect
val errOrAvailableSiteDirs: Either[ErrorReport, List[File]] = for {
s1 <- errOrSiteFromCliArgs.right
s2 <- siteFromConfig.right
s3 <- Right(siteFromAutoDetect).right
} yield {
(s1 :: s2 :: s3 :: Nil) collect {
case Some(file) => file
}
}
errOrAvailableSiteDirs.right.flatMap {
case mostPreferredSiteDir :: xs => Right(mostPreferredSiteDir)
case Nil => Left(ErrorReport(noSiteFound("Could not find a website.")))
}
}

def siteFromConfig(implicit yamlConfig: S3_website_yml, config: Config, workingDirectory: File): Option[File] =
config
def siteFromConfig(implicit yamlConfig: S3_website_yml, config: Config, workingDirectory: File): Either[ErrorReport, Option[File]] = {
val siteConfig = config
.site
.map(new File(_))
.map { siteDir =>
if (siteDir.isAbsolute) siteDir
else new File(yamlConfig.file.getParentFile, siteDir.getPath)
}

siteConfig match {
case s @ Some(siteDir) =>
if (siteDir.exists())
Right(s)
else
Left(ErrorReport(noSiteFound(s"Could not find a website. (The site setting in s3_website.yml points to a non-existing file $siteDir)")))
case None =>
Right(None)
}
}
}
17 changes: 15 additions & 2 deletions src/test/scala/s3/website/S3WebsiteSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,24 @@ class S3WebsiteSpec extends Specification {
|ignore_on_server:
| - tags/笔记/test.html
""".stripMargin
push
push()
noDeletesOccurred must beTrue
}
}

"error message" should {
"be helpful when the site directory is missing" in new BasicSetup {
config = "site: nonexisting_site_dir"
val logEntries = new mutable.MutableList[String]
push(logCapturer = Some((logEntry: String) =>
logEntries += logEntry
))
logEntries must contain(
s"Could not find a website. (The site setting in s3_website.yml points to a non-existing file $siteDirectory/nonexisting_site_dir)"
)
}
}

"site in config" should {
"let the user deploy a site from a custom location" in new CustomSiteDirectory with EmptySite with MockAWS with DefaultRunMode {
config = s"site: $siteDirectory"
Expand All @@ -407,7 +420,7 @@ class S3WebsiteSpec extends Specification {
}

"not override the --site command-line switch" in new BasicSetup {
config = s"site: dir-that-does-not-exist"
config = s"site: ${System.getProperty("java.io.tmpdir")}"
setLocalFile(".vimrc") // This creates a file in the directory into which the --site CLI arg points
push()
sentPutObjectRequest.getKey must equalTo(".vimrc")
Expand Down

0 comments on commit d4766da

Please sign in to comment.