1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

WTF-986 Read DigitalOcean column configuration from settings

Signed-off-by: Chris Cummer <chriscummer@me.com>
This commit is contained in:
Chris Cummer 2020-10-13 13:09:09 -07:00
parent 5522aa7a5d
commit 07f4d3f524
3 changed files with 42 additions and 23 deletions

View File

@ -6,45 +6,36 @@ import (
"github.com/wtfutil/wtf/utils"
)
const maxColWidth = 10
// 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 = []string{
"Name",
"Status",
"Vcpus",
"Disk",
"Memory",
"Region.Slug",
}
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
}
if len(defaultColumns) < 1 {
if len(columnSet) < 1 {
return title, " no columns defined", false
}
str := fmt.Sprintf(" [::b][%s]", widget.settings.common.Colors.Subheading)
for _, colName := range defaultColumns {
for _, colName := range columnSet {
truncName := utils.Truncate(colName, maxColWidth, false)
str += fmt.Sprintf("%-10s", truncName)
str += fmt.Sprintf("%-12s", truncName)
}
str += "\n"
for idx, droplet := range widget.droplets {
// This defines the formatting for the row, one tab-seperated string
// for each defined column
// This defines the formatting for the row, one tab-seperated string for each defined column
fmtStr := " [%s]"
for range defaultColumns {
fmtStr += "%-10s"
for range columnSet {
fmtStr += "%-12s"
}
vals := []interface{}{
@ -52,7 +43,7 @@ func (widget *Widget) content() (string, string, bool) {
}
// Dynamically access the droplet to get the requested columns values
for _, colName := range defaultColumns {
for _, colName := range columnSet {
val := droplet.ValueForColumn(colName)
truncVal := utils.Truncate(val, maxColWidth, false)

View File

@ -8,6 +8,8 @@ import (
"github.com/digitalocean/godo"
)
const invalidColumn = "???"
// Droplet represents WTF's view of a DigitalOcean droplet
type Droplet struct {
godo.Droplet
@ -43,6 +45,8 @@ func NewDroplet(doDroplet godo.Droplet) *Droplet {
return droplet
}
/* -------------------- Exported Functions -------------------- */
// ValueForColumn returns a string value for the given column
func (drop *Droplet) ValueForColumn(colName string) string {
r := reflect.ValueOf(drop)
@ -60,7 +64,12 @@ func (drop *Droplet) ValueForColumn(colName string) string {
case "Region":
strVal = drop.Region.ValueForColumn(split[1])
default:
strVal = fmt.Sprintf("%v", f)
if !f.IsValid() {
strVal = invalidColumn
} else {
strVal = fmt.Sprintf("%v", f)
}
}
return strVal
@ -71,6 +80,10 @@ func (reg *Image) ValueForColumn(colName string) string {
r := reflect.ValueOf(reg)
f := reflect.Indirect(r).FieldByName(colName)
if !f.IsValid() {
return invalidColumn
}
strVal := fmt.Sprintf("%v", f)
return strVal
@ -81,6 +94,10 @@ func (reg *Region) ValueForColumn(colName string) string {
r := reflect.ValueOf(reg)
f := reflect.Indirect(r).FieldByName(colName)
if !f.IsValid() {
return invalidColumn
}
strVal := fmt.Sprintf("%v", f)
return strVal

View File

@ -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,12 +14,21 @@ 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."`
dateFormat string `help:"The format to display dates and times in."`
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."`
}
// 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),
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),
}