Skip to content

Commit 09c1efd

Browse files
authored
detect installation root dir via marker file (#1404)
* use current working dir for workspace some users may want to install joern/ocular system-wide in a read-only directory, and have their workspace e.g. in their user home re https://discord.com/channels/832209896089976854/832214243230744626/887106423341875200 * make console/basedir configurable, mostly for tests * detect installation root dir via marker file #1387 introduced the assumption that the root directory is always two levels up from the lib dir. That's not true for ocular, and a little brittle anyway. This PR changes the behaviour to instead search for a marker file. * adjust test
1 parent 3961639 commit 09c1efd

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

.installation_root

Whitespace-only changes.

codepropertygraph/src/main/scala/io/shiftleft/utils/ProjectRoot.scala

+3
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,7 @@ object ProjectRoot {
2828
else ???
2929
}
3030

31+
def find: File =
32+
File(findRelativePath)
33+
3134
}

console/src/main/scala/io/shiftleft/console/ConsoleConfig.scala

+16-3
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,28 @@ import scala.collection.mutable
1010
* @param environment A map of system environment variables.
1111
* */
1212
class InstallConfig(environment: Map[String, String] = sys.env) {
13-
var rootPath: File = {
13+
lazy val rootPath: File = {
1414
if (environment.contains("SHIFTLEFT_OCULAR_INSTALL_DIR")) {
1515
environment("SHIFTLEFT_OCULAR_INSTALL_DIR").toFile
1616
} else {
1717
val uriToLibDir = classOf[io.shiftleft.console.InstallConfig].getProtectionDomain.getCodeSource.getLocation.toURI
18-
val pathToLibDir = new java.io.File(uriToLibDir).toPath
19-
pathToLibDir.getParent.getParent.toFile.toScala
18+
val pathToLibDir = File(uriToLibDir)
19+
findRootDirectory(pathToLibDir).getOrElse(throw new AssertionError(s"""unable to find root installation directory
20+
| context: tried to find marker file `$rootDirectoryMarkerFilename`
21+
| started search in $pathToLibDir and searched $maxSearchDepth directories upwards""".stripMargin))
2022
}
2123
}
24+
25+
private val rootDirectoryMarkerFilename = ".installation_root"
26+
private val maxSearchDepth = 10
27+
private def findRootDirectory(currentSearchDir: File, currentSearchDepth: Int = 0): Option[File] = {
28+
if (currentSearchDir.list.map(_.name).contains(rootDirectoryMarkerFilename))
29+
Some(currentSearchDir)
30+
else if (currentSearchDepth < maxSearchDepth && currentSearchDir.parentOption.isDefined)
31+
findRootDirectory(currentSearchDir.parent)
32+
else
33+
None
34+
}
2235
}
2336

2437
object InstallConfig {

console/src/test/scala/io/shiftleft/console/ConsoleConfigTest.scala

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package io.shiftleft.console
22

3-
import better.files.{File, FileExtensions}
3+
import better.files.File
4+
import io.shiftleft.utils.ProjectRoot
45
import org.scalatest.matchers.should.Matchers
56
import org.scalatest.wordspec.AnyWordSpec
67

78
class ConsoleConfigTest extends AnyWordSpec with Matchers {
89
"An InstallConfig" should {
9-
"set the rootPath to parent of directory containing jar by default" in {
10+
"set the rootPath to directory containing `.installation_root` by default" in {
1011
val config = new InstallConfig(environment = Map.empty)
11-
val uriToLibDir = classOf[io.shiftleft.console.InstallConfig].getProtectionDomain.getCodeSource.getLocation.toURI
12-
val pathToLibDir = new java.io.File(uriToLibDir).toPath
13-
config.rootPath shouldBe pathToLibDir.getParent.getParent.toFile.toScala
12+
config.rootPath shouldBe ProjectRoot.find
1413
}
1514

1615
"set the rootPath to SHIFTLEFT_OCULAR_INSTALL_DIR if it is defined" in {

0 commit comments

Comments
 (0)