From f1f8fd7042fcddb43b09a09a70ea136efc2e01e6 Mon Sep 17 00:00:00 2001 From: Fenris Wolf Date: Sun, 23 Aug 2026 21:36:49 +0200 Subject: [PATCH] [fix] put --- source/helpers/ssh.py | 28 ++++++++++++++--- source/main.py | 72 +++++++++++++++++++++++++++++++------------ 2 files changed, 76 insertions(+), 24 deletions(-) diff --git a/source/helpers/ssh.py b/source/helpers/ssh.py index 885976c..4a01733 100644 --- a/source/helpers/ssh.py +++ b/source/helpers/ssh.py @@ -1,3 +1,6 @@ +import os as _os +import pathlib as _pathlib + import helpers.misc as __misc @@ -67,7 +70,14 @@ def sshconf_encode_bool( return ("yes" if value else "no") -def sshconf_encode_entry(name, entry, prefix): +def sshconf_encode_entry(name, entry, prefix, options = None): + options = ( + { + "keys_directory": _os.path.join(_pathlib.Path.home(), ".ssh", "keys"), + } + | + (options or {}) + ) lines = [] lines.append(__misc.string_coin("Host {{prefix}}{{name}}", {"prefix": prefix, "name": name})) if ("host" in entry): @@ -77,7 +87,7 @@ def sshconf_encode_entry(name, entry, prefix): if ("username" in entry): lines.append(__misc.string_coin("\tUser {{username}}", {"username": entry["username"]})) if ("key_name" in entry): - lines.append(__misc.string_coin("\tIdentityFile {{key_path}}", {"key_path": ("~/.ssh/keypairs/%s%s" % (prefix, entry["key_name"]))})) + lines.append(__misc.string_coin("\tIdentityFile {{key_path}}", {"key_path": _os.path.join(options["keys_directory"], ("%s%s.pub" % (prefix, entry["key_name"])))})) if ("proxy_jump" in entry): lines.append(__misc.string_coin("\tProxyJump {{proxy_jump}}", {"proxy_jump": entry["proxy_jump"]})) if ("proxy_command" in entry): @@ -90,13 +100,23 @@ def sshconf_encode_entry(name, entry, prefix): return "\n".join(lines) -def sshconf_encode(conf): +def sshconf_encode(conf, options = None): + options = ( + { + "keys_directory": _os.path.join(_pathlib.Path.home(), ".ssh", "keys"), + } + | + (options or {}) + ) return "\n".join( map( lambda pair: sshconf_encode_entry( pair[0], pair[1], - conf["settings"]["prefix"] + conf["settings"]["prefix"], + { + "keys_directory": options["keys_directory"], + } ), sorted( conf["entries"].items(), diff --git a/source/main.py b/source/main.py index 3446a43..5d5f8b3 100755 --- a/source/main.py +++ b/source/main.py @@ -3,6 +3,7 @@ import os as _os import json as _json import shutil as _shutil import argparse as _argparse +import pathlib as _pathlib import helpers.misc as __helpers_misc import helpers.ssh as __helpers_ssh @@ -184,21 +185,26 @@ todo: backup (see old put script) ''' def action_put( source_directory, - target_directory + target_directory, + target_file ): sshconf = "" + keys_directory = _os.path.join(target_directory, "public_keys") for group in _os.listdir(source_directory): path_temp = _os.path.join(source_directory, group) if ( - not + (not _os.path.isdir(path_temp)) + or ( _os.path.isdir(path_temp) and - (group == ".") - and - (group == "..") - and - (group == ".git") + ( + (group == ".") + or + (group == "..") + or + (group == ".git") + ) ) ): pass @@ -208,17 +214,32 @@ def action_put( conf_data = _json.loads(conf_content) # conf if True: - sshconf = (sshconf + "\n" + __helpers_ssh.sshconf_encode(conf_data)) + sshconf = ( + "%s\n%s" + % ( + sshconf, + __helpers_ssh.sshconf_encode( + conf_data, + { + "keys_directory": keys_directory, + } + ) + ) + ) # public keys if True: __helpers_misc.directory_create(target_directory) for name in _os.listdir(_os.path.join(source_directory, group, "keys")): - _shutil.copy( - _os.path.join(source_directory, group, "keys", name), - _os.path.join(target_directory, "%s%s" % (conf_data["settings"]["prefix"], name, )) - ) + if (not name.endswith(".pub")): + pass + else: + _os.makedirs(keys_directory, exist_ok = True) + _shutil.copy( + _os.path.join(source_directory, group, "keys", name), + _os.path.join(keys_directory, ("%s%s" % (conf_data["settings"]["prefix"], name, ))) + ) # private keys - if True: + if False: keepass_db_path = _os.path.join(source_directory, "private_keys.kdbx") _shutil.copy( keepass_db_path, @@ -263,9 +284,16 @@ def main(): "-t", "--target-directory", type = str, - default = "~/.ssh", + default = _os.path.join(_pathlib.Path.home(), ".ssh"), metavar = "", ) + argument_parser.add_argument( + "-f", + "--target-file", + type = str, + default = _os.path.join(_pathlib.Path.home(), ".ssh", "private_keys.kdbx"), + metavar = "", + ) argument_parser.add_argument( "-g", "--group", @@ -310,11 +338,14 @@ def main(): } ) elif (args.action == "key-generate"): - action_key_generate( - args.source_directory, - args.group, - args.name - ) + if (args.name is None): + raise ValueError("name required") + else: + action_key_generate( + args.source_directory, + args.group, + args.name + ) elif (args.action == "key-remove"): action_key_remove( args.source_directory, @@ -324,7 +355,8 @@ def main(): elif (args.action == "put"): action_put( args.source_directory, - args.target_directory + args.target_directory, + args.target_file ) else: raise ValueError("invalid action: %s", args.action)