Skip to content

search tags in reverse order #26338

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions cmd/podman/images/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"slices"
"strings"

"github.com/containers/common/pkg/auth"
Expand Down Expand Up @@ -108,6 +109,7 @@ func searchFlags(cmd *cobra.Command) {

flags.BoolVar(&searchOptions.TLSVerifyCLI, "tls-verify", true, "Require HTTPS and verify certificates when contacting registries")
flags.BoolVar(&searchOptions.ListTags, "list-tags", false, "List the tags of the input registry")
flags.BoolVar(&searchOptions.ReverseOrder, "reverse-order", false, "List tags either in descending or ascending order")

if !registry.IsRemote() {
certDirFlagName := "cert-dir"
Expand Down Expand Up @@ -160,6 +162,9 @@ func imageSearch(cmd *cobra.Command, args []string) error {
if len(searchReport) == 0 {
return nil
}
if searchOptions.ReverseOrder {
reverseOrder(searchReport)
}

isJSON := report.IsJSON(searchOptions.Format)
for i, element := range searchReport {
Expand Down Expand Up @@ -241,3 +246,11 @@ ReportLoop:
}
return entries
}

// Reverse tags order
func reverseOrder(report []entities.ImageSearchReport) []entities.ImageSearchReport {
slices.SortFunc(report, func(a, b entities.ImageSearchReport) int {
return -1 * strings.Compare(strings.ToLower(a.Tag), strings.ToLower(b.Tag))
})
return report
}
4 changes: 4 additions & 0 deletions docs/source/markdown/podman-search.1.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ The result contains the Image name and its tag, one line for every tag associate

Do not truncate the output (default *false*).

#### **--reverse-order**

List tags in reverse order. Default is false.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of reverse order as a description, can you describe it in terms of time like you did in your commit?


@@option tls-verify

## EXAMPLES
Expand Down
2 changes: 2 additions & 0 deletions pkg/domain/entities/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ type ImageSearchOptions struct {
SkipTLSVerify types.OptionalBool
// ListTags search the available tags of the repository
ListTags bool
// List tags either in descending or ascending order
ReverseOrder bool
}

// ImageSearchReport is the response from searching images.
Expand Down
22 changes: 22 additions & 0 deletions test/e2e/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"encoding/json"
"fmt"
"os"
"slices"
"strconv"
"strings"
"text/template"

"github.com/containers/podman/v5/pkg/domain/entities"
Expand Down Expand Up @@ -101,6 +103,26 @@ registries = []`
}
})

It("podman search list tags in reverse order", func() {
searchAscending := podmanTest.Podman([]string{"search", "--list-tags", ALPINE})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider podmanTest.PodmanExitCleanly for this call and the next. You can then eliminate 109-12.

searchDecending := podmanTest.Podman([]string{"search", "--list-tags", "--reverse-order", ALPINE})
searchAscending.WaitWithDefaultTimeout()
searchDecending.WaitWithDefaultTimeout()
Expect(searchAscending).Should(ExitCleanly())
Expect(searchDecending).Should(ExitCleanly())

// Removed headers
ascendingReport := searchAscending.OutputToStringArray()[1:]
descendingReport := searchDecending.OutputToStringArray()[1:]

// Reverse ascending tags list
slices.SortFunc(ascendingReport, func(a, b string) int {
return -1 * strings.Compare(strings.ToLower(a), strings.ToLower(b))
})

Expect(ascendingReport).Should(Equal(descendingReport))
})

It("podman search format json list tags", func() {
search := podmanTest.Podman([]string{"search", "--list-tags", "--format", "json", ALPINE})
search.WaitWithDefaultTimeout()
Expand Down