Skip to content

Commit

Permalink
plugin: Add pager to info command
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Dec 17, 2020
1 parent dfe10ff commit 1a596e0
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
14 changes: 13 additions & 1 deletion cmd/plugin/rpaasv2/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,22 @@ func runInfo(c *cli.Context) error {
return writeInfoOnJSONFormat(c.App.Writer, infoPayload)
}

err = instanceInfoTemplate.Execute(c.App.Writer, infoPayload)
writer := newPagerWriter(c.App.Writer)

err = instanceInfoTemplate.Execute(writer, infoPayload)
if err != nil {
return err
}

if pw, ok := writer.(*pagerWriter); ok {
if pw.pagerPipe != nil {
pw.pagerPipe.Close()
}
err = pw.Wait()
if err != nil {
return err
}
}

return nil
}
92 changes: 92 additions & 0 deletions cmd/plugin/rpaasv2/cmd/pager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2020 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cmd

import (
"bytes"
"io"
"os/exec"
"strings"
"syscall"

"golang.org/x/crypto/ssh/terminal"
)

type descriptable interface {
Fd() uintptr
}

func newPagerWriter(baseWriter io.Writer) io.Writer {
pager, found := syscall.Getenv("TSURU_PAGER")
if found && pager == "" {
return baseWriter
}

outputDesc, ok := baseWriter.(descriptable)
if !ok {
return baseWriter
}
terminalFd := int(outputDesc.Fd())
if !terminal.IsTerminal(terminalFd) {
return baseWriter
}

if pager == "" {
pager = "less -RFXE"
}
return &pagerWriter{baseWriter: baseWriter, pager: pager}
}

type pagerWriter struct {
baseWriter io.Writer
pagerPipe io.WriteCloser
cmd *exec.Cmd
pager string
buf bytes.Buffer
erroed bool
}

func (w *pagerWriter) Write(data []byte) (int, error) {
if w.pagerPipe != nil {
return w.pagerPipe.Write(data)
}
if w.erroed {
return w.baseWriter.Write(data)
}
w.buf.Write(data)
if w.cmd == nil {
var err error
pagerParts := strings.Split(w.pager, " ")
w.cmd = exec.Command(pagerParts[0], pagerParts[1:]...)
w.cmd.Stdout = w.baseWriter
w.pagerPipe, err = w.cmd.StdinPipe()
if err != nil {
w.erroed = true
}
err = w.cmd.Start()
if err != nil {
w.pagerPipe = nil
w.erroed = true
}
}
w.flush()
return len(data), nil
}

func (w *pagerWriter) Wait() error {
if w.cmd == nil {
return nil
}

return w.cmd.Wait()
}

func (w *pagerWriter) flush() {
if w.pagerPipe != nil {
w.pagerPipe.Write(w.buf.Bytes())
} else {
w.baseWriter.Write(w.buf.Bytes())
}
w.buf.Reset()
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require (
github.com/willf/bitset v1.1.11
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.16.0 // indirect
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
golang.org/x/net v0.0.0-20200822124328-c89045814202
k8s.io/api v0.19.2
k8s.io/apimachinery v0.19.2
Expand Down

0 comments on commit 1a596e0

Please sign in to comment.