Skip to content

Commit

Permalink
Merge pull request #11153 from cdoern/scp
Browse files Browse the repository at this point in the history
Added autocompletion for images and system connections for podman image SCP
  • Loading branch information
openshift-ci[bot] authored Aug 11, 2021
2 parents 8b14cd8 + a4bdc67 commit 99e7ea5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
48 changes: 48 additions & 0 deletions cmd/podman/common/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,18 @@ func prefixSlice(pre string, slice []string) []string {
return slice
}

func suffixCompSlice(suf string, slice []string) []string {
for i := range slice {
split := strings.SplitN(slice[i], "\t", 2)
if len(split) > 1 {
slice[i] = split[0] + suf + "\t" + split[1]
} else {
slice[i] = slice[i] + suf
}
}
return slice
}

func completeKeyValues(toComplete string, k keyValueCompletion) ([]string, cobra.ShellCompDirective) {
suggestions := make([]string, 0, len(k))
directive := cobra.ShellCompDirectiveNoFileComp
Expand Down Expand Up @@ -664,6 +676,42 @@ func AutocompleteSystemConnections(cmd *cobra.Command, args []string, toComplete
return suggestions, cobra.ShellCompDirectiveNoFileComp
}

// AutocompleteScp returns a list of connections, images, or both, depending on the amount of arguments
func AutocompleteScp(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if !validCurrentCmdLine(cmd, args, toComplete) {
return nil, cobra.ShellCompDirectiveNoFileComp
}
switch len(args) {
case 0:
split := strings.SplitN(toComplete, "::", 2)
if len(split) > 1 {
imageSuggestions, _ := getImages(cmd, split[1])
return prefixSlice(split[0]+"::", imageSuggestions), cobra.ShellCompDirectiveNoFileComp
}
connectionSuggestions, _ := AutocompleteSystemConnections(cmd, args, toComplete)
imageSuggestions, _ := getImages(cmd, toComplete)
totalSuggestions := append(suffixCompSlice("::", connectionSuggestions), imageSuggestions...)
directive := cobra.ShellCompDirectiveNoFileComp
// if we have connections do not add a space after the completion
if len(connectionSuggestions) > 0 {
directive = cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace
}
return totalSuggestions, directive
case 1:
split := strings.SplitN(args[0], "::", 2)
if len(split) > 1 {
if len(split[1]) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
imageSuggestions, _ := getImages(cmd, toComplete)
return imageSuggestions, cobra.ShellCompDirectiveNoFileComp
}
connectionSuggestions, _ := AutocompleteSystemConnections(cmd, args, toComplete)
return suffixCompSlice("::", connectionSuggestions), cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}

/* -------------- Flags ----------------- */

// AutocompleteDetachKeys - Autocomplete detach-keys options.
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/images/scp.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var (
Short: "securely copy images",
RunE: scp,
Args: cobra.RangeArgs(1, 2),
ValidArgsFunction: common.AutocompleteImages,
ValidArgsFunction: common.AutocompleteScp,
Example: `podman image scp myimage:latest otherhost::`,
}
)
Expand Down

0 comments on commit 99e7ea5

Please sign in to comment.