-
-
Notifications
You must be signed in to change notification settings - Fork 949
Expand file tree
/
Copy pathcommand.py
More file actions
31 lines (27 loc) · 1.15 KB
/
command.py
File metadata and controls
31 lines (27 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from pathlib import Path
from click import UsageError
import json
class Command:
def __init__(self, command_path: Path):
self.command_path = command_path
self.command_path.mkdir(parents=True, exist_ok=True)
def get_last_command(self) -> tuple[str, str]:
"""
get the last command and output from the command path
"""
last_command_file = self.command_path / "last_command.json"
if not last_command_file.exists():
raise UsageError("No last command and output found.")
with open(last_command_file, "r", encoding="utf-8") as file:
data = json.load(file)
command = data.get("command", "")
output = data.get("output", "")
return command, output
def set_last_command(self, command: str, output: str) -> None:
"""
set the last command and output to the command path
"""
last_command_file = self.command_path / "last_command.json"
with open(last_command_file, "w", encoding="utf-8") as file:
data = {"command": command, "output": output}
json.dump(data, file, ensure_ascii=False)