From 2fbc934c6427fa0dfe4dc846668b3ab7d2ff83d3 Mon Sep 17 00:00:00 2001
From: Dilawar Singh <dilawar@subcom.tech>
Date: Sun, 9 Oct 2022 09:40:10 +0530
Subject: [PATCH] feat: add uuid1 when command line is used.

---
 bitia/helper.py   | 13 +++++++++++--
 tests/test_cli.py |  5 ++++-
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/bitia/helper.py b/bitia/helper.py
index bf2acfe..7785111 100644
--- a/bitia/helper.py
+++ b/bitia/helper.py
@@ -5,9 +5,11 @@ __email__ = "dilawar@subcom.tech"
 
 import hashlib
 import sys
+import uuid
 import zipfile
 import shutil
 from pathlib import Path
+from datetime import datetime
 import tempfile
 import typing as T
 
@@ -84,7 +86,9 @@ def create_pipeline_from_single_script(script: Path) -> Path:
     return prepare_archive(pipeline_dir)
 
 
-def create_pipeline_from_command(cmd: str) -> Path:
+def create_pipeline_from_command(
+    cmd: str, add_timestamp: bool = True, add_uuid1: bool = True
+) -> Path:
     """Create a pipeline from user input.
 
     Returns
@@ -94,7 +98,12 @@ def create_pipeline_from_command(cmd: str) -> Path:
     pipeline_dir = Path(tempfile.mkdtemp(prefix="bitia_"))
     pipeline_file = pipeline_dir / bconfig.BITIA_MAIN_SCRIPT_NAME
     with pipeline_file.open("w", newline="\n") as outf:
-        outf.write(f"#!/bin/sh\n\n{cmd}")
+        outf.writeline("#!/bin/sh")
+        if add_timestamp:
+            outf.writeline(f"# timestamp={datetime.now().isoformat()}")
+        if add_uuid1:
+            outf.writeline(f"# uuid={uuid.uuid1()}")
+        outf.writeline(f"{cmd}")
     logger.info("Wrote pipeline %s", pipeline_file.read_text())
     return prepare_archive(pipeline_dir)
 
diff --git a/tests/test_cli.py b/tests/test_cli.py
index fe4885e..30b3a9a 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -2,8 +2,11 @@ from bitia.__main__ import run_pipeline
 
 
 def test_cli_sanity():
+    nlines = 0
     for line in run_pipeline("ls -ltr /"):
-        print(line)
+        nlines += 1
+        print(11, line)
+    assert nlines > 10, "Too few lines"
 
 
 if __name__ == "__main__":
-- 
GitLab