Add withTempDir function
Sometimes it is required to execute a block of code inside
a temporaty directory that will be removed afterwards.
This commit adds withTempDir function that does exactly this.
Related-Prod: PRODX-5657
Change-Id: I79d3ac92f6a03bb32c10cb81e5c304e119c88414
diff --git a/src/com/mirantis/mk/Common.groovy b/src/com/mirantis/mk/Common.groovy
index 65a847e..5027041 100644
--- a/src/com/mirantis/mk/Common.groovy
+++ b/src/com/mirantis/mk/Common.groovy
@@ -1143,3 +1143,25 @@
return readYaml(text: kwargs['text'])
}
}
+
+/**
+ * withTempDir runs a block of code inside a new temporary directory.
+ * This temp dir will be removed when finished.
+ * @param: closure Closure - code block to be executed in a tmp directory
+ *
+ * Example:
+ *
+ * withTempDir {
+ * sh "pwd"
+ * }
+ *
+ **/
+void withTempDir(Closure closure) {
+ dir(pwd(tmp: true)) {
+ try {
+ closure()
+ } finally {
+ deleteDir()
+ }
+ }
+}