Skip to content
Merged
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
2 changes: 1 addition & 1 deletion cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func Command() *cobra.Command { //nolint:funlen
"output",
"o",
"",
"output format (json,name,path,references,table,yaml)")
"output format (json,name,path,references,selector,table,yaml)")

var state struct {
allMatchers matcher.Matcher
Expand Down
3 changes: 3 additions & 0 deletions printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func ByName(name string) (func(io.Writer, []resources.Resource) error, error) {
case "references":
return References, nil

case "selector":
return Selector, nil

case "table":
return Table, nil

Expand Down
47 changes: 47 additions & 0 deletions printer/selector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright Josh Komoroske. All rights reserved.
// Use of this source code is governed by the MIT license,
// a copy of which can be found in the LICENSE.txt file.
// SPDX-License-Identifier: MIT

package printer

import (
"fmt"
"io"
"maps"
"slices"
"strings"

"github.com/rodaine/table"

"github.com/joshdk/krf/resources"
)

// Selector prints each given resources.Resource with the resource name and a
// matching label selector.
func Selector(w io.Writer, items []resources.Resource) error {
tbl := table.New("Name", "Selector")
tbl.WithPrintHeaders(false)
tbl.WithWriter(w)

for _, item := range items {
var (
labels = item.GetLabels()
selectors []string
)

// Sort all labels by key name.
for _, key := range slices.Sorted(maps.Keys(labels)) {
selectors = append(selectors, fmt.Sprintf("%s=%s", key, labels[key]))
}

tbl.AddRow(
fmt.Sprintf("%s/%s", item.GetKind(), item.GetName()),
strings.Join(selectors, ","),
)
}

tbl.Print()

return nil
}