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) +}