From 19f2d028a65c7499a17b2c55ef735adeaaac6669 Mon Sep 17 00:00:00 2001 From: sqshq Date: Sun, 7 Apr 2019 21:00:25 -0400 Subject: [PATCH] interactive shell post processing added --- component/sparkline/sparkline.go | 1 + component/util/parse_test.go | 2 +- data/item.go | 16 +++++++--------- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/component/sparkline/sparkline.go b/component/sparkline/sparkline.go index ed26966..b0451e1 100644 --- a/component/sparkline/sparkline.go +++ b/component/sparkline/sparkline.go @@ -121,4 +121,5 @@ func (s *SparkLine) Draw(buffer *ui.Buffer) { } s.Block.Draw(buffer) + component.RenderAlert(s.alert, s.Rectangle, buffer) } diff --git a/component/util/parse_test.go b/component/util/parse_test.go index b8b6028..c5f87da 100644 --- a/component/util/parse_test.go +++ b/component/util/parse_test.go @@ -13,7 +13,7 @@ func TestParseFloat(t *testing.T) { wantErr bool }{ {"should parse a regular number", args{"123"}, 123, false}, - {"should parse a regular number with spaces", args{" 123 "}, 123, false}, + {"should parse a regular number with spaces, tabs and line breaks", args{" \t 123 \t \n "}, 123, false}, {"should parse a last line in the given string", args{"123\n456"}, 456, false}, } for _, tt := range tests { diff --git a/data/item.go b/data/item.go index d588200..c2cd5ca 100644 --- a/data/item.go +++ b/data/item.go @@ -60,13 +60,13 @@ func (i *Item) nextValue(variables []string) (string, error) { if i.InitScript != nil { return i.executeInteractiveShellCmd(variables) } else { - return i.executeCmd(variables) + return i.executeCmd(variables, i.SampleScript) } } -func (i *Item) executeCmd(variables []string) (string, error) { +func (i *Item) executeCmd(variables []string, script string) (string, error) { - cmd := exec.Command("sh", "-c", i.SampleScript) + cmd := exec.Command("sh", "-c", script) enrichEnvVariables(cmd, variables) output, err := cmd.Output() @@ -75,7 +75,7 @@ func (i *Item) executeCmd(variables []string) (string, error) { return "", err } - return strings.TrimSpace(string(output)), nil + return string(output), nil } func (i *Item) initInteractiveShell(variables []string) error { @@ -167,13 +167,11 @@ func (i *Item) executeInteractiveShellCmd(variables []string) (string, error) { func (i *Item) transformInteractiveShellCmd(sample string) (string, error) { - result := sample - - if i.TransformScript != nil { - result = result // TODO + if i.TransformScript != nil && len(sample) > 0 { + return i.executeCmd([]string{"sample=" + sample}, *i.TransformScript) } - return result, nil + return sample, nil } func enrichEnvVariables(cmd *exec.Cmd, variables []string) {