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

Add a zsh completion file (automatically generated) #1712

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

getzze
Copy link

@getzze getzze commented Sep 10, 2023

The problem

There is no auto-completion suggestions for ZSH.

Solution

Follow up on PR #1034.

Add auto-completion to ZSH, by generating that completion file from share/actionsmap.yml

As yunohost needs to be called with sudo, completion may ask for password to autocomplete.

PR Status

How to test

Run the script with:

python3 doc/generate_zsh_completion.py

It will create a file yunohost_zsh_completion in the doc folder.
Move and rename the created file to /usr/local/share/zsh/site-functions/_yunohost (or /usr/share/zsh/vendor-completions/_yunohost, that is where yunohost installation will install it).
Restart zsh to test the completions.

@tituspijean
Copy link
Contributor

In a testing directory, I have copied the appropriate files in doc/generate_zsh_completion.py and share/actionsmap.yml

I'm ending up with the following error:

python3 doc/generate_zsh_completion.py
Traceback (most recent call last):
  File "/root/apps/doc/generate_zsh_completion.py", line 720, in <module>
    main()
  File "/root/apps/doc/generate_zsh_completion.py", line 73, in main
    output += make_category(key, value)
  File "/root/apps/doc/generate_zsh_completion.py", line 159, in make_category
    output += make_action(action, action_details).replace("COMMAND", category_name)
  File "/root/apps/doc/generate_zsh_completion.py", line 223, in make_action
    args = make_argument_list(action_map, spaces)
  File "/root/apps/doc/generate_zsh_completion.py", line 273, in make_argument_list
    if argument_details["nargs"] == "+":
KeyError: 'nargs'

@getzze
Copy link
Author

getzze commented Sep 10, 2023

Ah sorry, I made some modifications after running the linters and I left a few bugs. I tested it and it should be working now.

@tituspijean
Copy link
Contributor

Thank you so much, it is so great. :)

Upon testing everything:

  • yunohost user create completion is empty, but maybe it is expected not to show the optional flags?
  • yunohost user delete lists both usernames and their email, making it a bit redundant
  • yunohost user import completion outputs _arguments:463: command not found: _file
  • yunohost domain action-run <domain> does not suggest action nor -a ARGS
  • yunohost domain config get <domain> lacks further completions (-f, -e, ...)
  • yunohost domain config set <domain> lacks further completions (-v, -a, ...)
  • yunohost app manifest <app> --with-screenshot should be hidden if possible (API only)
  • yunohost app install completion lists already installed apps, I suggest nothing
  • yunohost app setting <app>, I do not know how complex it would be to list said settings (from /etc/yunohost/apps/<app>/settings.yml)
  • Same thing for yunohost app config get <app> and set
  • yunohost backup download should be hidden if possible (API only)

The following completions seem broken, though they all fit the same pattern: it looks like they do not have any args, only flags.

  • yunohost user update <user> -
  • yunohost user group list -
  • yunohost domain main-domain -
  • yunohost domain dyndns set-recovery-password <domain> -
  • yunohost app catalog -
  • yunohost app list -
  • yunohost app info <app> -
  • yunohost app map -
  • yunohost app change-url <app> -
  • yunohost backup create -
  • yunohost backup list -
  • yunohost backup info <backup> -
  • yunohost settings list -
  • yunohost settings get <setting> - and set
  • yunohost firewall list -
  • yunohost dyndns subscribe - and update
  • yunohost tools <args> -
  • yunohost hook callback <hook> -
  • yunohost log list -
  • yunohost log show <log> -

For reference, here is an example output:

yunohost log list -
--limit
--with-details
--with-suboperations
-l
-d
-s
-- Maximum number of operations to list (default to 50)
-- Show additional infos (e.g. operation success) but may significantly increase command tim
-- Include metadata about operations that are not the main operation but are sub-operations
--limit
--with-details
--with-suboperations
-l
-d
-s
-- Maximum number of operations to list (default to 50)
-- Show additional infos (e.g. operation success) but may significantly increase command tim
-- Include metadata about operations that are not the main operation but are sub-operations
--limit
--with-details
--with-suboperations
-l
-d
-s
-- Maximum number of operations to list (default to 50)
-- Show additional infos (e.g. operation success) but may significantly increase command tim
-- Include metadata about operations that are not the main operation but are sub-operations

Everything else seems to be working, which is already a lot. ❤️

@getzze
Copy link
Author

getzze commented Sep 10, 2023

Thanks for testing!
First, about autocompleting or not the flags (starting with -), it seems it is the default behavior with zsh:

  • if the next word is an argument (not starting with -), only provide arguments autocompletion.
  • if there is no argument left to autocomplete, provide the flags.
  • if you start writing -, it provides the flags.

