Skip to content

Commit

Permalink
WIP: Complete paths for some transports
Browse files Browse the repository at this point in the history
left to do:
- Dir transports
- containers-storage - need some dependency?
- Count args - stop completing transports
  • Loading branch information
yedayak committed Feb 17, 2025
1 parent b7828b9 commit 5e6d14c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
28 changes: 25 additions & 3 deletions cmd/skopeo/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,32 @@ import (
"github.com/containers/image/v5/tarball"
"github.com/containers/image/v5/transports"
"github.com/spf13/cobra"
"strings"
)

// autocompleteSupportedTransports list all supported transports with the colon suffix.
func autocompleteSupportedTransports(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
func autocompleteTransports(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
directive := cobra.ShellCompDirectiveNoSpace
// We still don't have a transport
if !strings.Contains(toComplete, ":") {
transports := supportedTransportSuggestions()
return transports, directive | cobra.ShellCompDirectiveNoFileComp
}
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 []string{"docker://"}, directive | cobra.ShellCompDirectiveNoFileComp
}
if strings.HasPrefix(toComplete, "dir:") {
// This doesn't seem to actually work, at least in bash. It completes regular files as well.
return nil, cobra.ShellCompDirectiveFilterDirs
}
return nil, directive | cobra.ShellCompDirectiveNoFileComp

}

// supportedTransportSuggestions list all supported transports with the colon suffix.
func supportedTransportSuggestions() []string {
tps := transports.ListNames()
suggestions := make([]string, 0, len(tps))
for _, tp := range tps {
Expand All @@ -18,5 +40,5 @@ func autocompleteSupportedTransports(cmd *cobra.Command, args []string, toComple
suggestions = append(suggestions, tp+":")
}
}
return suggestions, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace
return suggestions
}
2 changes: 1 addition & 1 deletion cmd/skopeo/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ See skopeo(1) section "IMAGE NAMES" for the expected format
`, strings.Join(transports.ListNames(), ", ")),
RunE: commandAction(opts.run),
Example: `skopeo copy docker://quay.io/skopeo/stable:latest docker://registry.example.com/skopeo:latest`,
ValidArgsFunction: autocompleteSupportedTransports,
ValidArgsFunction: autocompleteTransports,
}
adjustUsage(cmd)
flags := cmd.Flags()
Expand Down
2 changes: 1 addition & 1 deletion cmd/skopeo/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ See skopeo(1) section "IMAGE NAMES" for the expected format
`, strings.Join(transports.ListNames(), ", ")),
RunE: commandAction(opts.run),
Example: `skopeo delete docker://registry.example.com/example/pause:latest`,
ValidArgsFunction: autocompleteSupportedTransports,
ValidArgsFunction: autocompleteTransports,
}
adjustUsage(cmd)
flags := cmd.Flags()
Expand Down
2 changes: 1 addition & 1 deletion cmd/skopeo/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ See skopeo(1) section "IMAGE NAMES" for the expected format
Example: `skopeo inspect docker://registry.fedoraproject.org/fedora
skopeo inspect --config docker://docker.io/alpine
skopeo inspect --format "Name: {{.Name}} Digest: {{.Digest}}" docker://registry.access.redhat.com/ubi8`,
ValidArgsFunction: autocompleteSupportedTransports,
ValidArgsFunction: autocompleteTransports,
}
adjustUsage(cmd)
flags := cmd.Flags()
Expand Down

0 comments on commit 5e6d14c

Please sign in to comment.