From d4b78bcff70613540a01deaf3d3a5bc6e37e804f Mon Sep 17 00:00:00 2001
From: Thomas Schneider <thomas@fsmpi.rwth-aachen.de>
Date: Wed, 12 Apr 2023 22:40:18 +0200
Subject: [PATCH] Implement get-config command

---
 src/nctool/cli.py | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/src/nctool/cli.py b/src/nctool/cli.py
index 7b47333..4d06d0b 100644
--- a/src/nctool/cli.py
+++ b/src/nctool/cli.py
@@ -107,3 +107,35 @@ def get_models(ctx: click.Context, device, host, port, username, destination):
 
             with open(os.path.join(destination, filename), "w") as fp:
                 fp.write(r.data)
+
+
+@main.command()
+@click.option("-D", "--device", help="Device defined in config")
+@click.option("-o", "--output", type=click.File("w"), default="-")
+@click.option(
+    "--source",
+    type=click.Choice(["running", "startup", "candidate"]),
+    default="running",
+    help="Config store",
+)
+@click.argument("xpath")
+def get_config(device, output, source, xpath):
+    """Get config from device"""
+
+    dev = cfg["devices"][device]
+
+    try:
+        password = getpass(prompt="Password (C-d for none/agent only): ")
+    except EOFError:
+        password = None
+
+    with manager.connect(
+        host=dev.get("hostname"),
+        port=dev.get("port", 830),  # TODO
+        username=dev.get("user"),
+        password=password,
+        timeout=90,
+        device_params=dev.get("params"),
+    ) as m:
+        res = m.get_config(source, filter=("xpath", (dev.get("namespaces", {}), xpath)))
+        output.write(res.data_xml)
-- 
GitLab