Skip to content

Commit

Permalink
Sanitise snapshot name
Browse files Browse the repository at this point in the history
  • Loading branch information
keynmol committed Feb 13, 2024
1 parent ee35f84 commit fc96b17
Show file tree
Hide file tree
Showing 22 changed files with 27 additions and 18 deletions.

This file was deleted.

@@ -0,0 +1 @@
hello - more stuff

This file was deleted.

@@ -0,0 +1 @@
hello - more stuff

This file was deleted.

@@ -0,0 +1 @@
hello - more stuff

This file was deleted.

@@ -0,0 +1 @@
hello - more stuff

This file was deleted.

@@ -0,0 +1 @@
hello - more stuff

This file was deleted.

@@ -0,0 +1 @@
hello - more stuff

This file was deleted.

@@ -0,0 +1 @@
hello - more stuff

This file was deleted.

@@ -0,0 +1 @@
hello - more stuff

This file was deleted.

@@ -0,0 +1 @@
hello - more stuff
27 changes: 18 additions & 9 deletions modules/snapshots-runtime/src/main/scala/Snapshots.scala
Expand Up @@ -25,38 +25,47 @@ case class Snapshots(
private def getTmpFile(name: String) =
tmpLocation.resolve(name)

def sanitiseSnapshotName(name: String): String = {
name.replaceAll("[^a-zA-Z0-9_\\-]", "_")
}

def recordChanges(name: String, contents: String, diff: String): Unit = {
val tmpName = name + "__snap.new"
val tmpDiff = name + "__snap.new.diff"
val file = location.resolve(name)
val saneName = sanitiseSnapshotName(name)
val tmpName = saneName + "__snap.new"
val tmpDiff = saneName + "__snap.new.diff"
val file = location.resolve(saneName)
val tmpFile = tmpLocation.resolve(tmpName)
val tmpFileDiff = tmpLocation.resolve(tmpDiff)

val snapContents =
name + "\n" + file + "\n" + contents
saneName + "\n" + file + "\n" + contents

tmpLocation.createDirectories()
tmpFile.fileWriteContents(snapContents)
tmpFileDiff.fileWriteContents(diff)
}

def write(name: String, contents: String): Unit = {
val file = location.resolve(name)
val saneName = sanitiseSnapshotName(name)
val file = location.resolve(saneName)
location.createDirectories()
file.fileWriteContents(contents)
}

def clearChanges(name: String): Unit = {
val tmpName = name + "__snap.new"
val tmpDiff = name + "__snap.new.diff"
val saneName = sanitiseSnapshotName(name)
val tmpName = saneName + "__snap.new"
val tmpDiff = saneName + "__snap.new.diff"
val tmpFile = getTmpFile(tmpName)
val tmpFileDiff = getTmpFile(tmpDiff)

tmpFileDiff.delete()
tmpFile.delete()
}

def read(name: String): Option[String] =
location.resolve(name).readFileContents()
def read(name: String): Option[String] = {
val saneName = sanitiseSnapshotName(name)
location.resolve(saneName).readFileContents()
}

}

0 comments on commit fc96b17

Please sign in to comment.