Skip to content

Commit

Permalink
add TP_BLOCK_COMMAND
Browse files Browse the repository at this point in the history
  • Loading branch information
minefuto committed Feb 4, 2021
1 parent bf506bc commit 06ff496
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 6 deletions.
62 changes: 56 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ var (
horizontalFlag bool
versionFlag bool
stdinBytes []byte
blockCommands = getBlockList()
)

var env = func() string {
return os.Getenv("TP_BLOCK_COMMAND")
}

var getTerminalHeight = func() int {
_, height, _ := terminal.GetSize(int(os.Stderr.Fd()))
return height
Expand Down Expand Up @@ -91,12 +96,12 @@ func (t *tui) setAction() {
})

t.cliPane.SetChangedFunc(func(text string) {
_trimText := strings.TrimSpace(text)
if t.cliPane.trimText == _trimText {
t.cliPane.trimText = _trimText
_text := strings.TrimSpace(text)
if t.cliPane.trimText == _text {
t.cliPane.trimText = _text
return
}
t.cliPane.trimText = _trimText
t.cliPane.trimText = _text
t.stdoutPane.reset()
t.updateStdoutView(text)
})
Expand Down Expand Up @@ -124,12 +129,18 @@ func (t *tui) setAction() {
t.stdoutPane.cancel()
t.Stop()

_text := adjustPipe(t.cliPane.prompt) + t.cliPane.GetText()
if commandFlag {
fmt.Println(adjustPipe(t.cliPane.prompt) + t.cliPane.GetText())
fmt.Println(_text)
return nil
}

if isBlock(_text) {
fmt.Fprintf(os.Stderr, "This command is blocked")
return nil
}

cmd := exec.Command(shell, "-c", adjustPipe(t.cliPane.prompt)+t.cliPane.GetText())
cmd := exec.Command(shell, "-c", _text)
cmd.Stdin = bytes.NewReader(stdinBytes)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand All @@ -155,6 +166,10 @@ func (t *tui) setAction() {
t.stdinPane.reset()
t.updateStdinView()
return nil
case '>':
return nil
case '<':
return nil
}
}
return event
Expand Down Expand Up @@ -359,6 +374,11 @@ func (si *stdinViewPane) execCommand(ctx context.Context, text string, inputByte
w := transform.NewWriter(tview.ANSIWriter(si), tt)
mw := io.MultiWriter(w, _data)

if isBlock(text) {
fmt.Fprint(mw, "This command is blocked")
return
}

cmd := exec.CommandContext(ctx, shell, "-c", text)

cmd.Stdin = bytes.NewReader(inputBytes)
Expand Down Expand Up @@ -386,6 +406,11 @@ func (so *stdoutViewPane) execCommand(ctx context.Context, text string, inputByt
tt := newTextLineTransformer()
w := transform.NewWriter(tview.ANSIWriter(so), tt)

if isBlock(text) {
fmt.Fprint(w, "This command is blocked")
return
}

cmd := exec.CommandContext(ctx, shell, "-c", text)

cmd.Stdin = bytes.NewReader(inputBytes)
Expand All @@ -395,6 +420,31 @@ func (so *stdoutViewPane) execCommand(ctx context.Context, text string, inputByt
cmd.Run()
}

func getBlockList() []string {
_env := env()
if _env == "" {
return nil
}
return strings.Split(_env, ":")
}

func isBlock(text string) bool {
if blockCommands == nil {
return false
}

for _, cmd := range blockCommands {
_text := strings.TrimLeft(text, " ")
if _text == cmd {
return true
}
if strings.HasPrefix(_text, cmd+" ") {
return true
}
}
return false
}

type textLineTransformer struct {
transform.NopResetter
line int
Expand Down
49 changes: 49 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,59 @@ import (
"context"
"fmt"
"io"
"reflect"
"strings"
"testing"

"golang.org/x/text/transform"
)

func TestGetBlockList(t *testing.T) {
var input string
env = func() string {
return input
}

cases := []struct {
input string
result []string
}{
{input: "", result: nil},
{input: "a", result: []string{"a"}},
{input: "a:b", result: []string{"a", "b"}},
}

for _, tc := range cases {
input = tc.input
result := getBlockList()
if !(reflect.DeepEqual(result, tc.result)) {
r := `result: "%s"`
e := `expected: "%s"`
t.Errorf("\n%s\n%s", r, e)
}
}
}

func TestIsBlock(t *testing.T) {
cases := []struct {
input string
result bool
}{
{input: "rm", result: true},
{input: "rm aaa", result: true},
{input: "rma aaa", result: false},
}

for _, tc := range cases {
if isBlock(tc.input) != tc.result {
r := `result: "%s"`
e := `expected: "%s"`
t.Errorf("\n%s\n%s", r, e)
}
}

}

func TestSpinner(t *testing.T) {
cases := []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
s := spinner()
Expand Down Expand Up @@ -101,6 +148,7 @@ func TestSetData(t *testing.T) {

func TestExecCommandStdin(t *testing.T) {
shell = "sh"
blockCommands = nil
getTerminalHeight = func() int {
return 6
}
Expand Down Expand Up @@ -134,6 +182,7 @@ func TestExecCommandStdin(t *testing.T) {

func TestExecCommandStdout(t *testing.T) {
shell = "sh"
blockCommands = nil
getTerminalHeight = func() int {
return 6
}
Expand Down

0 comments on commit 06ff496

Please sign in to comment.