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

Added autocompletion for images and system connections for podman image SCP #11153

Merged
merged 1 commit into from
Aug 11, 2021

Conversation

cdoern
Copy link
Contributor

@cdoern cdoern commented Aug 6, 2021

image scp should autocomplete images and system connections since the args can
be either. Made a new function, common.AutocompleteScp

This function looks at the current arg length and either gives the user a connection name, an image, or nothing depending on what has already been typed.

Signed-off-by: cdoern [email protected]

@cdoern cdoern force-pushed the scp branch 2 times, most recently from 2d86740 to 849fea9 Compare August 6, 2021 18:37
@Luap99
Copy link
Member

Luap99 commented Aug 9, 2021

Can you add this diff:

diff --git a/cmd/podman/common/completion.go b/cmd/podman/common/completion.go
index a86d17dda..944b17641 100644
--- a/cmd/podman/common/completion.go
+++ b/cmd/podman/common/completion.go
@@ -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
@@ -669,21 +681,34 @@ func AutocompleteScp(cmd *cobra.Command, args []string, toComplete string) ([]st
 	if !validCurrentCmdLine(cmd, args, toComplete) {
 		return nil, cobra.ShellCompDirectiveNoFileComp
 	}
-	connectionSuggestions, _ := AutocompleteSystemConnections(cmd, args, toComplete)
-	imageSuggestions, _ := getImages(cmd, toComplete)
 
 	switch len(args) {
 	case 0:
-		totalSuggestions := append(connectionSuggestions, imageSuggestions...)
-		return totalSuggestions, cobra.ShellCompDirectiveNoFileComp
+		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:
-		if strings.Contains(args[0], "::") {
-			if len(strings.Split(args[0], "::")[1]) > 0 {
+		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
 		}
-		return connectionSuggestions, cobra.ShellCompDirectiveNoFileComp
+		connectionSuggestions, _ := AutocompleteSystemConnections(cmd, args, toComplete)
+		return suffixCompSlice("::", connectionSuggestions), cobra.ShellCompDirectiveNoFileComp
 	}
 	return nil, cobra.ShellCompDirectiveNoFileComp
 }

The advantages are the it can complete images after connection:: and that it does not query unnecessary for images or connection when they are not used. It also adds :: after connection names and does not insert a extra space.

@cdoern
Copy link
Contributor Author

cdoern commented Aug 9, 2021

@Luap99 thanks I was wondering how to complete mid-arg with connection:: just applied this change

[NO TESTS NEEDED] image scp should autocomplete images and system connections since the args can
be either. Made a new function, common.AutocompleteScp

Signed-off-by: cdoern <[email protected]>
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.

LGTM

@cdoern
Copy link
Contributor Author

cdoern commented Aug 10, 2021

@rhatdan @TomSweeneyRedHat PTAL, works well now due to Paul's diff on the switch in AutoCompleteScp

@TomSweeneyRedHat
Copy link
Member

LGTM
nice work all

@TomSweeneyRedHat
Copy link
Member

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Aug 11, 2021
@Luap99
Copy link
Member

Luap99 commented Aug 11, 2021

/approve

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Aug 11, 2021

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cdoern, Luap99

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 11, 2021
@openshift-ci openshift-ci bot merged commit 99e7ea5 into containers:main Aug 11, 2021
@github-actions github-actions bot added the locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. label Sep 22, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants