Skip to content

Commit

Permalink
Remove cpprof
Browse files Browse the repository at this point in the history
  • Loading branch information
howardjohn committed Sep 9, 2021
1 parent 314e65f commit cc93ae0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
7 changes: 0 additions & 7 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"regexp"
"runtime/pprof"
"strings"

"github.com/howardjohn/kubectl-grep/pkg"
Expand All @@ -25,12 +24,6 @@ var rootCmd = &cobra.Command{
Use: "kubectl-grep",
Short: "A plugin to grep Kubernetes resources.",
RunE: func(cmd *cobra.Command, args []string) error {
f, perr := os.Create("cpu.pprof")
if perr != nil {
return perr
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
dm := pkg.Full
if summary {
dm = pkg.Summary
Expand Down
20 changes: 17 additions & 3 deletions pkg/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@ import (

// Implementation borrowed from k8s.io/apimachinery to avoid large imports

const separator = "---"
const (
separator = "---"
listSeparator = "- "
)

type Reader interface {
Read() ([]byte, error)
}

type YAMLReader struct {
reader Reader
isList bool
}

func NewYAMLReader(r *bufio.Reader) *YAMLReader {
func NewYAMLReader(r *bufio.Reader, isList bool) *YAMLReader {
return &YAMLReader{
reader: &LineReader{reader: r},
isList: isList,
}
}

Expand All @@ -35,6 +40,15 @@ func (r *YAMLReader) Read() ([]byte, error) {
return nil, err
}

if r.isList {
// TODO: optimize if we know its a list
//sep := len([]byte(listSeparator))
//if i := bytes.Index(line, []byte(listSeparator)); i == 0 {
// fmt.Println(string(line))
// i+=sep
//}
}

sep := len([]byte(separator))
if i := bytes.Index(line, []byte(separator)); i == 0 {
// We have a potential document terminator
Expand Down Expand Up @@ -82,4 +96,4 @@ func (r *LineReader) Read() ([]byte, error) {
}
buffer.WriteByte('\n')
return buffer.Bytes(), err
}
}
8 changes: 7 additions & 1 deletion pkg/grep.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@ const (
CleanStatus
)

var listFastHeader = `apiVersion: v1
items:
-`

func GrepResources(sel Selector, in io.Reader, mode DisplayMode) (string, error) {
reader := NewYAMLReader(bufio.NewReader(in))
r := bufio.NewReader(in)
b, _ := r.Peek(len(listFastHeader))
reader := NewYAMLReader(r, string(b) == listFastHeader)
matches := []string{}
for {
text, err := reader.Read()
Expand Down

0 comments on commit cc93ae0

Please sign in to comment.