Skip to content
This repository has been archived by the owner on Jul 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #31 from dty1er/dty1er/header
Browse files Browse the repository at this point in the history
implement getting multiple resources
  • Loading branch information
dtyler committed Oct 29, 2020
2 parents 236798e + 366f9ae commit 0c51fbb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
3 changes: 0 additions & 3 deletions README.md
Expand Up @@ -132,10 +132,7 @@ Not in the list: Won't be supported because it's not READ operation

## Other features which currently unsupported but will be done in the future

- [x] make it works with -w option
- [ ] Configuring custom colors
- [ ] specifying multiple resources at once (e.g. `kubectl get pod,replicaset`)
- This will actually work, but if you don't specify "--no-headers" it might look a bit strange.

## Supported kubectl version

Expand Down
19 changes: 16 additions & 3 deletions printer/table.go
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"io"
"strings"

"github.com/dty1er/kubecolor/color"
)
Expand Down Expand Up @@ -33,7 +34,7 @@ func (tp *TablePrinter) Print(r io.Reader, w io.Writer) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
if tp.isHeader() {
if tp.isHeader(line) {
fmt.Fprintf(w, "%s\n", color.Apply(line, getHeaderColorByBackground(tp.DarkBackground)))
tp.isFirstLine = false
continue
Expand All @@ -43,8 +44,20 @@ func (tp *TablePrinter) Print(r io.Reader, w io.Writer) {
}
}

func (tp *TablePrinter) isHeader() bool {
return tp.WithHeader && tp.isFirstLine
func (tp *TablePrinter) isHeader(line string) bool {
// If every character is upper case, probably it's a header line.
// e.g.
// kubecolor get pod,rs
// NAME READY STATUS RESTARTS AGE
// pod/nginx-8spn9 1/1 Running 1 19d
// pod/nginx-dplns 1/1 Running 1 19d
// pod/nginx-lpv5x 1/1 Running 1 19d

// NAME DESIRED CURRENT READY AGE <- this
// replicaset.apps/nginx 3 3 3 19d
// replicaset.apps/nginx-6799fc88d8 3 3 3 19d
isEveryCharacterUpper := strings.ToUpper(line) == line
return (tp.WithHeader && tp.isFirstLine) || isEveryCharacterUpper
}

// printTableFormat prints a line to w in kubectl "table" Format.
Expand Down
26 changes: 26 additions & 0 deletions printer/table_test.go
Expand Up @@ -36,6 +36,32 @@ func Test_TablePrinter_Print(t *testing.T) {
nginx-qdf9b 1/1 Running 0 6d6h
`),
},
{
name: "multiple headers",
colorDeciderFn: nil,
withHeader: true,
darkBackground: true,
input: testutil.NewHereDoc(`
NAME READY STATUS RESTARTS AGE
pod/nginx-8spn9 1/1 Running 1 19d
pod/nginx-dplns 1/1 Running 1 19d
pod/nginx-lpv5x 1/1 Running 1 19d
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx 3 3 3 19d
replicaset.apps/nginx-6799fc88d8 3 3 3 19d
`),
expected: testutil.NewHereDoc(`
NAME READY STATUS RESTARTS AGE
pod/nginx-8spn9 1/1 Running 1 19d
pod/nginx-dplns 1/1 Running 1 19d
pod/nginx-lpv5x 1/1 Running 1 19d

NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx 3 3 3 19d
replicaset.apps/nginx-6799fc88d8 3 3 3 19d
`),
},
{
name: "withheader=false, 1st line is not colored in header color but colored as a content of table",
colorDeciderFn: nil,
Expand Down

0 comments on commit 0c51fbb

Please sign in to comment.