Skip to content

Commit

Permalink
Follow Debian guidelines for launching editors (#157)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
maxnordlund and Jip-Hop authored May 7, 2024
1 parent e6d8f74 commit 487b0cd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 16 additions & 12 deletions jlmkr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -123,7 +123,6 @@
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():
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 487b0cd

Please sign in to comment.