diff --git a/lxcd.completion b/lxcd.completion index 668e022..2ef34f9 100644 --- a/lxcd.completion +++ b/lxcd.completion @@ -7,7 +7,7 @@ _lxcd_completion() { local commands="create clone rename enter list delete add stop start restart" if [ $cword -eq 1 ]; then - COMPREPLY=($(compgen -W "${commands}" -- "$cur")) + COMPREPLY=($(compgen -W "-v --verbose ${commands}" -- "$cur")) return 0 fi diff --git a/lxcd.py b/lxcd.py index d8a7363..e98b278 100755 --- a/lxcd.py +++ b/lxcd.py @@ -49,6 +49,8 @@ def detect_locale(): return os.environ.get("LANG", "en_US.UTF-8") +VERBOSE = False + def lxc_prefix(): """Return ['sudo'] if the user isn't root and isn't in the lxd group.""" if os.getuid() == 0: @@ -70,11 +72,17 @@ def run_cmd(cmd, check=True): def lxc_run(args, check=True): """Run an lxc command via run_cmd, auto-adding sudo if needed.""" - return run_cmd([*lxc_prefix(), "lxc", *args], check=check) + cmd = [*lxc_prefix(), "lxc", *args] + if VERBOSE: + print(f"+ {' '.join(cmd)}", file=sys.stderr) + return run_cmd(cmd, check=check) def lxc_subprocess(args, **kwargs): """Run an lxc command via subprocess.run, auto-adding sudo if needed.""" - return subprocess.run([*lxc_prefix(), "lxc", *args], **kwargs) + cmd = [*lxc_prefix(), "lxc", *args] + if VERBOSE: + print(f"+ {' '.join(cmd)}", file=sys.stderr) + return subprocess.run(cmd, **kwargs) def handle_list(args): print("Fetching lxcd container environments...") @@ -518,6 +526,7 @@ def handle_rename(args): def main(): parser = argparse.ArgumentParser(description="lxcd: A Distrobox-like wrapper for LXD (Sudo Optimized)") + parser.add_argument("-v", "--verbose", action="store_true", help="Show verbose command output") subparsers = parser.add_subparsers(dest="command", required=True) # Create command @@ -578,6 +587,9 @@ def main(): args = parser.parse_args() + global VERBOSE + VERBOSE = args.verbose + if args.command == "create": handle_create(args) elif args.command == "clone":