Skip to content

Commit

Permalink
Complete paths for some transports
Browse files Browse the repository at this point in the history
Transports that reference a file or directory are completed.

Signed-off-by: Yedaya Katsman <yedaya.ka@gmail.com>
  • Loading branch information
yedayak committed Feb 22, 2025
1 parent 48a08d2 commit 8f37068
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
43 changes: 40 additions & 3 deletions cmd/skopeo/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,47 @@ import (
"github.com/containers/image/v5/tarball"
"github.com/containers/image/v5/transports"
"github.com/spf13/cobra"
"os"
"path"
"path/filepath"
"strings"
)

// 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
// 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 []cobra.Completion{"docker://"}, directive | cobra.ShellCompDirectiveNoFileComp
}
if strings.HasPrefix(toComplete, "dir:") {
// 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
}
return nil, directive | cobra.ShellCompDirectiveNoFileComp
}

// supportedTransportSuggestions list all supported transports with the colon suffix.
func supportedTransportSuggestions() []string {
tps := transports.ListNames()
suggestions := make([]cobra.Completion, 0, len(tps))
for _, tp := range tps {
Expand All @@ -18,5 +55,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 8f37068

Please sign in to comment.