2019-01-31 23:40:05 +00:00
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
2019-03-14 03:01:44 +00:00
|
|
|
ui "github.com/gizak/termui/v3"
|
2019-03-10 04:41:23 +00:00
|
|
|
"github.com/sqshq/sampler/config"
|
2019-03-21 02:23:08 +00:00
|
|
|
"os"
|
2019-01-31 23:40:05 +00:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Item struct {
|
2019-04-06 02:11:52 +00:00
|
|
|
Label string
|
|
|
|
SampleScript string
|
|
|
|
InitScript *string
|
|
|
|
TransformScript *string
|
|
|
|
Color *ui.Color
|
2019-03-10 04:41:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewItems(cfgs []config.Item) []Item {
|
|
|
|
|
|
|
|
items := make([]Item, 0)
|
|
|
|
|
|
|
|
for _, i := range cfgs {
|
2019-04-06 02:11:52 +00:00
|
|
|
item := Item{
|
|
|
|
Label: *i.Label,
|
|
|
|
SampleScript: *i.SampleScript,
|
|
|
|
InitScript: i.InitScript,
|
|
|
|
TransformScript: i.TransformScript,
|
|
|
|
Color: i.Color}
|
2019-03-10 04:41:23 +00:00
|
|
|
items = append(items, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return items
|
2019-01-31 23:40:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 02:23:08 +00:00
|
|
|
func (i *Item) nextValue(variables []string) (value string, err error) {
|
2019-01-31 23:40:05 +00:00
|
|
|
|
2019-04-06 02:11:52 +00:00
|
|
|
cmd := exec.Command("sh", "-c", i.SampleScript)
|
2019-03-21 02:23:08 +00:00
|
|
|
cmd.Env = os.Environ()
|
|
|
|
|
|
|
|
for _, variable := range variables {
|
|
|
|
cmd.Env = append(cmd.Env, variable)
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := cmd.Output()
|
2019-01-31 23:40:05 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSpace(string(output)), nil
|
|
|
|
}
|