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:
parent
5522aa7a5d
commit
07f4d3f524
@ -6,45 +6,36 @@ import (
|
|||||||
"github.com/wtfutil/wtf/utils"
|
"github.com/wtfutil/wtf/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const maxColWidth = 10
|
const maxColWidth = 12
|
||||||
|
|
||||||
// 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",
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(defaultColumns) < 1 {
|
if len(columnSet) < 1 {
|
||||||
return title, " no columns defined", false
|
return title, " no columns defined", false
|
||||||
}
|
}
|
||||||
|
|
||||||
str := fmt.Sprintf(" [::b][%s]", widget.settings.common.Colors.Subheading)
|
str := fmt.Sprintf(" [::b][%s]", widget.settings.common.Colors.Subheading)
|
||||||
|
|
||||||
for _, colName := range defaultColumns {
|
for _, colName := range columnSet {
|
||||||
truncName := utils.Truncate(colName, maxColWidth, false)
|
truncName := utils.Truncate(colName, maxColWidth, false)
|
||||||
|
|
||||||
str += fmt.Sprintf("%-10s", truncName)
|
str += fmt.Sprintf("%-12s", truncName)
|
||||||
}
|
}
|
||||||
|
|
||||||
str += "\n"
|
str += "\n"
|
||||||
|
|
||||||
for idx, droplet := range widget.droplets {
|
for idx, droplet := range widget.droplets {
|
||||||
// This defines the formatting for the row, one tab-seperated string
|
// This defines the formatting for the row, one tab-seperated string for each defined column
|
||||||
// for each defined column
|
|
||||||
fmtStr := " [%s]"
|
fmtStr := " [%s]"
|
||||||
for range defaultColumns {
|
|
||||||
fmtStr += "%-10s"
|
for range columnSet {
|
||||||
|
fmtStr += "%-12s"
|
||||||
}
|
}
|
||||||
|
|
||||||
vals := []interface{}{
|
vals := []interface{}{
|
||||||
@ -52,7 +43,7 @@ func (widget *Widget) content() (string, string, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Dynamically access the droplet to get the requested columns values
|
// Dynamically access the droplet to get the requested columns values
|
||||||
for _, colName := range defaultColumns {
|
for _, colName := range columnSet {
|
||||||
val := droplet.ValueForColumn(colName)
|
val := droplet.ValueForColumn(colName)
|
||||||
truncVal := utils.Truncate(val, maxColWidth, false)
|
truncVal := utils.Truncate(val, maxColWidth, false)
|
||||||
|
|
||||||
|
@ -8,6 +8,8 @@ import (
|
|||||||
"github.com/digitalocean/godo"
|
"github.com/digitalocean/godo"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const invalidColumn = "???"
|
||||||
|
|
||||||
// Droplet represents WTF's view of a DigitalOcean droplet
|
// Droplet represents WTF's view of a DigitalOcean droplet
|
||||||
type Droplet struct {
|
type Droplet struct {
|
||||||
godo.Droplet
|
godo.Droplet
|
||||||
@ -43,6 +45,8 @@ func NewDroplet(doDroplet godo.Droplet) *Droplet {
|
|||||||
return droplet
|
return droplet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* -------------------- Exported Functions -------------------- */
|
||||||
|
|
||||||
// ValueForColumn returns a string value for the given column
|
// ValueForColumn returns a string value for the given column
|
||||||
func (drop *Droplet) ValueForColumn(colName string) string {
|
func (drop *Droplet) ValueForColumn(colName string) string {
|
||||||
r := reflect.ValueOf(drop)
|
r := reflect.ValueOf(drop)
|
||||||
@ -60,7 +64,12 @@ func (drop *Droplet) ValueForColumn(colName string) string {
|
|||||||
case "Region":
|
case "Region":
|
||||||
strVal = drop.Region.ValueForColumn(split[1])
|
strVal = drop.Region.ValueForColumn(split[1])
|
||||||
default:
|
default:
|
||||||
strVal = fmt.Sprintf("%v", f)
|
if !f.IsValid() {
|
||||||
|
strVal = invalidColumn
|
||||||
|
} else {
|
||||||
|
strVal = fmt.Sprintf("%v", f)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return strVal
|
return strVal
|
||||||
@ -71,6 +80,10 @@ func (reg *Image) ValueForColumn(colName string) string {
|
|||||||
r := reflect.ValueOf(reg)
|
r := reflect.ValueOf(reg)
|
||||||
f := reflect.Indirect(r).FieldByName(colName)
|
f := reflect.Indirect(r).FieldByName(colName)
|
||||||
|
|
||||||
|
if !f.IsValid() {
|
||||||
|
return invalidColumn
|
||||||
|
}
|
||||||
|
|
||||||
strVal := fmt.Sprintf("%v", f)
|
strVal := fmt.Sprintf("%v", f)
|
||||||
|
|
||||||
return strVal
|
return strVal
|
||||||
@ -81,6 +94,10 @@ func (reg *Region) ValueForColumn(colName string) string {
|
|||||||
r := reflect.ValueOf(reg)
|
r := reflect.ValueOf(reg)
|
||||||
f := reflect.Indirect(r).FieldByName(colName)
|
f := reflect.Indirect(r).FieldByName(colName)
|
||||||
|
|
||||||
|
if !f.IsValid() {
|
||||||
|
return invalidColumn
|
||||||
|
}
|
||||||
|
|
||||||
strVal := fmt.Sprintf("%v", f)
|
strVal := fmt.Sprintf("%v", f)
|
||||||
|
|
||||||
return strVal
|
return strVal
|
||||||
|
@ -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),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user