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

Transport completions #2524

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

Conversation

yedayak
Copy link

@yedayak yedayak commented Feb 17, 2025

Another thing I would like to add is completion of containers-storage:, but I'm not sure how to get the list of available images.

Signed-off-by: Yedaya Katsman <yedaya.ka@gmail.com>
@yedayak yedayak force-pushed the transport-completions branch 2 times, most recently from 1539aa9 to 8f37068 Compare February 22, 2025 16:56
@yedayak yedayak changed the title WIP: Transport completions Transport completions Feb 22, 2025
Copy link
Member

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

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

Overall this looks like a nice improvement, just some comments on the implementation. (Note I am not an official maintainer here but I have done most of the cobra completion work in podman and skopeo)

func autocompleteTransports(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
directive := cobra.ShellCompDirectiveNoSpace
// We still don't have a transport
if !strings.Contains(toComplete, ":") {
Copy link
Member

Choose a reason for hiding this comment

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

Overall this would likely be much better written with strings.Cut()

That way you can easily check if a colon was set and the you have the clear prefix and can match that without the colon below.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, this works much better. Updated to do this

Comment on lines 20 to 26
if strings.HasPrefix(toComplete, "oci-archive:") || strings.HasPrefix(toComplete, "docker-archive:") || strings.HasPrefix(toComplete, "sif:") || strings.HasPrefix(toComplete, "oci:") {
return nil, directive
}
if toComplete == "docker:" {
return []cobra.Completion{"docker://"}, directive | cobra.ShellCompDirectiveNoFileComp
}
if strings.HasPrefix(toComplete, "dir:") {
Copy link
Member

Choose a reason for hiding this comment

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

here you then should match the transpart names as defined by the code and not hard code the strings
docker.Transport.Name(), directory.Transport.Name(),etc...

Comment on lines 27 to 43
// just ShellCompDirectiveFilterDirs is more correct, but doesn't work here in bash, see https://github.com/spf13/cobra/issues/2242. Instead we get the directories ourselves.
curDir := filepath.Dir(strings.TrimPrefix(toComplete, "dir:"))
entries, err := os.ReadDir(curDir)
if err != nil {
cobra.CompErrorln("Failed ReadDir at " + curDir)
// Fallback to whatever the shell gives us
return nil, cobra.ShellCompDirectiveFilterDirs | cobra.ShellCompDirectiveNoFileComp
}
suggestions := make([]cobra.Completion, 0, len(entries))
for _, e := range entries {
if e.IsDir() {
suggestions = append(suggestions, "dir:"+path.Join(curDir, e.Name())+"/")
}
}
return suggestions, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace
Copy link
Member

Choose a reason for hiding this comment

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

Is that actually worth it? Returning all paths as done by the shell seems more than fine, compared of having to read all files here.

Copy link
Author

Choose a reason for hiding this comment

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

I agree its a bit overkill, this just makes the completions more accurate, showing only directories. I'm fine with not doing it.

@yedayak yedayak force-pushed the transport-completions branch 2 times, most recently from 1f10c79 to 0dcebd9 Compare February 27, 2025 14:24
Copy link

Tests failed. @containers/packit-build please check.

@lsm5
Copy link
Member

lsm5 commented Feb 27, 2025

@Luap99 do we test completions in CI anywhere yet?

@Luap99
Copy link
Member

Luap99 commented Feb 27, 2025

@Luap99 do we test completions in CI anywhere yet?

In podman we have number of rather complex tests (test/system/600-completion.bats). But that is not something I would recommend for skopeo, most of the changes here are trivial to test manually (and it is not like they will get changed often) and there is basically no risk if the completion is not working fully.

Copy link
Contributor

@mtrmac mtrmac left a comment

Choose a reason for hiding this comment

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

Thanks!

// autocompleteSupportedTransports list all supported transports with the colon suffix.
func autocompleteSupportedTransports(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
func autocompleteTransports(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
directive := cobra.ShellCompDirectiveNoSpace
Copy link
Contributor

Choose a reason for hiding this comment

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

I don’t see a benefit to using this variable; AFAICS it only makes the return values harder to understand.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, I over complicated it :) removed

if err != nil {
cobra.CompErrorln("Failed ReadDir at " + curDir)
// Fallback to whatever the shell gives us
return nil, cobra.ShellCompDirectiveFilterDirs | cobra.ShellCompDirectiveNoFileComp
Copy link
Contributor

Choose a reason for hiding this comment

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

The way I read the documentation, NoFileComp should make FilterDirs entirely irrelevant. (In the implementation, that seems not to matter.)

Copy link
Author

Choose a reason for hiding this comment

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

Yeah ShellCompDirectiveFilterDirs by itself should just complete directories (though in bash it currently doesn't). fixed

)

// autocompleteSupportedTransports list all supported transports with the colon suffix.
func autocompleteSupportedTransports(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
func autocompleteTransports(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is now autocompleteImageNames, now that it is trying to complete the full arguments.

Copy link
Author

Choose a reason for hiding this comment

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

done

func autocompleteSupportedTransports(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
func autocompleteTransports(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
directive := cobra.ShellCompDirectiveNoSpace
// We still don't have a transport
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment belongs inside the next if (or on the if !haveTransport line), it is incorrect for other paths through this function.

Copy link
Author

Choose a reason for hiding this comment

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

Removed the comment since it's now obvious from the variable name

@mtrmac
Copy link
Contributor

mtrmac commented Feb 27, 2025

@Luap99 do we test completions in CI anywhere yet?

In podman we have number of rather complex tests (test/system/600-completion.bats).

Here, I think it would be easier, and we would not give up on ~any test coverage compared to what Podman does, to use Go unit tests for the individual parts (and, for that purpose, to split the dir: logic into a separate function).

and there is basically no risk if the completion is not working fully.

Yes. I don’t think missing tests are a blocker.

Transports that reference a file or directory are completed.

Signed-off-by: Yedaya Katsman <yedaya.ka@gmail.com>
@yedayak yedayak force-pushed the transport-completions branch from 0dcebd9 to d577111 Compare March 2, 2025 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants