-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
layout: | ||
screens: | ||
- stack: vertical | ||
children: | ||
- name: Hostname | ||
module: command | ||
config: | ||
bash: hostname | ||
- name: Date | ||
module: command | ||
config: | ||
interval: 5s | ||
bash: date |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package captured | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os/exec" | ||
) | ||
|
||
type Cmd struct { | ||
cmd *exec.Cmd | ||
stdout *capturedOutput | ||
stderr *capturedOutput | ||
} | ||
|
||
func New(ctx context.Context, command string, args ...string) *Cmd { | ||
cmd := exec.CommandContext(ctx, command, args...) | ||
|
||
c := &Cmd{ | ||
cmd: cmd, | ||
stdout: newCapturedOutput(), | ||
stderr: newCapturedOutput(), | ||
} | ||
|
||
cmd.Stdout = c.stdout | ||
cmd.Stderr = c.stderr | ||
|
||
return c | ||
} | ||
|
||
func (c *Cmd) Run() error { | ||
err := c.cmd.Start() | ||
|
||
if err != nil { | ||
return fmt.Errorf("cmd.Start: %w", err) | ||
} | ||
|
||
return c.cmd.Wait() | ||
} | ||
|
||
func (r *Cmd) Stop() error { | ||
if r == nil || r.cmd == nil { | ||
return fmt.Errorf("cmd is nil") | ||
} | ||
|
||
err := r.cmd.Process.Kill() | ||
|
||
if err != nil { | ||
return fmt.Errorf("process.Kill: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *Cmd) Stdout() string { | ||
output := r.stdout.String() | ||
|
||
return output | ||
} | ||
|
||
func (r *Cmd) Stderr() string { | ||
output := r.stderr.String() | ||
|
||
return output | ||
} | ||
|
||
func (r *Cmd) ResetOutput() { | ||
r.stdout.reset() | ||
r.stderr.reset() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package captured | ||
|
||
import "sync" | ||
|
||
type capturedOutput struct { | ||
sync.RWMutex | ||
data []byte | ||
} | ||
|
||
func newCapturedOutput() *capturedOutput { | ||
return &capturedOutput{ | ||
data: make([]byte, 0, 1024), | ||
} | ||
} | ||
|
||
func (c *capturedOutput) Write(data []byte) (n int, err error) { | ||
c.Lock() | ||
defer c.Unlock() | ||
|
||
c.data = append(c.data, data...) | ||
|
||
return len(data), nil | ||
} | ||
|
||
func (c *capturedOutput) reset() { | ||
c.Lock() | ||
defer c.Unlock() | ||
|
||
c.data = make([]byte, 0, 1024) | ||
} | ||
|
||
func (c *capturedOutput) String() string { | ||
c.RLock() | ||
defer c.RUnlock() | ||
|
||
return string(c.data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
|
||
"github.com/evertras/yakdash/pkg/id" | ||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
type model struct { | ||
id id.ID | ||
config config | ||
|
||
lastOutput string | ||
} | ||
|
||
func New(cfg interface{}) (model, error) { | ||
var config config | ||
err := mapstructure.Decode(cfg, &config) | ||
|
||
if err != nil { | ||
return model{}, fmt.Errorf("failed to decode config: %w", err) | ||
} | ||
|
||
config, err = config.setDefaultsAndParse() | ||
|
||
if err != nil { | ||
return model{}, fmt.Errorf("invalid config: %w", err) | ||
} | ||
|
||
m := model{ | ||
id: id.New(), | ||
config: config, | ||
} | ||
|
||
return m, nil | ||
} | ||
|
||
type tickMsg struct { | ||
id id.ID | ||
} | ||
|
||
func (m model) doTick() tea.Cmd { | ||
// Tick on a regular interval so that all commands | ||
// from different panes can try to update at the | ||
// same time, if they use the same interval. | ||
// The command will timeout before the next tick. | ||
return tea.Batch( | ||
tea.Tick(m.config.interval, func(time.Time) tea.Msg { | ||
return tickMsg{m.id} | ||
}), | ||
m.runCommand(), | ||
) | ||
} | ||
|
||
func (m model) Init() tea.Cmd { | ||
return m.doTick() | ||
} | ||
|
||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
var ( | ||
cmds []tea.Cmd | ||
) | ||
|
||
switch msg := msg.(type) { | ||
case tickMsg: | ||
if msg.id != m.id { | ||
break | ||
} | ||
|
||
cmds = append(cmds, m.doTick()) | ||
|
||
case commandResult: | ||
if msg.id != m.id { | ||
break | ||
} | ||
|
||
if msg.err != nil { | ||
m.lastOutput = fmt.Sprintf("Error: %s", msg.err) | ||
break | ||
} | ||
|
||
m.lastOutput = msg.stdout | ||
|
||
} | ||
|
||
return m, tea.Batch(cmds...) | ||
} | ||
|
||
func (m model) View() string { | ||
return m.lastOutput | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type config struct { | ||
Bash string `mapstructure:"bash,omitempty"` | ||
IntervalRaw string `mapstructure:"interval,omitempty"` | ||
|
||
interval time.Duration | ||
} | ||
|
||
func (c config) setDefaultsAndParse() (config, error) { | ||
if c.Bash == "" { | ||
return c, fmt.Errorf("must supply a command to run") | ||
} | ||
|
||
if c.IntervalRaw == "" { | ||
c.IntervalRaw = "10s" | ||
} | ||
|
||
var err error | ||
c.interval, err = time.ParseDuration(c.IntervalRaw) | ||
|
||
if err != nil { | ||
return c, fmt.Errorf("failed to parse time %q: %w", c.IntervalRaw, err) | ||
} | ||
|
||
return c, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package command | ||
|
||
import ( | ||
"context" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
|
||
"github.com/evertras/yakdash/pkg/id" | ||
"github.com/evertras/yakdash/pkg/modules/command/captured" | ||
) | ||
|
||
type commandResult struct { | ||
id id.ID | ||
stdout string | ||
stderr string | ||
err error | ||
} | ||
|
||
func (m model) runCommand() tea.Cmd { | ||
return func() tea.Msg { | ||
ctx, cancel := context.WithTimeout(context.Background(), m.config.interval) | ||
defer cancel() | ||
cmd := captured.New(ctx, "bash", "-c", m.config.Bash) | ||
|
||
err := cmd.Run() | ||
|
||
return commandResult{ | ||
id: m.id, | ||
stdout: cmd.Stdout(), | ||
stderr: cmd.Stderr(), | ||
err: err, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters