mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
WTF-986 User-definable DigitalOcean columns (#1001)
* WTF-986 Wrap the DigitalOcean droplet in our own droplet This gives us something to build off while still providing the underlying functionality of the original droplet instance. Signed-off-by: Chris Cummer <chriscummer@me.com> * WTF-986 Dynamically display droplet attributes based on defined column names Signed-off-by: Chris Cummer <chriscummer@me.com> * WTF-986 Read DigitalOcean column configuration from settings Signed-off-by: Chris Cummer <chriscummer@me.com> * WTF-986 Extract the reflection bits into a Reflective package Signed-off-by: Chris Cummer <chriscummer@me.com>
This commit is contained in:
parent
b2e0c520e8
commit
f84142553c
@ -2,33 +2,60 @@ package digitalocean
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/wtfutil/wtf/utils"
|
||||
)
|
||||
|
||||
const maxColWidth = 12
|
||||
|
||||
func (widget *Widget) content() (string, string, bool) {
|
||||
columnSet := widget.settings.columns
|
||||
|
||||
title := widget.CommonSettings().Title
|
||||
if widget.err != nil {
|
||||
return title, widget.err.Error(), true
|
||||
}
|
||||
|
||||
str := fmt.Sprintf(
|
||||
" [%s]Droplets\n\n",
|
||||
widget.settings.common.Colors.Subheading,
|
||||
)
|
||||
if len(columnSet) < 1 {
|
||||
return title, " no columns defined", false
|
||||
}
|
||||
|
||||
str := fmt.Sprintf(" [::b][%s]", widget.settings.common.Colors.Subheading)
|
||||
|
||||
for _, colName := range columnSet {
|
||||
truncName := utils.Truncate(colName, maxColWidth, false)
|
||||
|
||||
str += fmt.Sprintf("%-12s", truncName)
|
||||
}
|
||||
|
||||
str += "\n"
|
||||
|
||||
for idx, droplet := range widget.droplets {
|
||||
dropletName := droplet.Name
|
||||
// This defines the formatting for the row, one tab-seperated string for each defined column
|
||||
fmtStr := " [%s]"
|
||||
|
||||
row := fmt.Sprintf(
|
||||
"[%s] %-8s %-24s %s",
|
||||
for range columnSet {
|
||||
fmtStr += "%-12s"
|
||||
}
|
||||
|
||||
vals := []interface{}{
|
||||
widget.RowColor(idx),
|
||||
droplet.Status,
|
||||
dropletName,
|
||||
utils.Truncate(strings.Join(droplet.Tags, ","), 24, true),
|
||||
)
|
||||
}
|
||||
|
||||
// Dynamically access the droplet to get the requested columns values
|
||||
for _, colName := range columnSet {
|
||||
val, err := droplet.StringValueForProperty(colName)
|
||||
if err != nil {
|
||||
val = "???"
|
||||
}
|
||||
|
||||
truncVal := utils.Truncate(val, maxColWidth, false)
|
||||
|
||||
vals = append(vals, truncVal)
|
||||
}
|
||||
|
||||
// And format, print, and color the row
|
||||
row := fmt.Sprintf(fmtStr, vals...)
|
||||
str += utils.HighlightableHelper(widget.View, row, idx, 33)
|
||||
}
|
||||
|
||||
|
80
modules/digitalocean/droplet.go
Normal file
80
modules/digitalocean/droplet.go
Normal file
@ -0,0 +1,80 @@
|
||||
package digitalocean
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/digitalocean/godo"
|
||||
"github.com/wtfutil/wtf/utils"
|
||||
)
|
||||
|
||||
// Droplet represents WTF's view of a DigitalOcean droplet
|
||||
type Droplet struct {
|
||||
godo.Droplet
|
||||
|
||||
Image Image
|
||||
Region Region
|
||||
}
|
||||
|
||||
// Image represents WTF's view of a DigitalOcean droplet image
|
||||
type Image struct {
|
||||
godo.Image
|
||||
utils.Reflective
|
||||
}
|
||||
|
||||
// Region represents WTF's view of a DigitalOcean region
|
||||
type Region struct {
|
||||
godo.Region
|
||||
utils.Reflective
|
||||
}
|
||||
|
||||
// NewDroplet creates and returns an instance of Droplet
|
||||
func NewDroplet(doDroplet godo.Droplet) *Droplet {
|
||||
droplet := &Droplet{
|
||||
doDroplet,
|
||||
|
||||
Image{
|
||||
*doDroplet.Image,
|
||||
utils.Reflective{},
|
||||
},
|
||||
|
||||
Region{
|
||||
*doDroplet.Region,
|
||||
utils.Reflective{},
|
||||
},
|
||||
}
|
||||
|
||||
return droplet
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
// StringValueForProperty returns a string value for the given column
|
||||
func (drop *Droplet) StringValueForProperty(propName string) (string, error) {
|
||||
var strVal string
|
||||
var err error
|
||||
|
||||
// Figure out if we should forward this property to a sub-object
|
||||
// Lets us support "Region.Name" column definitions
|
||||
split := strings.Split(propName, ".")
|
||||
|
||||
switch split[0] {
|
||||
case "Image":
|
||||
strVal, err = drop.Image.StringValueForProperty(split[1])
|
||||
case "Region":
|
||||
strVal, err = drop.Region.StringValueForProperty(split[1])
|
||||
default:
|
||||
v := reflect.ValueOf(drop)
|
||||
refVal := reflect.Indirect(v).FieldByName(propName)
|
||||
|
||||
if !refVal.IsValid() {
|
||||
err = fmt.Errorf("invalid property name: %s", propName)
|
||||
} else {
|
||||
strVal = fmt.Sprintf("%v", refVal)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return strVal, err
|
||||
}
|
@ -5,13 +5,12 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/digitalocean/godo"
|
||||
"github.com/wtfutil/wtf/utils"
|
||||
"github.com/wtfutil/wtf/view"
|
||||
)
|
||||
|
||||
type dropletPropertiesTable struct {
|
||||
droplet *godo.Droplet
|
||||
droplet *Droplet
|
||||
propertyMap map[string]string
|
||||
|
||||
colWidth0 int
|
||||
@ -20,7 +19,7 @@ type dropletPropertiesTable struct {
|
||||
}
|
||||
|
||||
// newDropletPropertiesTable creates and returns an instance of DropletPropertiesTable
|
||||
func newDropletPropertiesTable(droplet *godo.Droplet) *dropletPropertiesTable {
|
||||
func newDropletPropertiesTable(droplet *Droplet) *dropletPropertiesTable {
|
||||
propTable := &dropletPropertiesTable{
|
||||
droplet: droplet,
|
||||
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/olebedev/config"
|
||||
"github.com/wtfutil/wtf/cfg"
|
||||
"github.com/wtfutil/wtf/utils"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
@ -13,11 +14,20 @@ const (
|
||||
defaultTitle = "DigitalOcean"
|
||||
)
|
||||
|
||||
// defaultColumns defines the default set of columns to display in the widget
|
||||
// This can be over-ridden in the cofig by explicitly defining a set of columns
|
||||
var defaultColumns = []interface{}{
|
||||
"Name",
|
||||
"Status",
|
||||
"Region.Slug",
|
||||
}
|
||||
|
||||
// Settings defines the configuration properties for this module
|
||||
type Settings struct {
|
||||
common *cfg.Common
|
||||
|
||||
apiKey string `help:"Your DigitalOcean API key."`
|
||||
columns []string `help:"A list of the droplet properties to display."`
|
||||
dateFormat string `help:"The format to display dates and times in."`
|
||||
}
|
||||
|
||||
@ -28,6 +38,7 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co
|
||||
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
|
||||
|
||||
apiKey: ymlConfig.UString("apiKey", ymlConfig.UString("apikey", os.Getenv("WTF_DIGITALOCEAN_API_KEY"))),
|
||||
columns: utils.ToStrs(ymlConfig.UList("columns", defaultColumns)),
|
||||
dateFormat: ymlConfig.UString("dateFormat", wtf.DateFormat),
|
||||
}
|
||||
|
||||
|
@ -35,9 +35,10 @@ type Widget struct {
|
||||
|
||||
app *tview.Application
|
||||
client *godo.Client
|
||||
droplets []godo.Droplet
|
||||
droplets []*Droplet
|
||||
pages *tview.Pages
|
||||
settings *Settings
|
||||
|
||||
err error
|
||||
}
|
||||
|
||||
@ -127,7 +128,7 @@ func (widget *Widget) createClient() {
|
||||
|
||||
// currentDroplet returns the currently-selected droplet, if there is one
|
||||
// Returns nil if no droplet is selected
|
||||
func (widget *Widget) currentDroplet() *godo.Droplet {
|
||||
func (widget *Widget) currentDroplet() *Droplet {
|
||||
if len(widget.droplets) == 0 {
|
||||
return nil
|
||||
}
|
||||
@ -136,21 +137,24 @@ func (widget *Widget) currentDroplet() *godo.Droplet {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &widget.droplets[widget.Selected]
|
||||
return widget.droplets[widget.Selected]
|
||||
}
|
||||
|
||||
// dropletsFetch uses the DigitalOcean API to fetch information about all the available droplets
|
||||
func (widget *Widget) dropletsFetch() ([]godo.Droplet, error) {
|
||||
dropletList := []godo.Droplet{}
|
||||
func (widget *Widget) dropletsFetch() ([]*Droplet, error) {
|
||||
dropletList := []*Droplet{}
|
||||
opts := &godo.ListOptions{}
|
||||
|
||||
for {
|
||||
droplets, resp, err := widget.client.Droplets.List(context.Background(), opts)
|
||||
doDroplets, resp, err := widget.client.Droplets.List(context.Background(), opts)
|
||||
if err != nil {
|
||||
return dropletList, err
|
||||
}
|
||||
|
||||
dropletList = append(dropletList, droplets...)
|
||||
for _, doDroplet := range doDroplets {
|
||||
droplet := NewDroplet(doDroplet)
|
||||
dropletList = append(dropletList, droplet)
|
||||
}
|
||||
|
||||
if resp.Links == nil || resp.Links.IsLastPage() {
|
||||
break
|
||||
|
25
utils/reflective.go
Normal file
25
utils/reflective.go
Normal file
@ -0,0 +1,25 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Reflective is a convenience wrapper for objects that makes it possible to
|
||||
// extract property values from the object by property name
|
||||
type Reflective struct{}
|
||||
|
||||
// StringValueForProperty returns a string value for the given property
|
||||
// If the property doesn't exist, it returns an error
|
||||
func (ref *Reflective) StringValueForProperty(propName string) (string, error) {
|
||||
v := reflect.ValueOf(ref)
|
||||
refVal := reflect.Indirect(v).FieldByName(propName)
|
||||
|
||||
if !refVal.IsValid() {
|
||||
return "", fmt.Errorf("invalid property name: %s", propName)
|
||||
}
|
||||
|
||||
strVal := fmt.Sprintf("%v", refVal)
|
||||
|
||||
return strVal, nil
|
||||
}
|
@ -77,12 +77,13 @@ func Truncate(src string, maxLen int, withEllipse bool) string {
|
||||
return src
|
||||
}
|
||||
|
||||
// Formats number as string with 1000 delimiters and, if necessary, rounds it to 2 decimals
|
||||
// PrettyNumber formats number as string with 1000 delimiters and, if necessary, rounds it to 2 decimals
|
||||
func PrettyNumber(number float64) string {
|
||||
p := message.NewPrinter(language.English)
|
||||
|
||||
if number == math.Trunc(number) {
|
||||
return p.Sprintf("%.0f", number)
|
||||
} else {
|
||||
return p.Sprintf("%.2f", number)
|
||||
}
|
||||
|
||||
return p.Sprintf("%.2f", number)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user