Skip to content

Commit

Permalink
Add extra target folder validation on cmta commands
Browse files Browse the repository at this point in the history
- `cmta linearize` was missing target folder validation
  altogether
- Added target folder validation on `cmta studentify` and
  `cmta linearize` commands
  • Loading branch information
eloots committed Jul 19, 2023
1 parent 8646b8a commit 8ca3b42
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 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 validateTargetFolder(mainRepository: File, target: File): Either[CmtError, Unit] =
val canonicalStudentifyBaseDirectory = target.getCanonicalPath
val canonicalMainRepository = mainRepository.getCanonicalPath
val mainRepositoryEqualsStudentifyBaseDirectory = canonicalStudentifyBaseDirectory == canonicalMainRepository
val StudentifyBaseDirectoryIsSubfolderOfmainRepository =
canonicalStudentifyBaseDirectory.startsWith(canonicalMainRepository)
(mainRepositoryEqualsStudentifyBaseDirectory, StudentifyBaseDirectoryIsSubfolderOfmainRepository) 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.validateTargetFolder
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,10 @@ 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)
_ <- validateTargetFolder(mainRepository, 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.validateTargetFolder
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,10 @@ 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)
_ <- validateTargetFolder(mainRepository, options.studentifyBaseDirectory.value)
} yield options
end validated
end given

Expand Down

0 comments on commit 8ca3b42

Please sign in to comment.