-
Notifications
You must be signed in to change notification settings - Fork 815
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
base: main
Are you sure you want to change the base?
Transport completions #2524
Conversation
5e6d14c
to
627aced
Compare
627aced
to
580ed9f
Compare
Signed-off-by: Yedaya Katsman <yedaya.ka@gmail.com>
1539aa9
to
8f37068
Compare
There was a problem hiding this 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)
cmd/skopeo/completions.go
Outdated
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, ":") { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
cmd/skopeo/completions.go
Outdated
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:") { |
There was a problem hiding this comment.
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...
cmd/skopeo/completions.go
Outdated
// 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
1f10c79
to
0dcebd9
Compare
Tests failed. @containers/packit-build please check. |
@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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
cmd/skopeo/completions.go
Outdated
// 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
cmd/skopeo/completions.go
Outdated
if err != nil { | ||
cobra.CompErrorln("Failed ReadDir at " + curDir) | ||
// Fallback to whatever the shell gives us | ||
return nil, cobra.ShellCompDirectiveFilterDirs | cobra.ShellCompDirectiveNoFileComp |
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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
cmd/skopeo/completions.go
Outdated
) | ||
|
||
// 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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
cmd/skopeo/completions.go
Outdated
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
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
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>
0dcebd9
to
d577111
Compare
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.