Follow Debian guidelines for launching editors (#157)

* Follow Debian guidelines for launching editors

When jailmaker wants to ask the user to edit a file, mostly a jail
config, it currently uses `nano`. This is not how a program is supposed
to work according to [Debian's guidelines]. This changes the hardcoded
`nano` to look up the correct editor to use using environmental
variables.

[1]: https://www.debian.org/doc/debian-policy/ch-customized-programs.html#editors-and-pagers

* Bump version to 1.3.0

---------

Co-authored-by: Jip-Hop <2871973+Jip-Hop@users.noreply.github.com>
This commit is contained in:
Max Nordlund 2024-05-07 20:02:36 +02:00 committed by GitHub
parent e6d8f74c51
commit 487b0cde68
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 13 deletions

View File

@ -114,7 +114,7 @@ jlmkr exec myjail bash -c 'echo test; echo $RANDOM;'
jlmkr edit myjail
```
Once you've created a jail, it will exist in a directory inside the `jails` dir next to `jlmkr.py`. For example `/mnt/mypool/jailmaker/jails/myjail` if you've named your jail `myjail`. You may edit the jail configuration file, e.g. using the `jlmkr edit myjail` command (which uses the nano text editor). You'll have to stop the jail and start it again with `jlmkr` for these changes to take effect.
Once you've created a jail, it will exist in a directory inside the `jails` dir next to `jlmkr.py`. For example `/mnt/mypool/jailmaker/jails/myjail` if you've named your jail `myjail`. You may edit the jail configuration file using the `jlmkr edit myjail` command. This opens the config file in your favorite editor, as determined by following [Debian's guidelines](https://www.debian.org/doc/debian-policy/ch-customized-programs.html#editors-and-pagers) on the matter. You'll have to stop the jail and start it again with `jlmkr` for these changes to take effect.
### Remove Jail

View File

@ -4,7 +4,7 @@
with full access to all files via bind mounts, \
thanks to systemd-nspawn!"""
__version__ = "1.2.1"
__version__ = "1.3.0"
__disclaimer__ = """USE THIS SCRIPT AT YOUR OWN RISK!
IT COMES WITHOUT WARRANTY AND IS NOT SUPPORTED BY IXSYSTEMS."""
@ -123,7 +123,6 @@ SCRIPT_NAME = os.path.basename(SCRIPT_PATH)
SCRIPT_DIR_PATH = os.path.dirname(SCRIPT_PATH)
COMMAND_NAME = os.path.basename(__file__)
SYMLINK_NAME = "jlmkr"
TEXT_EDITOR = "nano"
# Only set a color if we have an interactive tty
if sys.stdout.isatty():
@ -1021,6 +1020,17 @@ def agree_with_default(config, key, question):
config.my_set(key, agree(question, default_answer))
def get_text_editor():
def get_from_environ(key):
if editor := os.environ.get(key):
return shutil.which(editor)
return get_from_environ("VISUAL") \
or get_from_environ("EDITOR") \
or shutil.which("editor") \
or shutil.which("/usr/bin/editor") \
or "nano"
def interactive_config():
config = KeyValueParser()
config.read_string(DEFAULT_CONFIG)
@ -1049,7 +1059,7 @@ def interactive_config():
input("Press Enter to open the text editor.")
with tempfile.NamedTemporaryFile(mode="w+t") as f:
subprocess.call([TEXT_EDITOR, f.name])
subprocess.call([get_text_editor(), f.name])
f.seek(0)
# Start over with a new KeyValueParser to parse user config
config = KeyValueParser()
@ -1506,19 +1516,13 @@ def edit_jail(jail_name):
return 1
jail_config_path = get_jail_config_path(jail_name)
if not shutil.which(TEXT_EDITOR):
eprint(
f"Unable to edit config file: {jail_config_path}.",
f"\nThe {TEXT_EDITOR} text editor is not available",
)
return 1
returncode = subprocess.run(
[TEXT_EDITOR, get_jail_config_path(jail_name)]
[get_text_editor(), jail_config_path]
).returncode
if returncode != 0:
eprint("An error occurred while editing the jail config.")
eprint(f"An error occurred while editing {jail_config_path}.")
return returncode
if jail_is_running(jail_name):
@ -1922,7 +1926,7 @@ def main():
),
dict(
name="edit",
help=f"edit jail config with {TEXT_EDITOR} text editor",
help=f"edit jail config with {get_text_editor()} text editor",
func=edit_jail,
),
dict(