diff options
author | matthieu <matthieu.cneude@gmail.com> | 2020-05-30 16:26:09 +0200 |
---|---|---|
committer | matthieu <matthieu.cneude@gmail.com> | 2020-05-30 16:26:09 +0200 |
commit | cc549ee96e9b8eb9063b11ce0dfb91409ad5123f (patch) | |
tree | 43b74ee0be3a47154309469d956644b6bb08e017 | |
parent | 83a87f4654e665dc311c46180f21e2e12e85dfe6 (diff) |
Clean up and fix bugs
-rw-r--r-- | cmd/config.go | 21 | ||||
-rw-r--r-- | gokit/cmd.go | 4 | ||||
-rw-r--r-- | internal/platform/remote_host.go | 26 | ||||
-rw-r--r-- | internal/platform/remote_host_test.go | 2 | ||||
-rw-r--r-- | internal/remote_host_widget.go | 30 | ||||
-rw-r--r-- | internal/templates.go | 25 |
6 files changed, 64 insertions, 44 deletions
diff --git a/cmd/config.go b/cmd/config.go index 663cb63..d5fda16 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -217,26 +217,7 @@ func defaultConfig(path string, filename string) string { // TODO kind of ugly, but not sure if I can use a template here: it will be runtimeee if file != nil { - _, err := file.Write([]byte( - `--- -general: - refresh: 600 - keys: - quit: "C-c" - -projects: - - name: Default Configuration - You can modify at at $XDG_CONFIG_HOME/devdash/devdash.yml - services: - monitor: - address: "https://thevaluable.dev" - widgets: - - row: - - col: - size: "M" - elements: - - name: mon.box_availability - options: - border_color: green`)) + _, err := file.Write([]byte(internal.DefaultTemplate())) if err != nil { panic(err) } diff --git a/gokit/cmd.go b/gokit/cmd.go index 8899081..296246a 100644 --- a/gokit/cmd.go +++ b/gokit/cmd.go @@ -6,7 +6,9 @@ import ( "strings" ) -func Pipeline(command string) (out, errs []byte, pipeLineError error) { +// execCmd from a string. Support pipes. Single quote deleted. +// Example: "/bin/df -x devtmpfs -x tmpfs -x debugfs | sed -n '1!p'" +func ExecCmd(command string) (out, errs []byte, pipeLineError error) { cmds := []*exec.Cmd{} piped := strings.Split(command, "|") for _, v := range piped { diff --git a/internal/platform/remote_host.go b/internal/platform/remote_host.go index b0f8290..624b0f9 100644 --- a/internal/platform/remote_host.go +++ b/internal/platform/remote_host.go @@ -1,5 +1,9 @@ package platform +// RemoteHost execute a command on a remote host or on the local host. +// The output is parsed and displayed in the dashboard. +// A personalized command (or shell script) can be added to the config to render whatever output you want. + import ( "bufio" "bytes" @@ -23,7 +27,6 @@ type RemoteHost struct { localhost bool } -// TODO accept more method of connection than only via ssh-agent func NewRemoteHost(username, addr string) (*RemoteHost, error) { if username == "localhost" && addr == "localhost" { return &RemoteHost{ @@ -67,7 +70,7 @@ func (s *RemoteHost) run(command string) (string, error) { } func runLocalhost(command string) (string, error) { - out, errs, err := gokit.Pipeline(command) + out, errs, err := gokit.ExecCmd(command) if err != nil { return "", err } @@ -349,15 +352,27 @@ func (s *RemoteHost) Table(command string, headers []string) (cells [][]string, scanner := bufio.NewScanner(strings.NewReader(lines)) data := "" + + lineNumber := 0 for scanner.Scan() { line := scanner.Text() parts := strings.Fields(line) + if len(headers) == 0 { + headers = parts + lineNumber++ + continue + } + data += strings.Join(parts, ",") + "," + lineNumber++ } data = strings.Trim(data, ",") - return formatToTable(headers, data), nil + cells = [][]string{headers} + cells = append(cells, formatToTable(len(headers), data)...) + + return } func (s *RemoteHost) Disk(headers []string, unit string) ([][]string, error) { @@ -409,7 +424,7 @@ func (s *RemoteHost) Disk(headers []string, unit string) ([][]string, error) { data = strings.Trim(data, ",") c := [][]string{headers} - c = append(c, formatToTable(headers, data)...) + c = append(c, formatToTable(len(headers), data)...) return c, nil } @@ -467,8 +482,7 @@ func formatToBar(data string) (val []uint64) { // Info needs to be splitted with comma // Depending on number of columns (headers) // TODO improve this comment :D -func formatToTable(headers []string, data string) (cells [][]string) { - col := len(headers) +func formatToTable(col int, data string) (cells [][]string) { c := strings.Split(data, ",") lenCells := len(c) for i := 0; i < lenCells; i += col { diff --git a/internal/platform/remote_host_test.go b/internal/platform/remote_host_test.go index 9018c85..975ce15 100644 --- a/internal/platform/remote_host_test.go +++ b/internal/platform/remote_host_test.go @@ -34,7 +34,7 @@ func Test_formatToTable(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - actual := formatToTable(tc.headers, tc.data) + actual := formatToTable(len(tc.headers), tc.data) if !reflect.DeepEqual(actual, tc.expected) { t.Errorf("Expected %v, actual %v", tc.expected, actual) diff --git a/internal/remote_host_widget.go b/internal/remote_host_widget.go index df782f2..bcfdfed 100644 --- a/internal/remote_host_widget.go +++ b/internal/remote_host_widget.go @@ -96,7 +96,7 @@ func (ms *remoteHostWidget) boxLoad(widget Widget) (f func() error, err error) { } func (ms *remoteHostWidget) boxProcesses(widget Widget) (f func() error, err error) { - title := " Processes " + title := " Running processes " if _, ok := widget.Options[optionTitle]; ok { title = widget.Options[optionTitle] } @@ -132,7 +132,7 @@ func (ms *remoteHostWidget) boxUptime(widget Widget) (f func() error, err error) } func (ms *remoteHostWidget) boxCPURate(widget Widget) (f func() error, err error) { - title := " CPU Rate " + title := " CPU usage " if _, ok := widget.Options[optionTitle]; ok { title = widget.Options[optionTitle] } @@ -150,7 +150,7 @@ func (ms *remoteHostWidget) boxCPURate(widget Widget) (f func() error, err error } func (ms *remoteHostWidget) boxMemRate(widget Widget) (f func() error, err error) { - title := " Memory Rate " + title := " Memory usage " if _, ok := widget.Options[optionTitle]; ok { title = widget.Options[optionTitle] } @@ -168,7 +168,7 @@ func (ms *remoteHostWidget) boxMemRate(widget Widget) (f func() error, err error } func (ms *remoteHostWidget) boxSwapRate(widget Widget) (f func() error, err error) { - title := " Swap Rate " + title := " Swap usage " if _, ok := widget.Options[optionTitle]; ok { title = widget.Options[optionTitle] } @@ -186,7 +186,7 @@ func (ms *remoteHostWidget) boxSwapRate(widget Widget) (f func() error, err erro } func (ms *remoteHostWidget) barRates(widget Widget) (f func() error, err error) { - title := " Swap Rate " + title := " Resources usage (%) " if _, ok := widget.Options[optionTitle]; ok { title = widget.Options[optionTitle] } @@ -250,7 +250,7 @@ func (ms *remoteHostWidget) boxNetIO(widget Widget) (f func() error, err error) unit = widget.Options[optionUnit] } - title := fmt.Sprintf(" Net I/O (%s) ", unit) + title := fmt.Sprintf(" Net I/O (%s) ", strings.ToUpper(unit)) if _, ok := widget.Options[optionTitle]; ok { title = widget.Options[optionTitle] } @@ -273,7 +273,7 @@ func (ms *remoteHostWidget) boxDiskIO(widget Widget) (f func() error, err error) unit = widget.Options[optionUnit] } - title := fmt.Sprintf(" Disk I/O (%s) ", unit) + title := fmt.Sprintf(" Disk I/O (%s) ", strings.ToUpper(unit)) if _, ok := widget.Options[optionTitle]; ok { title = widget.Options[optionTitle] } @@ -310,7 +310,7 @@ func (ms *remoteHostWidget) barMemory(widget Widget) (f func() error, err error) unit = widget.Options[optionUnit] } - title := fmt.Sprintf(" Memory (%s) ", unit) + title := fmt.Sprintf(" Memory (%s) ", strings.ToUpper(unit)) if _, ok := widget.Options[optionTitle]; ok { title = widget.Options[optionTitle] } @@ -357,28 +357,26 @@ func (ms *remoteHostWidget) table(widget Widget) (f func() error, err error) { title = widget.Options[optionTitle] } - // cmd := "/bin/df -x devtmpfs -x tmpfs -x debugfs | tail -n +2" - cmd := "/bin/df -x devtmpfs -x tmpfs -x debugfs | sed -n '1!p'" + headers := []string{"Filesystem", "Size", "Used", "Available", "Use%", "Mount"} + + cmd := "/bin/df -x devtmpfs -x tmpfs -x debugfs | tail -n +2" + // cmd := "/bin/df -x devtmpfs -x tmpfs -x debugfs | sed -n '1!p'" if _, ok := widget.Options[optionCommand]; ok { cmd = widget.Options[optionCommand] + headers = []string{} } - headers := []string{"Filesystem", "Size", "Used", "Available", "Use%", "Mount"} if _, ok := widget.Options[optionHeaders]; ok { if len(widget.Options[optionHeaders]) > 0 { headers = strings.Split(strings.TrimSpace(widget.Options[optionHeaders]), ",") } } - d, err := ms.service.Table(cmd, headers) + data, err := ms.service.Table(cmd, headers) if err != nil { return nil, err } - data := [][]string{} - data = append(data, headers) - data = append(data, d...) - f = func() error { return ms.tui.AddTable(data, title, widget.Options) } diff --git a/internal/templates.go b/internal/templates.go new file mode 100644 index 0000000..09180d6 --- /dev/null +++ b/internal/templates.go @@ -0,0 +1,25 @@ +package internal + +func DefaultTemplate() string { + return `--- +general: + refresh: 600 + keys: + quit: "C-c" + hot_reload: "C-r" + +projects: + - name: Default dashboard located at $HOME/.config/devdash/default.yml + services: + monitor: + address: "https://thevaluable.dev" + widgets: + - row: + - col: + size: "M" + elements: + - name: mon.box_availability + options: + title: " thevaluable.dev status " + color: yellow` +} |