From 2ce8bc5fd8ab8505205a315e29ada12c829743fa Mon Sep 17 00:00:00 2001 From: Q Date: Sat, 2 Mar 2024 17:35:57 +0200 Subject: [PATCH] update tunnelier again --- py-packages/sshtunnelier/setup.py | 2 +- .../sshtunnelier/sshtunnelier/__init__.py | 51 +++++++++++-------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/py-packages/sshtunnelier/setup.py b/py-packages/sshtunnelier/setup.py index 03cd4af..4b5ecbb 100644 --- a/py-packages/sshtunnelier/setup.py +++ b/py-packages/sshtunnelier/setup.py @@ -23,6 +23,6 @@ setup( ] }, install_requires=[ - "psutil", + "psutil", "pyyaml" ], ) diff --git a/py-packages/sshtunnelier/sshtunnelier/__init__.py b/py-packages/sshtunnelier/sshtunnelier/__init__.py index 60ba505..3687abe 100644 --- a/py-packages/sshtunnelier/sshtunnelier/__init__.py +++ b/py-packages/sshtunnelier/sshtunnelier/__init__.py @@ -9,32 +9,33 @@ import sys from argparse import ArgumentError, ArgumentParser import psutil +import yaml -__version__ = "2024.01.07" +__version__ = "2024.03.02" CONFDIR = os.path.expanduser("~/.config/ssh-tunnelier") -CONF = os.path.join(CONFDIR, "tunnels.json") +CONF_OLD = os.path.join(CONFDIR, "tunnels.json") +CONF = os.path.join(CONFDIR, "tunnels.yaml") # Just over a year in minutes MAGIC_TIME = 525601 LOCALHOSTSYMBOL = "💻" -EXAMPLE_CONFIG = """{ - "test": { - "host": "host to connect, or defaults to name", - "options": "-4 and other ssh options", - "auto-connect": false, - "tunnels": [ - { - "local_port": 1111, - "remote_port": 8080, - "remote_address": "localhost", - "reverse": false, - "comment": "`comment`, `reverse` and `remote_address` are not required" - } - ] - } +EXAMPLE_CONFIG = { + "test": { + "host": "host to connect, or defaults to name", + "options": "-4 and other ssh options", + "auto-connect": False, + "tunnels": [ + { + "local_port": 1111, + "remote_port": 8080, + "remote_address": "localhost", + "reverse": False, + "comment": "`comment`, `reverse` and `remote_address` are not required", + } + ], + } } -""" def args(): @@ -106,11 +107,17 @@ def load_config(): try: os.makedirs(CONFDIR, exist_ok=True) if not os.path.exists(CONF): - print("Creating example config: " + CONF) - with open(CONF, "w") as fp: - fp.write(EXAMPLE_CONFIG) + if os.path.exists(CONF_OLD): + with open(CONF_OLD, "r") as fp: + config = json.load(fp) + with open(CONF, "w") as fp: + yaml.dump(config, fp, sort_keys=False) + else: + print("Creating example config: " + CONF) + with open(CONF, "w") as fp: + yaml.dump(EXAMPLE_CONFIG, fp) with open(CONF, "r") as fp: - config = json.load(fp) + config = yaml.safe_load(fp) for name in config: config[name]["host"] = config[name].get("host", name)