Skip to content

Commit

Permalink
Improve cmtc install error messages/remove unused code (#241)
Browse files Browse the repository at this point in the history
* Improve `cmtc install` error messages/remove unused code

* Add extra target folder validation on cmta commands

- `cmta linearize` was missing target folder validation
  altogether
- Added target folder validation on `cmta studentify` and
  `cmta linearize` commands

* Better naming
  • Loading branch information
eloots authored Jul 19, 2023
1 parent 5e44a9c commit c978ccc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.lunatech.cmt.admin

import com.lunatech.cmt.CmtError
import java.io.File
import com.lunatech.cmt.toExecuteCommandErrorMessage

def validateDestinationFolder(mainRepository: File, destination: File): Either[CmtError, Unit] =
val canonicalTarget = destination.getCanonicalPath
val canonicalMainRepository = mainRepository.getCanonicalPath
val destinationEqualsMainRepository = canonicalTarget == canonicalMainRepository
val destinationIsSubfolderOfmainRepository =
canonicalTarget.startsWith(canonicalMainRepository)
(destinationEqualsMainRepository, destinationIsSubfolderOfmainRepository) match {
case (true, _) =>
Left("destination folder cannot be the same as the main repository root folder".toExecuteCommandErrorMessage)
case (_, true) =>
Left("destination folder cannot be a subfolder of the main repository".toExecuteCommandErrorMessage)
case _ =>
Right(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.lunatech.cmt.{CMTaConfig, CmtError, printResult, toConsoleGreen}
import com.lunatech.cmt.admin.Domain.{ForceDeleteDestinationDirectory, LinearizeBaseDirectory, MainRepository}
import com.lunatech.cmt.admin.cli.ArgParsers.{forceDeleteDestinationDirectoryArgParser, linearizeBaseDirectoryArgParser}
import com.lunatech.cmt.admin.cli.SharedOptions
import com.lunatech.cmt.admin.validateDestinationFolder
import com.lunatech.cmt.core.cli.CmtCommand
import com.lunatech.cmt.core.execution.Executable
import com.lunatech.cmt.core.validation.Validatable
Expand All @@ -30,7 +31,12 @@ object Linearize:
given Validatable[Linearize.Options] with
extension (options: Linearize.Options)
def validated(): Either[CmtError, Linearize.Options] =
Right(options)
for {
mainRepository <- resolveMainRepoPath(options.shared.mainRepository.value)
_ <- validateDestinationFolder(
mainRepository = mainRepository,
destination = options.linearizeBaseDirectory.value)
} yield options
end given

given Executable[Linearize.Options] with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.lunatech.cmt.core.execution.Executable
import com.lunatech.cmt.{CMTaConfig, CmtError, printResult, toConsoleGreen}
import sbt.io.IO as sbtio
import sbt.io.syntax.*
import com.lunatech.cmt.admin.*
import com.lunatech.cmt.admin.validateDestinationFolder
import com.lunatech.cmt.admin.cli.SharedOptions
import com.lunatech.cmt.core.validation.Validatable
import com.lunatech.cmt.admin.cli.ArgParsers.{
Expand All @@ -22,7 +22,6 @@ import com.lunatech.cmt.admin.cli.ArgParsers.{
}
import com.lunatech.cmt.core.GeneratorInfo
import com.lunatech.cmt.core.cli.CmtCommand
import com.lunatech.cmt.toExecuteCommandErrorMessage

object Studentify:

Expand All @@ -46,11 +45,12 @@ object Studentify:
given Validatable[Studentify.Options] with
extension (options: Studentify.Options)
def validated(): Either[CmtError, Studentify.Options] =
if (options.studentifyBaseDirectory.value.equals(options.shared.mainRepository.value)) {
Left("main repository cannot be the same as the destination directory".toExecuteCommandErrorMessage)
} else {
Right(options)
}
for {
mainRepository <- resolveMainRepoPath(options.shared.mainRepository.value)
_ <- validateDestinationFolder(
mainRepository = mainRepository,
destination = options.studentifyBaseDirectory.value)
} yield options
end validated
end given

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,7 @@ object Install:
forceDelete: Boolean,
deleteZipAfterInstall: Boolean = false): Either[CmtError, String] =
val installResult = Using(TmpDir()) { case TmpDir(tmpDir) =>
sbtio
.unzip(zipFile.value, tmpDir)
.map(sbtio.relativizeFile(tmpDir, _))
.collect { case Some(f) => f }
.filterNot(_.getName.startsWith("__MACOSX"))
sbtio.unzip(zipFile.value, tmpDir)
sbtio.delete(tmpDir / "__MACOSX") // Hack for MacOSX
val zipRootFolders = sbtio.listFiles(tmpDir).to(Vector)
if zipRootFolders.size == 1 then
Expand All @@ -105,7 +101,9 @@ object Install:
installResult match {
case Success(Right(ok)) => ok.asRight[CmtError]
case Success(failure) => failure
case _ => s"Unexpected error".toExecuteCommandErrorMessage.asLeft
case Failure(e) =>
s"""Unexpected error(${e.getMessage})
| ${zipFile.value} may be corrupt""".toExecuteCommandErrorMessage.asLeft
}

private def extractTag(lsFilesTagLine: String): String =
Expand Down

0 comments on commit c978ccc

Please sign in to comment.