I don't know how to change this default behavior, neither if we want to change it.

Regarding the other points:

  • yunohost user create completion is empty, but maybe it is expected not to show the optional flags?

Default behavior of zsh.

  • yunohost user delete lists both usernames and their email, making it a bit redundant

The documentation says you can use both, but it is indeed redundant for completion.

  • yunohost user import completion outputs _arguments:463: command not found: _file

I missplelled the command.

  • yunohost domain action-run <domain> does not suggest action nor -a ARGS

I don't know how to retrieve the list of actions. Is there a function to list them? Or if it's a fixed list, I can add the list of choices to actionsmap.yml.

  • yunohost domain config get <domain> lacks further completions (-f, -e, ...)
  • yunohost domain config set <domain> lacks further completions (-v, -a, ...)

I don't know how to retrieve the list of allowed keys. Same as above, is there a function to get this list?

  • yunohost app manifest <app> --with-screenshot should be hidden if possible (API only)

I can add a flag in actionsmap.yml to ignore it for completion.

  • yunohost app install completion lists already installed apps, I suggest nothing

Sometimes you want to install the same app twice, but I agree it can be confusing.

  • yunohost app setting <app>, I do not know how complex it would be to list said settings (from /etc/yunohost/apps//settings.yml). Same thing for yunohost app config get and set

I didn't find a way of completing the next word depending on a previous one, in this case we need to know which app was given in the first argument to know which file to read. But it would be great to be able to do that.

  • yunohost backup download should be hidden if possible (API only)

Same as above.

Also, for yunohost hook list <action>, I ended up writing the list of allowed hooks in actionsmap.yml because I didn't find a command to list the allowed hook names, but maybe this function exists.

Comment on lines +40 to +65
arguments:
-h:
full: --help
help: Show this help message and exit
--version:
help: Display YunoHost packages versions
action: callback
callback:
method: yunohost.utils.packages.ynh_packages_version
return: true
--output-as:
help: Output result in another format
choices:
- json
- plain
- none
--debug:
help: Log and print debug messages
action: store_true
--quiet:
help: Don't produce any output
action: store_true
--timeout:
help: Number of seconds before this command will timeout because it can't acquire the lock (meaning that another command is currently running), by default there is no timeout and the command will wait until it can get the lock
type: int

Copy link
Member

Choose a reason for hiding this comment

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

Hmmm I'm confused about this ... I understand that you need some declarative way of finding about these for the zsh autocompletion, but I doubt that moulinette really does handle these ? It's gonna be pretty confusing when debugging the action map later ... Also I see that you have a GLOBAL_ARGUMENTS which kinda hard-code those in generate_zsh_completion.py so i'm confused x_X

Copy link
Author

Choose a reason for hiding this comment

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

Hey, thanks for reviewing. This section was in the actionsmap before and it was removed. I didn't test with moulinette actually. I let the GLOBAL_ARGUMENTS in the code in case the section is missing in actionsmap, which can happen if the actionsmap file is still an old version. I don't know what's best... I think having the arguments in the _global section in actionsmap makes is easier to update them because they are all together. But if it is not working with moulinette we can remove the section.

@alexAubin
Copy link
Member

Regarding auto-completing the - flag thingy:

First, about autocompleting or not the flags (starting with -), it seems it is the default behavior with zsh:

if the next word is an argument (not starting with -), only provide arguments autocompletion.
if there is no argument left to autocomplete, provide the flags.
if you start writing -, it provides the flags.

I don't know how to change this default behavior, neither if we want to change it.

It seems that titus is more pointing the issue of rendering : each flag is displayed on a newline instead of all available flags on a single line, plus the output is duplicated/triplicated

@getzze
Copy link
Author

getzze commented Sep 27, 2023

It seems that titus is more pointing the issue of rendering : each flag is displayed on a newline instead of all available flags on a single line, plus the output is duplicated/triplicated

Ah ok, I also have this problem sometimes but it disappear when I start a new session of zsh. Can you check if you still see the glitch @tituspijean ?

@getzze
Copy link
Author

getzze commented Mar 16, 2024

Hey, I just realized this was never merged. It's working fine on my server, what else is needed to merge it?

@getzze
Copy link
Author

getzze commented Mar 16, 2024

@alexAubin

@Salamandar
Copy link
Contributor

I will test it on my setup and make comments afterwards.

Anyways we might want to prevent the code duplication with the bash completion autogeneration code.

@getzze
Copy link
Author

getzze commented May 17, 2024

Great! Let me know if I can help.
I went to check the bash autogeneration script and it looks very different I don't know if we can share a lot of code. Maybe more with YunoHost/issues#2309

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants