forked from clan/clan-core
Merge pull request 'clan-cli: Add input arg to cmd.run. Replace subprocess.run in password_store' (#1533) from Qubasa/clan-core:Qubasa-main into main
This commit is contained in:
commit
320fb776ea
@ -101,13 +101,19 @@ TIME_TABLE = TimeTable()
|
|||||||
def run(
|
def run(
|
||||||
cmd: list[str],
|
cmd: list[str],
|
||||||
*,
|
*,
|
||||||
|
input: bytes | None = None, # noqa: A002
|
||||||
env: dict[str, str] | None = None,
|
env: dict[str, str] | None = None,
|
||||||
cwd: Path = Path.cwd(),
|
cwd: Path = Path.cwd(),
|
||||||
log: Log = Log.STDERR,
|
log: Log = Log.STDERR,
|
||||||
check: bool = True,
|
check: bool = True,
|
||||||
error_msg: str | None = None,
|
error_msg: str | None = None,
|
||||||
) -> CmdOut:
|
) -> CmdOut:
|
||||||
glog.debug(f"$: {shlex.join(cmd)} \nCaller: {get_caller()}")
|
if input:
|
||||||
|
glog.debug(
|
||||||
|
f"""$: echo "{input.decode('utf-8')}" | {shlex.join(cmd)} \nCaller: {get_caller()}"""
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
glog.debug(f"$: {shlex.join(cmd)} \nCaller: {get_caller()}")
|
||||||
tstart = datetime.now()
|
tstart = datetime.now()
|
||||||
|
|
||||||
# Start the subprocess
|
# Start the subprocess
|
||||||
@ -120,7 +126,10 @@ def run(
|
|||||||
)
|
)
|
||||||
stdout_buf, stderr_buf = handle_output(process, log)
|
stdout_buf, stderr_buf = handle_output(process, log)
|
||||||
|
|
||||||
rc = process.wait()
|
if input:
|
||||||
|
process.communicate(input)
|
||||||
|
else:
|
||||||
|
process.wait()
|
||||||
tend = datetime.now()
|
tend = datetime.now()
|
||||||
|
|
||||||
global TIME_TABLE
|
global TIME_TABLE
|
||||||
@ -136,7 +145,7 @@ def run(
|
|||||||
msg=error_msg,
|
msg=error_msg,
|
||||||
)
|
)
|
||||||
|
|
||||||
if check and rc != 0:
|
if check and process.returncode != 0:
|
||||||
raise ClanCmdError(cmd_out)
|
raise ClanCmdError(cmd_out)
|
||||||
|
|
||||||
return cmd_out
|
return cmd_out
|
||||||
|
@ -32,6 +32,7 @@ def read_multiline_input(prompt: str = "Finish with Ctrl-D") -> str:
|
|||||||
"""
|
"""
|
||||||
print(prompt, flush=True)
|
print(prompt, flush=True)
|
||||||
proc = subprocess.run(["cat"], stdout=subprocess.PIPE, text=True)
|
proc = subprocess.run(["cat"], stdout=subprocess.PIPE, text=True)
|
||||||
|
log.info("Input received. Processing...")
|
||||||
return proc.stdout
|
return proc.stdout
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ import os
|
|||||||
import subprocess
|
import subprocess
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from clan_cli.cmd import Log, run
|
||||||
from clan_cli.machines.machines import Machine
|
from clan_cli.machines.machines import Machine
|
||||||
from clan_cli.nix import nix_shell
|
from clan_cli.nix import nix_shell
|
||||||
|
|
||||||
@ -15,25 +16,25 @@ class SecretStore(SecretStoreBase):
|
|||||||
def set(
|
def set(
|
||||||
self, service: str, name: str, value: bytes, groups: list[str]
|
self, service: str, name: str, value: bytes, groups: list[str]
|
||||||
) -> Path | None:
|
) -> Path | None:
|
||||||
subprocess.run(
|
run(
|
||||||
nix_shell(
|
nix_shell(
|
||||||
["nixpkgs#pass"],
|
["nixpkgs#pass"],
|
||||||
["pass", "insert", "-m", f"machines/{self.machine.name}/{name}"],
|
["pass", "insert", "-m", f"machines/{self.machine.name}/{name}"],
|
||||||
),
|
),
|
||||||
input=value,
|
input=value,
|
||||||
check=True,
|
log=Log.BOTH,
|
||||||
|
error_msg=f"Failed to insert secret {name}",
|
||||||
)
|
)
|
||||||
return None # we manage the files outside of the git repo
|
return None # we manage the files outside of the git repo
|
||||||
|
|
||||||
def get(self, service: str, name: str) -> bytes:
|
def get(self, service: str, name: str) -> bytes:
|
||||||
return subprocess.run(
|
return run(
|
||||||
nix_shell(
|
nix_shell(
|
||||||
["nixpkgs#pass"],
|
["nixpkgs#pass"],
|
||||||
["pass", "show", f"machines/{self.machine.name}/{name}"],
|
["pass", "show", f"machines/{self.machine.name}/{name}"],
|
||||||
),
|
),
|
||||||
check=True,
|
error_msg=f"Failed to get secret {name}",
|
||||||
stdout=subprocess.PIPE,
|
).stdout.encode("utf-8")
|
||||||
).stdout
|
|
||||||
|
|
||||||
def exists(self, service: str, name: str) -> bool:
|
def exists(self, service: str, name: str) -> bool:
|
||||||
password_store = os.environ.get(
|
password_store = os.environ.get(
|
||||||
@ -48,7 +49,7 @@ class SecretStore(SecretStoreBase):
|
|||||||
)
|
)
|
||||||
hashes = []
|
hashes = []
|
||||||
hashes.append(
|
hashes.append(
|
||||||
subprocess.run(
|
run(
|
||||||
nix_shell(
|
nix_shell(
|
||||||
["nixpkgs#git"],
|
["nixpkgs#git"],
|
||||||
[
|
[
|
||||||
@ -61,13 +62,15 @@ class SecretStore(SecretStoreBase):
|
|||||||
f"machines/{self.machine.name}",
|
f"machines/{self.machine.name}",
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
stdout=subprocess.PIPE,
|
check=False,
|
||||||
).stdout.strip()
|
)
|
||||||
|
.stdout.encode("utf-8")
|
||||||
|
.strip()
|
||||||
)
|
)
|
||||||
for symlink in Path(password_store).glob(f"machines/{self.machine.name}/**/*"):
|
for symlink in Path(password_store).glob(f"machines/{self.machine.name}/**/*"):
|
||||||
if symlink.is_symlink():
|
if symlink.is_symlink():
|
||||||
hashes.append(
|
hashes.append(
|
||||||
subprocess.run(
|
run(
|
||||||
nix_shell(
|
nix_shell(
|
||||||
["nixpkgs#git"],
|
["nixpkgs#git"],
|
||||||
[
|
[
|
||||||
@ -80,8 +83,10 @@ class SecretStore(SecretStoreBase):
|
|||||||
str(symlink),
|
str(symlink),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
stdout=subprocess.PIPE,
|
check=False,
|
||||||
).stdout.strip()
|
)
|
||||||
|
.stdout.encode("utf-8")
|
||||||
|
.strip()
|
||||||
)
|
)
|
||||||
|
|
||||||
# we sort the hashes to make sure that the order is always the same
|
# we sort the hashes to make sure that the order is always the same
|
||||||
|
@ -30,7 +30,7 @@ if ! check_remote "$remoteUpstream"; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
treefmt -C "$root_dir"
|
treefmt --fail-on-change -C "$root_dir"
|
||||||
|
|
||||||
upstream_url=$(git remote get-url "$remoteUpstream")
|
upstream_url=$(git remote get-url "$remoteUpstream")
|
||||||
set -x
|
set -x
|
||||||
@ -61,4 +61,4 @@ tea pr create \
|
|||||||
--head "$user:$tempRemoteBranch" \
|
--head "$user:$tempRemoteBranch" \
|
||||||
--title "$firstLine" \
|
--title "$firstLine" \
|
||||||
--description "$rest" \
|
--description "$rest" \
|
||||||
"$@"
|
"$@"
|
||||||
|
Loading…
Reference in New Issue
Block a user