Skip to content

Commit

Permalink
fix(anta.cli): Allow commands in snapshot to use / and protect filename
Browse files Browse the repository at this point in the history
  • Loading branch information
titom73 committed Jan 17, 2024
1 parent 723f69d commit 475b477
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions anta/cli/exec/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import itertools
import json
import logging
import re
from pathlib import Path
from typing import Literal

Expand All @@ -22,7 +23,7 @@
from anta.models import AntaCommand

EOS_SCHEDULED_TECH_SUPPORT = "/mnt/flash/schedule/tech-support"

INVALID_CHAR = "`~!@#$/"
logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -61,16 +62,17 @@ async def collect_commands(
async def collect(dev: AntaDevice, command: str, outformat: Literal["json", "text"]) -> None:
outdir = Path() / root_dir / dev.name / outformat
outdir.mkdir(parents=True, exist_ok=True)
safe_command = re.sub(r"(/|\|$)", "_", command)
c = AntaCommand(command=command, ofmt=outformat)
await dev.collect(c)
if not c.collected:
logger.error(f"Could not collect commands on device {dev.name}: {c.errors}")
return
if c.ofmt == "json":
outfile = outdir / f"{command}.json"
outfile = outdir / f"{safe_command}.json"
content = json.dumps(c.json_output, indent=2)
elif c.ofmt == "text":
outfile = outdir / f"{command}.log"
outfile = outdir / f"{safe_command}.log"
content = c.text_output
with outfile.open(mode="w", encoding="UTF-8") as f:
f.write(content)
Expand Down

0 comments on commit 475b477

Please sign in to comment.