Add code to build the custom columns from whatever is configured

This commit is contained in:
David Dworken 2022-10-23 22:01:53 -07:00
parent 12163483dd
commit 07e4c781e4
2 changed files with 34 additions and 1 deletions

View File

@ -38,7 +38,7 @@ type HistoryEntry struct {
CustomColumns CustomColumns `json:"custom_columns"`
}
type CustomColumns []CustomColumns
type CustomColumns []CustomColumn
type CustomColumn struct {
Name string `json:"name"`

View File

@ -152,9 +152,42 @@ func BuildHistoryEntry(ctx *context.Context, args []string) (*data.HistoryEntry,
config := hctx.GetConf(ctx)
entry.DeviceId = config.DeviceId
// custom columns
cc, err := buildCustomColumns(ctx)
if err != nil {
return nil, err
}
entry.CustomColumns = cc
return &entry, nil
}
func buildCustomColumns(ctx *context.Context) (data.CustomColumns, error) {
ccs := data.CustomColumns{}
config := hctx.GetConf(ctx)
for _, cc := range config.CustomColumns {
cmd := exec.Command("bash", "-c", cc.ColumnCommand)
var stdout bytes.Buffer
cmd.Stdout = &stdout
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Start()
if err != nil {
return nil, fmt.Errorf("failed to execute custom command named %v (stdout=%#v, stderr=%#v)", cc.ColumnName, stdout.String(), stderr.String())
}
err = cmd.Wait()
if err != nil {
return nil, fmt.Errorf("failed to execute custom command named %v (stdout=%#v, stderr=%#v)", cc.ColumnName, stdout.String(), stderr.String())
}
ccv := data.CustomColumn{
Name: cc.ColumnName,
Val: strings.TrimSpace(stdout.String()),
}
ccs = append(ccs, ccv)
}
return ccs, nil
}
func isZshWeirdness(cmd string) bool {
// Zsh has this weird behavior where the currently running command is persisted to
// the history file with a weird prefix. This only matters to us when running