From c2e30a373af041ecebcc798564499f2347005df6 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 1 Nov 2024 04:09:41 -0500 Subject: [PATCH] DumpDroplet() Signed-off-by: Jeff Carr --- human.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/human.go b/human.go index 0ace136..f9c5af4 100644 --- a/human.go +++ b/human.go @@ -8,9 +8,13 @@ package virtbuf // are in text columns and rows that can be easily read in a terminal import ( + "errors" "fmt" + "net/http" "strings" "time" + + "go.wit.com/log" ) func oldGetDurationStamp(t time.Time) string { @@ -105,7 +109,6 @@ func FormatDuration(d time.Duration) string { return result } - func (d *Droplet) SprintHeader() string { header := fmt.Sprintf("%-3.3s %-9.9s %-20.20s", d.Current.State, d.Current.Hypervisor, d.Hostname) @@ -175,3 +178,28 @@ func (d *Droplet) SprintDumpHeader() string { } return header } + +func (d *Droplet) DumpDroplet(w http.ResponseWriter, r *http.Request) (string, error) { + if d == nil { + reason := "DumpDroplet() got d == nil" + log.Warn(reason) + fmt.Fprintln(w, reason) + return "", errors.New(reason) + } + t := d.FormatTEXT() + log.Info(t) + fmt.Fprintln(w, t) + return t, nil +} + +func (c *NewCluster) DumpDroplet(w http.ResponseWriter, r *http.Request) (string, error) { + hostname := r.URL.Query().Get("hostname") + d := c.FindDropletByName(hostname) + if d == nil { + result := "can not find droplet hostname=" + hostname + log.Info(result) + fmt.Fprintln(w, result) + return result, errors.New(result) + } + return d.DumpDroplet(w, r) +}