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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/wtfutil/wtf/utils"
|
"github.com/wtfutil/wtf/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const maxColWidth = 12
|
||||||
|
|
||||||
func (widget *Widget) content() (string, string, bool) {
|
func (widget *Widget) content() (string, string, bool) {
|
||||||
|
columnSet := widget.settings.columns
|
||||||
|
|
||||||
title := widget.CommonSettings().Title
|
title := widget.CommonSettings().Title
|
||||||
if widget.err != nil {
|
if widget.err != nil {
|
||||||
return title, widget.err.Error(), true
|
return title, widget.err.Error(), true
|
||||||
}
|
}
|
||||||
|
|
||||||
str := fmt.Sprintf(
|
if len(columnSet) < 1 {
|
||||||
" [%s]Droplets\n\n",
|
return title, " no columns defined", false
|
||||||
widget.settings.common.Colors.Subheading,
|
}
|
||||||
)
|
|
||||||
|
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 {
|
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(
|
for range columnSet {
|
||||||
"[%s] %-8s %-24s %s",
|
fmtStr += "%-12s"
|
||||||
|
}
|
||||||
|
|
||||||
|
vals := []interface{}{
|
||||||
widget.RowColor(idx),
|
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)
|
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"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/digitalocean/godo"
|
|
||||||
"github.com/wtfutil/wtf/utils"
|
"github.com/wtfutil/wtf/utils"
|
||||||
"github.com/wtfutil/wtf/view"
|
"github.com/wtfutil/wtf/view"
|
||||||
)
|
)
|
||||||
|
|
||||||
type dropletPropertiesTable struct {
|
type dropletPropertiesTable struct {
|
||||||
droplet *godo.Droplet
|
droplet *Droplet
|
||||||
propertyMap map[string]string
|
propertyMap map[string]string
|
||||||
|
|
||||||
colWidth0 int
|
colWidth0 int
|
||||||
@ -20,7 +19,7 @@ type dropletPropertiesTable struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// newDropletPropertiesTable creates and returns an instance of DropletPropertiesTable
|
// newDropletPropertiesTable creates and returns an instance of DropletPropertiesTable
|
||||||
func newDropletPropertiesTable(droplet *godo.Droplet) *dropletPropertiesTable {
|
func newDropletPropertiesTable(droplet *Droplet) *dropletPropertiesTable {
|
||||||
propTable := &dropletPropertiesTable{
|
propTable := &dropletPropertiesTable{
|
||||||
droplet: droplet,
|
droplet: droplet,
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
"github.com/olebedev/config"
|
"github.com/olebedev/config"
|
||||||
"github.com/wtfutil/wtf/cfg"
|
"github.com/wtfutil/wtf/cfg"
|
||||||
|
"github.com/wtfutil/wtf/utils"
|
||||||
"github.com/wtfutil/wtf/wtf"
|
"github.com/wtfutil/wtf/wtf"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -13,12 +14,21 @@ const (
|
|||||||
defaultTitle = "DigitalOcean"
|
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
|
// Settings defines the configuration properties for this module
|
||||||
type Settings struct {
|
type Settings struct {
|
||||||
common *cfg.Common
|
common *cfg.Common
|
||||||
|
|
||||||
apiKey string `help:"Your DigitalOcean API key."`
|
apiKey string `help:"Your DigitalOcean API key."`
|
||||||
dateFormat string `help:"The format to display dates and times in."`
|
columns []string `help:"A list of the droplet properties to display."`
|
||||||
|
dateFormat string `help:"The format to display dates and times in."`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSettingsFromYAML creates a new settings instance from a YAML config block
|
// NewSettingsFromYAML creates a new settings instance from a YAML config block
|
||||||
@ -28,6 +38,7 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co
|
|||||||
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
|
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
|
||||||
|
|
||||||
apiKey: ymlConfig.UString("apiKey", ymlConfig.UString("apikey", os.Getenv("WTF_DIGITALOCEAN_API_KEY"))),
|
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),
|
dateFormat: ymlConfig.UString("dateFormat", wtf.DateFormat),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,10 +35,11 @@ type Widget struct {
|
|||||||
|
|
||||||
app *tview.Application
|
app *tview.Application
|
||||||
client *godo.Client
|
client *godo.Client
|
||||||
droplets []godo.Droplet
|
droplets []*Droplet
|
||||||
pages *tview.Pages
|
pages *tview.Pages
|
||||||
settings *Settings
|
settings *Settings
|
||||||
err error
|
|
||||||
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewWidget creates a new instance of a widget
|
// NewWidget creates a new instance of a widget
|
||||||
@ -127,7 +128,7 @@ func (widget *Widget) createClient() {
|
|||||||
|
|
||||||
// currentDroplet returns the currently-selected droplet, if there is one
|
// currentDroplet returns the currently-selected droplet, if there is one
|
||||||
// Returns nil if no droplet is selected
|
// Returns nil if no droplet is selected
|
||||||
func (widget *Widget) currentDroplet() *godo.Droplet {
|
func (widget *Widget) currentDroplet() *Droplet {
|
||||||
if len(widget.droplets) == 0 {
|
if len(widget.droplets) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -136,21 +137,24 @@ func (widget *Widget) currentDroplet() *godo.Droplet {
|
|||||||
return nil
|
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
|
// dropletsFetch uses the DigitalOcean API to fetch information about all the available droplets
|
||||||
func (widget *Widget) dropletsFetch() ([]godo.Droplet, error) {
|
func (widget *Widget) dropletsFetch() ([]*Droplet, error) {
|
||||||
dropletList := []godo.Droplet{}
|
dropletList := []*Droplet{}
|
||||||
opts := &godo.ListOptions{}
|
opts := &godo.ListOptions{}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
droplets, resp, err := widget.client.Droplets.List(context.Background(), opts)
|
doDroplets, resp, err := widget.client.Droplets.List(context.Background(), opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return dropletList, err
|
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() {
|
if resp.Links == nil || resp.Links.IsLastPage() {
|
||||||
break
|
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
|
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 {
|
func PrettyNumber(number float64) string {
|
||||||
p := message.NewPrinter(language.English)
|
p := message.NewPrinter(language.English)
|
||||||
|
|
||||||
if number == math.Trunc(number) {
|
if number == math.Trunc(number) {
|
||||||
return p.Sprintf("%.0f", 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