Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds tutor config edit #1099

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

tecoholic
Copy link

@tecoholic tecoholic commented Jul 24, 2024

Quickly launch the default YAML editor for editing the config.yml file.

Background

As a developer working with multiple projects, I am constantly changing the tutor config and re-running the tutor dev launch to update the devstack. While it is as simple as running vim $(tutor config printroot)/config.yml, I often found myself wanting to simply express this better using tutor config edit. So this is an attempt at cross-platform solution doing the same. It might not be the best solution, but something to start with if someone else finds this useful and improves their DevEx.

Caveat: I have only tested this on Linux (both open and xdg-open works in mine, so it could be said Unix).

@click.command(name="edit", help="Edit config.yml of the current environment")
@click.pass_obj
def edit(context: Context) -> None:
config_file = os.path.join(context.root, "config.yml")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use config.config_path(context.root) instead.

elif which("start"): # Windows
open_cmd = ["start", '""', config_file]
else:
click.echo(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This command should fail if neither open commands are available. You could for instance raise exceptions.TutorError.


open_cmd = None

if which("open"): # MacOS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's amend the comment. open is also available on Ubuntu, for instance.

I have another, more important problem with this command. According to my testing, when I run open file.txt on a headless server (for instance: a remote production server), it opens a read-only editor. How can we fix this command to open a write-capable editor? (xdg-open is not available on servers without a Desktop) Note that $EDITOR is set to "vim" on this server.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@regisb I wanted to test this scenario to evaluate the options. However, the server image I used (ubuntu based) didn't have a open at all. And my desktop with Ubuntu seems to just ship xdg-open as open.

❯ which open
/usr/bin/open

❯ open --version
xdg-open 1.1.3

when I run open file.txt on a headless server (for instance: a remote production server), it opens a read-only editor.

Which version of open is this, and what's the editor that it opens?

Copy link
Author

@tecoholic tecoholic Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the topic of a "writable editor", MacOS's open implements -a flag, which allows specifying the application to open the file. I am wondering if something similar can be added to this command, that can force a specific editor to be launched. tutor config edit -a vim maybe? What do think about this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be a good option, this gives control to the user. For instance, if someone wants to open the file in Sublime/VScode, they can override the default utils the command is checking.

open_cmd = ["start", '""', config_file]
else:
click.echo(
"Cannot find a way to open the editor automatically. Kindly open the file manually: "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rephrase that along the lines of: "Failed to detect an adequate text file editor". (mode concise, no need for "kindly")

tutor/commands/config.py Outdated Show resolved Hide resolved
@tecoholic
Copy link
Author

@regisb Hi, Thanks for the review comments. I hoped to have addressed them by now. Somehow haven't found the time. I will handle the comments this week.

@kdmccormick kdmccormick removed their request for review August 9, 2024 17:14
@kdmccormick
Copy link
Collaborator

I have nothing to add beyond Regis's review--just want to say that this is a great idea and thank you for the contribution 👍🏻

@tecoholic tecoholic force-pushed the tecoholic/add-config-edit-command branch from ecf0b53 to 853e51e Compare August 12, 2024 01:06
@tecoholic tecoholic requested a review from regisb August 12, 2024 01:11
elif which("start"): # Windows
open_cmd = ["start", '""', config_file]
else:
raise exceptions.TutorError(f"Failed to find utility to launch an editor.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the error message, we can specify which utilities are used by the command internally so that user can install them on the system if needed (not all the users of Tutor are dev, some are operators who might not want to delve into code to see what is happening under the hood).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some research, it seems to me that Windows will always have start, macOS will always have open, and Linux desktop environments will always have xdg-open. That leaves only non-desktop Linux users and perhaps BSD users. I think those users will know their systems better then we will, and they will be capable of figuring out how to open a yaml file as long as we tell them where it is 😄 @tecoholic , rather than letting them know what to install, could you just enhance this error message a bit just to let users know that they can edit configuration at {config_file}?

@tecoholic tecoholic force-pushed the tecoholic/add-config-edit-command branch from 853e51e to c095e45 Compare September 2, 2024 12:45
@kdmccormick
Copy link
Collaborator

Hi @tecoholic . I'm looking forward to this change! Looks like there are just a couple minor change requests left, and then we're good to merge this.

@tecoholic
Copy link
Author

@kdmccormick Hi, thanks for the ping. I was waiting on @regisb for more details about read only editor. Can we skip that scenario for now?

@regisb
Copy link
Contributor

regisb commented Sep 24, 2024

@kdmccormick I am less interested in this feature than you seem to be -- mostly because (IMHO) it doesn't bring anything more than a .bashrc alias. E.g: alias tutor-config-edit='vim "$(tutor config printroot)/env.yml"'. So I'd rather defer to you for review.

@kdmccormick kdmccormick self-requested a review September 24, 2024 13:09
Copy link
Collaborator

@kdmccormick kdmccormick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patience @tecoholic . If you can rebase and address these comments, I'll be happy to merge this.

tutor/commands/config.py Show resolved Hide resolved
tutor/commands/config.py Outdated Show resolved Hide resolved
elif which("start"): # Windows
open_cmd = ["start", '""', config_file]
else:
raise exceptions.TutorError(f"Failed to find utility to launch an editor.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some research, it seems to me that Windows will always have start, macOS will always have open, and Linux desktop environments will always have xdg-open. That leaves only non-desktop Linux users and perhaps BSD users. I think those users will know their systems better then we will, and they will be capable of figuring out how to open a yaml file as long as we tell them where it is 😄 @tecoholic , rather than letting them know what to install, could you just enhance this error message a bit just to let users know that they can edit configuration at {config_file}?

tutor/commands/config.py Show resolved Hide resolved
@tecoholic tecoholic force-pushed the tecoholic/add-config-edit-command branch from c095e45 to 2b00211 Compare November 10, 2024 05:12
@tecoholic
Copy link
Author

@kdmccormick Thank you for your comments and your patience with this PR. I have addressed your comments to my best. Kindly take another look when you can.

Copy link
Contributor

@DawoudSheraz DawoudSheraz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a changelog entry for this change.

Copy link
Collaborator

@kdmccormick kdmccormick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested locally, works great on Ubuntu! I also commented out the which("open") block in order to confirm that xdg-open also works.

In addition to Dawoud's request to add a changelog entry, I have just one more change request and one optional suggestion ⬇️

@@ -255,9 +259,41 @@ def patches_show(context: Context, name: str) -> None:
print(rendered)


@click.command(name="edit", help="Edit config.yml of the current environment")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: What do you think about adding a -s/--save flag here? If supplied, then instead of reminding the user to run tutor config save, it would just save config automatically.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was something I thought of adding later based on user feedback. Now that you have pointed it out, I have taken it as the feeback and added 2 features I think are QOL improvments.

  1. -s automatically updating the environment after a successful save. The caveat here is "successful save". More on them below.
  2. -e to specify an editor of choice. The annoying thing about desktop systems is, installing new software somehow messes up file associations. I discovered after installing KDE that my YAML files now open in Kate Editor. Now I do tutor config edit -e (vim|emacs|..) to open the file in a specific editor.

Save caveats:

  • Sometimes, when I do :wq in Neovim (Lazyvim config), it returns a non-zero error code. This prevents the "save" from happening.
  • When the file gets opened in a GUI editor like Kate, it opens it as an independent process, instead of subprocess. So the utils.execute() returns as soon as the editor is launched instead of return after the editor is closed. So, the environment is updated with config before editing.

tutor/commands/config.py Outdated Show resolved Hide resolved
@tecoholic tecoholic force-pushed the tecoholic/add-config-edit-command branch from 00565bb to d3cd4c0 Compare November 14, 2024 02:32
@tecoholic
Copy link
Author

tecoholic commented Nov 14, 2024

@kdmccormick Thank you for the feedback. I added a changelog entry, new options -s and -e (explanation here), made the messages a bit more verbose. Kindly take a look.

Comment on lines 1 to 11
<!--
Create a changelog entry for every new user-facing change. Please respect the following instructions:
- Indicate breaking changes by prepending an explosion 💥 character.
- Prefix your changes with either [Bugfix], [Improvement], [Feature], [Security], [Deprecation].
- You may optionally append "(by @<author>)" at the end of the line, where "<author>" is either one (just one)
of your GitHub username, real name or affiliated organization. These affiliations will be displayed in
the release notes for every release.
-->

<!-- - 💥[Feature] Foobarize the blorginator. This breaks plugins by renaming the `FOO_DO` filter to `BAR_DO`. (by @regisb) -->
<!-- - [Improvement] This is a non-breaking change. Life is good. (by @billgates) -->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove the default text from changelog.


<!-- - 💥[Feature] Foobarize the blorginator. This breaks plugins by renaming the `FOO_DO` filter to `BAR_DO`. (by @regisb) -->
<!-- - [Improvement] This is a non-breaking change. Life is good. (by @billgates) -->
[Feature] Adds `tutor config edit`. This opens the active tutor environment's config.yaml in an editor for manual editing. (by @tecoholic)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Please add - at the start so that it renders as a list item.

"-e",
"--editor",
default="",
help="Editor to open the config.yaml file.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the help text should mention if this is name or path to editor (or both).

@tecoholic
Copy link
Author

@DawoudSheraz I have addressed your comments about the docs and updated the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In review
Development

Successfully merging this pull request may close these issues.

4 participants