mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Fix merge conflict in go.sum
Signed-off-by: Chris Cummer <chriscummer@me.com>
This commit is contained in:
92
modules/cds/favorites/display.go
Normal file
92
modules/cds/favorites/display.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package cdsfavorites
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/ovh/cds/sdk"
|
||||
)
|
||||
|
||||
func (widget *Widget) display() {
|
||||
widget.TextWidget.Redraw(widget.content)
|
||||
}
|
||||
|
||||
func (widget *Widget) content() (string, string, bool) {
|
||||
if len(widget.View.GetHighlights()) > 0 {
|
||||
widget.View.ScrollToHighlight()
|
||||
} else {
|
||||
widget.View.ScrollToBeginning()
|
||||
}
|
||||
|
||||
widget.Items = make([]int64, 0)
|
||||
|
||||
workflow := widget.currentCDSWorkflow()
|
||||
if workflow == nil {
|
||||
return "", " Workflow not selected ", false
|
||||
}
|
||||
|
||||
_, _, width, _ := widget.View.GetRect()
|
||||
str := widget.settings.common.SigilStr(len(widget.workflows), widget.Idx, width) + "\n"
|
||||
title := fmt.Sprintf("%s - %s", widget.CommonSettings().Title, widget.title(workflow))
|
||||
|
||||
str += widget.displayWorkflowRuns(workflow)
|
||||
|
||||
return title, str, false
|
||||
}
|
||||
|
||||
func (widget *Widget) title(workflow *sdk.Workflow) string {
|
||||
return fmt.Sprintf(
|
||||
"[%s]%s/%s[white]",
|
||||
widget.settings.common.Colors.TextTheme.Title,
|
||||
workflow.ProjectKey, workflow.Name,
|
||||
)
|
||||
}
|
||||
|
||||
func (widget *Widget) displayWorkflowRuns(workflow *sdk.Workflow) string {
|
||||
runs, _ := widget.client.WorkflowRunList(workflow.ProjectKey, workflow.Name, 0, 16)
|
||||
|
||||
widget.SetItemCount(len(runs))
|
||||
|
||||
if len(runs) == 0 {
|
||||
return " [grey]none[white]\n"
|
||||
}
|
||||
|
||||
content := ""
|
||||
for idx, run := range runs {
|
||||
var tags string
|
||||
for _, tag := range run.Tags {
|
||||
toadd := true
|
||||
for _, v := range widget.settings.hideTags {
|
||||
if v == tag.Tag {
|
||||
toadd = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if toadd {
|
||||
tags = fmt.Sprintf("%s%s:%s ", tags, tag.Tag, tag.Value)
|
||||
}
|
||||
}
|
||||
content += fmt.Sprintf(`[%s]["%d"]%d %-6s[""][gray] %s`, getStatusColor(run.Status), idx, run.Number, run.Status, tags)
|
||||
content += "\n"
|
||||
widget.Items = append(widget.Items, run.Number)
|
||||
}
|
||||
|
||||
return content
|
||||
}
|
||||
|
||||
func getStatusColor(status string) string {
|
||||
switch status {
|
||||
case sdk.StatusSuccess:
|
||||
return "green"
|
||||
case sdk.StatusBuilding, sdk.StatusWaiting:
|
||||
return "blue"
|
||||
case sdk.StatusFail:
|
||||
return "red"
|
||||
case sdk.StatusStopped:
|
||||
return "red"
|
||||
case sdk.StatusSkipped:
|
||||
return "grey"
|
||||
case sdk.StatusDisabled:
|
||||
return "grey"
|
||||
}
|
||||
return "red"
|
||||
}
|
||||
22
modules/cds/favorites/keyboard.go
Normal file
22
modules/cds/favorites/keyboard.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package cdsfavorites
|
||||
|
||||
import (
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
func (widget *Widget) initializeKeyboardControls() {
|
||||
widget.InitializeCommonControls(widget.Refresh)
|
||||
|
||||
widget.SetKeyboardChar("j", widget.Next, "Select next workflow")
|
||||
widget.SetKeyboardChar("k", widget.Prev, "Select previous workflow")
|
||||
widget.SetKeyboardChar("l", widget.NextSource, "Select next source")
|
||||
widget.SetKeyboardChar("h", widget.PrevSource, "Select previous source")
|
||||
widget.SetKeyboardChar("o", widget.openWorkflow, "Open workflow in browser")
|
||||
|
||||
widget.SetKeyboardKey(tcell.KeyDown, widget.Next, "Select next workflow")
|
||||
widget.SetKeyboardKey(tcell.KeyUp, widget.Prev, "Select previous workflow")
|
||||
widget.SetKeyboardKey(tcell.KeyRight, widget.NextSource, "Select next source")
|
||||
widget.SetKeyboardKey(tcell.KeyLeft, widget.PrevSource, "Select previous source")
|
||||
widget.SetKeyboardKey(tcell.KeyEnter, widget.openWorkflow, "Open workflow in browser")
|
||||
widget.SetKeyboardKey(tcell.KeyEsc, widget.Unselect, "Clear selection")
|
||||
}
|
||||
34
modules/cds/favorites/settings.go
Normal file
34
modules/cds/favorites/settings.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package cdsfavorites
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/olebedev/config"
|
||||
"github.com/wtfutil/wtf/cfg"
|
||||
"github.com/wtfutil/wtf/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultFocusable = true
|
||||
defaultTitle = "CDS Favorites"
|
||||
)
|
||||
|
||||
// Settings defines the configuration properties for this module
|
||||
type Settings struct {
|
||||
common *cfg.Common
|
||||
token string `help:"Your CDS API token."`
|
||||
apiURL string `help:"Your CDS API URL."`
|
||||
uiURL string
|
||||
hideTags []string `help:"Hide some workflow tags."`
|
||||
}
|
||||
|
||||
// NewSettingsFromYAML creates a new settings instance from a YAML config block
|
||||
func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
|
||||
settings := Settings{
|
||||
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
|
||||
token: ymlConfig.UString("token", ymlConfig.UString("token", os.Getenv("CDS_TOKEN"))),
|
||||
apiURL: ymlConfig.UString("apiURL", os.Getenv("CDS_API_URL")),
|
||||
hideTags: utils.ToStrs(ymlConfig.UList("hideTags")),
|
||||
}
|
||||
return &settings
|
||||
}
|
||||
152
modules/cds/favorites/widget.go
Normal file
152
modules/cds/favorites/widget.go
Normal file
@@ -0,0 +1,152 @@
|
||||
package cdsfavorites
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/ovh/cds/sdk"
|
||||
"github.com/ovh/cds/sdk/cdsclient"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/utils"
|
||||
"github.com/wtfutil/wtf/view"
|
||||
)
|
||||
|
||||
// Widget define wtf widget to register widget later
|
||||
type Widget struct {
|
||||
view.MultiSourceWidget
|
||||
view.KeyboardWidget
|
||||
view.TextWidget
|
||||
|
||||
workflows []sdk.Workflow
|
||||
|
||||
client cdsclient.Interface
|
||||
|
||||
settings *Settings
|
||||
Selected int
|
||||
maxItems int
|
||||
Items []int64
|
||||
}
|
||||
|
||||
// NewWidget creates a new instance of the widget
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
KeyboardWidget: view.NewKeyboardWidget(app, pages, settings.common),
|
||||
MultiSourceWidget: view.NewMultiSourceWidget(settings.common, "workflow", "workflows"),
|
||||
TextWidget: view.NewTextWidget(app, settings.common),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
||||
widget.initializeKeyboardControls()
|
||||
widget.View.SetRegions(true)
|
||||
widget.View.SetInputCapture(widget.InputCapture)
|
||||
widget.SetDisplayFunction(widget.display)
|
||||
|
||||
widget.Unselect()
|
||||
widget.KeyboardWidget.SetView(widget.View)
|
||||
|
||||
widget.client = cdsclient.New(cdsclient.Config{
|
||||
Host: settings.apiURL,
|
||||
BuitinConsumerAuthenticationToken: settings.token,
|
||||
})
|
||||
|
||||
config, _ := widget.client.ConfigUser()
|
||||
|
||||
if config.URLUI != "" {
|
||||
widget.settings.uiURL = config.URLUI
|
||||
}
|
||||
|
||||
widget.workflows = widget.buildWorkflowsCollection()
|
||||
return &widget
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
// SetItemCount sets the amount of workflows throughout the widgets display creation
|
||||
func (widget *Widget) SetItemCount(items int) {
|
||||
widget.maxItems = items
|
||||
}
|
||||
|
||||
// GetItemCount returns the amount of workflows calculated so far as an int
|
||||
func (widget *Widget) GetItemCount() int {
|
||||
return widget.maxItems
|
||||
}
|
||||
|
||||
// GetSelected returns the index of the currently highlighted item as an int
|
||||
func (widget *Widget) GetSelected() int {
|
||||
if widget.Selected < 0 {
|
||||
return 0
|
||||
}
|
||||
return widget.Selected
|
||||
}
|
||||
|
||||
// Next cycles the currently highlighted text down
|
||||
func (widget *Widget) Next() {
|
||||
widget.Selected++
|
||||
if widget.Selected >= widget.maxItems {
|
||||
widget.Selected = 0
|
||||
}
|
||||
widget.View.Highlight(strconv.Itoa(widget.Selected)).ScrollToHighlight()
|
||||
}
|
||||
|
||||
// Prev cycles the currently highlighted text up
|
||||
func (widget *Widget) Prev() {
|
||||
widget.Selected--
|
||||
if widget.Selected < 0 {
|
||||
widget.Selected = widget.maxItems - 1
|
||||
}
|
||||
widget.View.Highlight(strconv.Itoa(widget.Selected)).ScrollToHighlight()
|
||||
}
|
||||
|
||||
// Unselect stops highlighting the text and jumps the scroll position to the top
|
||||
func (widget *Widget) Unselect() {
|
||||
widget.Selected = -1
|
||||
widget.View.Highlight()
|
||||
widget.View.ScrollToBeginning()
|
||||
}
|
||||
|
||||
// Refresh reloads the data
|
||||
func (widget *Widget) Refresh() {
|
||||
widget.display()
|
||||
}
|
||||
|
||||
// HelpText displays the widgets controls
|
||||
func (widget *Widget) HelpText() string {
|
||||
return widget.KeyboardWidget.HelpText()
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) buildWorkflowsCollection() []sdk.Workflow {
|
||||
workflows := []sdk.Workflow{}
|
||||
data, _ := widget.client.Navbar()
|
||||
for _, v := range data {
|
||||
if v.Favorite && v.WorkflowName != "" {
|
||||
workflows = append(workflows, sdk.Workflow{ProjectKey: v.Key, Name: v.WorkflowName})
|
||||
}
|
||||
}
|
||||
return workflows
|
||||
}
|
||||
|
||||
func (widget *Widget) currentCDSWorkflow() *sdk.Workflow {
|
||||
if len(widget.workflows) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if widget.Idx < 0 || widget.Idx >= len(widget.workflows) {
|
||||
widget.Idx = 0
|
||||
}
|
||||
|
||||
p := widget.workflows[widget.Idx]
|
||||
return &p
|
||||
}
|
||||
|
||||
func (widget *Widget) openWorkflow() {
|
||||
currentSelection := widget.View.GetHighlights()
|
||||
if widget.Selected >= 0 && currentSelection[0] != "" {
|
||||
wf := widget.currentCDSWorkflow()
|
||||
url := fmt.Sprintf("%s/project/%s/workflow/%s/run/%d",
|
||||
widget.settings.uiURL, wf.ProjectKey, wf.Name, widget.Items[widget.Selected])
|
||||
utils.OpenFile(url)
|
||||
}
|
||||
}
|
||||
130
modules/cds/queue/display.go
Normal file
130
modules/cds/queue/display.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package cdsqueue
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ovh/cds/sdk"
|
||||
)
|
||||
|
||||
func (widget *Widget) display() {
|
||||
widget.TextWidget.Redraw(widget.content)
|
||||
}
|
||||
|
||||
func (widget *Widget) content() (string, string, bool) {
|
||||
if len(widget.View.GetHighlights()) > 0 {
|
||||
widget.View.ScrollToHighlight()
|
||||
} else {
|
||||
widget.View.ScrollToBeginning()
|
||||
}
|
||||
|
||||
widget.Items = make([]sdk.WorkflowNodeJobRun, 0)
|
||||
filter := widget.currentFilter()
|
||||
_, _, width, _ := widget.View.GetRect()
|
||||
|
||||
str := widget.settings.common.SigilStr(len(widget.filters), widget.Idx, width) + "\n"
|
||||
str += widget.displayQueue(filter)
|
||||
|
||||
title := fmt.Sprintf("%s - %s", widget.CommonSettings().Title, widget.title(filter))
|
||||
|
||||
return title, str, false
|
||||
}
|
||||
|
||||
func (widget *Widget) title(filter string) string {
|
||||
return fmt.Sprintf(
|
||||
"[%s]%d - %s[white]",
|
||||
widget.settings.common.Colors.TextTheme.Title,
|
||||
widget.maxItems,
|
||||
filter,
|
||||
)
|
||||
}
|
||||
|
||||
func (widget *Widget) displayQueue(filter string) string {
|
||||
runs, _ := widget.client.QueueWorkflowNodeJobRun(filter)
|
||||
|
||||
widget.SetItemCount(len(runs))
|
||||
|
||||
if len(runs) == 0 {
|
||||
return " [grey]none[white]\n"
|
||||
}
|
||||
|
||||
var content string
|
||||
for idx, job := range runs {
|
||||
content += fmt.Sprintf(`[grey]["%d"]%s`,
|
||||
idx, widget.generateQueueJobLine(job.ID, job.Parameters, job.Job, time.Since(job.Queued), job.BookedBy, job.Status))
|
||||
|
||||
widget.Items = append(widget.Items, job)
|
||||
}
|
||||
|
||||
return content
|
||||
}
|
||||
|
||||
func (widget *Widget) generateQueueJobLine(id int64, parameters []sdk.Parameter, executedJob sdk.ExecutedJob,
|
||||
duration time.Duration, bookedBy sdk.Service, status string) string {
|
||||
prj := getVarsInPbj("cds.project", parameters)
|
||||
workflow := getVarsInPbj("cds.workflow", parameters)
|
||||
node := getVarsInPbj("cds.node", parameters)
|
||||
run := getVarsInPbj("cds.run", parameters)
|
||||
triggeredBy := getVarsInPbj("cds.triggered_by.username", parameters)
|
||||
|
||||
row := make([]string, 6)
|
||||
row[0] = pad(fmt.Sprintf(sdk.Round(duration, time.Second).String()), 9)
|
||||
row[2] = pad(run, 7)
|
||||
row[3] = fmt.Sprintf("%s", pad(prj+"/"+workflow+"/"+node, 40))
|
||||
|
||||
if status == sdk.StatusBuilding {
|
||||
row[1] = pad(fmt.Sprintf(" %s.%s ", executedJob.WorkerName, executedJob.WorkerID), 27)
|
||||
} else if bookedBy.ID != 0 {
|
||||
row[1] = pad(fmt.Sprintf(" %s.%d ", bookedBy.Name, bookedBy.ID), 27)
|
||||
} else {
|
||||
row[1] = pad("", 27)
|
||||
}
|
||||
|
||||
row[4] = fmt.Sprintf("➤ %s", pad(triggeredBy, 17))
|
||||
|
||||
c := "grey"
|
||||
if status == sdk.StatusWaiting {
|
||||
if duration > 120*time.Second {
|
||||
c = "red"
|
||||
} else if duration > 50*time.Second {
|
||||
c = "yellow"
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Sprintf("[%s]%s [grey]%s %s %s %s\n", c, row[0], row[1], row[2], row[3], row[4])
|
||||
}
|
||||
|
||||
func getStatusColor(status string) string {
|
||||
switch status {
|
||||
case sdk.StatusSuccess:
|
||||
return "green"
|
||||
case sdk.StatusBuilding, sdk.StatusWaiting:
|
||||
return "blue"
|
||||
case sdk.StatusFail:
|
||||
return "red"
|
||||
case sdk.StatusStopped:
|
||||
return "red"
|
||||
case sdk.StatusSkipped:
|
||||
return "grey"
|
||||
case sdk.StatusDisabled:
|
||||
return "grey"
|
||||
}
|
||||
return "red"
|
||||
}
|
||||
|
||||
func pad(t string, size int) string {
|
||||
if len(t) > size {
|
||||
return t[0:size-3] + "..."
|
||||
}
|
||||
return t + strings.Repeat(" ", size-len(t))
|
||||
}
|
||||
|
||||
func getVarsInPbj(key string, ps []sdk.Parameter) string {
|
||||
for _, p := range ps {
|
||||
if p.Name == key {
|
||||
return p.Value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
22
modules/cds/queue/keyboard.go
Normal file
22
modules/cds/queue/keyboard.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package cdsqueue
|
||||
|
||||
import (
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
func (widget *Widget) initializeKeyboardControls() {
|
||||
widget.InitializeCommonControls(widget.Refresh)
|
||||
|
||||
widget.SetKeyboardChar("j", widget.Next, "Select next workflow")
|
||||
widget.SetKeyboardChar("k", widget.Prev, "Select previous workflow")
|
||||
widget.SetKeyboardChar("l", widget.NextSource, "Select next filter")
|
||||
widget.SetKeyboardChar("h", widget.PrevSource, "Select previous filter")
|
||||
widget.SetKeyboardChar("o", widget.openWorkflow, "Open workflow in browser")
|
||||
|
||||
widget.SetKeyboardKey(tcell.KeyDown, widget.Next, "Select next workflow")
|
||||
widget.SetKeyboardKey(tcell.KeyUp, widget.Prev, "Select previous workflow")
|
||||
widget.SetKeyboardKey(tcell.KeyRight, widget.NextSource, "Select next filter")
|
||||
widget.SetKeyboardKey(tcell.KeyLeft, widget.PrevSource, "Select previous filter")
|
||||
widget.SetKeyboardKey(tcell.KeyEnter, widget.openWorkflow, "Open workflow in browser")
|
||||
widget.SetKeyboardKey(tcell.KeyEsc, widget.Unselect, "Clear selection")
|
||||
}
|
||||
31
modules/cds/queue/settings.go
Normal file
31
modules/cds/queue/settings.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package cdsqueue
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/olebedev/config"
|
||||
"github.com/wtfutil/wtf/cfg"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultFocusable = true
|
||||
defaultTitle = "CDS Queue"
|
||||
)
|
||||
|
||||
// Settings defines the configuration properties for this module
|
||||
type Settings struct {
|
||||
common *cfg.Common
|
||||
token string `help:"Your CDS API token."`
|
||||
apiURL string `help:"Your CDS API URL."`
|
||||
uiURL string
|
||||
}
|
||||
|
||||
// NewSettingsFromYAML creates a new settings instance from a YAML config block
|
||||
func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
|
||||
settings := Settings{
|
||||
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
|
||||
token: ymlConfig.UString("token", ymlConfig.UString("token", os.Getenv("CDS_TOKEN"))),
|
||||
apiURL: ymlConfig.UString("apiURL", os.Getenv("CDS_API_URL")),
|
||||
}
|
||||
return &settings
|
||||
}
|
||||
145
modules/cds/queue/widget.go
Normal file
145
modules/cds/queue/widget.go
Normal file
@@ -0,0 +1,145 @@
|
||||
package cdsqueue
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/ovh/cds/sdk"
|
||||
"github.com/ovh/cds/sdk/cdsclient"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/utils"
|
||||
"github.com/wtfutil/wtf/view"
|
||||
)
|
||||
|
||||
// Widget define wtf widget to register widget later
|
||||
type Widget struct {
|
||||
view.MultiSourceWidget
|
||||
view.KeyboardWidget
|
||||
view.TextWidget
|
||||
|
||||
jobs []sdk.WorkflowNodeJobRun
|
||||
filters []string
|
||||
|
||||
client cdsclient.Interface
|
||||
|
||||
settings *Settings
|
||||
Selected int
|
||||
maxItems int
|
||||
Items []sdk.WorkflowNodeJobRun
|
||||
}
|
||||
|
||||
// NewWidget creates a new instance of the widget
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
KeyboardWidget: view.NewKeyboardWidget(app, pages, settings.common),
|
||||
MultiSourceWidget: view.NewMultiSourceWidget(settings.common, "workflow", "workflows"),
|
||||
TextWidget: view.NewTextWidget(app, settings.common),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
||||
widget.initializeKeyboardControls()
|
||||
widget.View.SetRegions(true)
|
||||
widget.View.SetInputCapture(widget.InputCapture)
|
||||
widget.SetDisplayFunction(widget.display)
|
||||
|
||||
widget.Unselect()
|
||||
widget.filters = []string{sdk.StatusWaiting, sdk.StatusBuilding}
|
||||
|
||||
widget.KeyboardWidget.SetView(widget.View)
|
||||
|
||||
widget.client = cdsclient.New(cdsclient.Config{
|
||||
Host: settings.apiURL,
|
||||
BuitinConsumerAuthenticationToken: settings.token,
|
||||
})
|
||||
|
||||
config, _ := widget.client.ConfigUser()
|
||||
|
||||
if config.URLUI != "" {
|
||||
widget.settings.uiURL = config.URLUI
|
||||
}
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
// SetItemCount sets the amount of workflows throughout the widgets display creation
|
||||
func (widget *Widget) SetItemCount(items int) {
|
||||
widget.maxItems = items
|
||||
}
|
||||
|
||||
// GetItemCount returns the amount of workflows calculated so far as an int
|
||||
func (widget *Widget) GetItemCount() int {
|
||||
return widget.maxItems
|
||||
}
|
||||
|
||||
// GetSelected returns the index of the currently highlighted item as an int
|
||||
func (widget *Widget) GetSelected() int {
|
||||
if widget.Selected < 0 {
|
||||
return 0
|
||||
}
|
||||
return widget.Selected
|
||||
}
|
||||
|
||||
// Next cycles the currently highlighted text down
|
||||
func (widget *Widget) Next() {
|
||||
widget.Selected++
|
||||
if widget.Selected >= widget.maxItems {
|
||||
widget.Selected = 0
|
||||
}
|
||||
widget.View.Highlight(strconv.Itoa(widget.Selected)).ScrollToHighlight()
|
||||
}
|
||||
|
||||
// Prev cycles the currently highlighted text up
|
||||
func (widget *Widget) Prev() {
|
||||
widget.Selected--
|
||||
if widget.Selected < 0 {
|
||||
widget.Selected = widget.maxItems - 1
|
||||
}
|
||||
widget.View.Highlight(strconv.Itoa(widget.Selected)).ScrollToHighlight()
|
||||
}
|
||||
|
||||
// Unselect stops highlighting the text and jumps the scroll position to the top
|
||||
func (widget *Widget) Unselect() {
|
||||
widget.Selected = -1
|
||||
widget.View.Highlight()
|
||||
widget.View.ScrollToBeginning()
|
||||
}
|
||||
|
||||
// Refresh reloads the data
|
||||
func (widget *Widget) Refresh() {
|
||||
widget.display()
|
||||
}
|
||||
|
||||
// HelpText displays the widgets controls
|
||||
func (widget *Widget) HelpText() string {
|
||||
return widget.KeyboardWidget.HelpText()
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) currentFilter() string {
|
||||
if len(widget.filters) == 0 {
|
||||
return sdk.StatusWaiting
|
||||
}
|
||||
|
||||
if widget.Idx < 0 || widget.Idx >= len(widget.filters) {
|
||||
widget.Idx = 0
|
||||
return sdk.StatusWaiting
|
||||
}
|
||||
|
||||
return widget.filters[widget.Idx]
|
||||
}
|
||||
|
||||
func (widget *Widget) openWorkflow() {
|
||||
currentSelection := widget.View.GetHighlights()
|
||||
if widget.Selected >= 0 && currentSelection[0] != "" {
|
||||
job := widget.Items[widget.Selected]
|
||||
prj := getVarsInPbj("cds.project", job.Parameters)
|
||||
workflow := getVarsInPbj("cds.workflow", job.Parameters)
|
||||
runNumber := getVarsInPbj("cds.run.number", job.Parameters)
|
||||
url := fmt.Sprintf("%s/project/%s/workflow/%s/run/%s", widget.settings.uiURL, prj, workflow, runNumber)
|
||||
utils.OpenFile(url)
|
||||
}
|
||||
}
|
||||
121
modules/cds/status/display.go
Normal file
121
modules/cds/status/display.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package cdsstatus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/ovh/cds/sdk"
|
||||
)
|
||||
|
||||
func (widget *Widget) display() {
|
||||
widget.TextWidget.Redraw(widget.content)
|
||||
}
|
||||
|
||||
func (widget *Widget) content() (string, string, bool) {
|
||||
if len(widget.View.GetHighlights()) > 0 {
|
||||
widget.View.ScrollToHighlight()
|
||||
} else {
|
||||
widget.View.ScrollToBeginning()
|
||||
}
|
||||
|
||||
widget.Items = make([]sdk.MonitoringStatusLine, 0)
|
||||
str := widget.displayStatus()
|
||||
title := widget.CommonSettings().Title
|
||||
return title, str, false
|
||||
}
|
||||
|
||||
func (widget *Widget) displayStatus() string {
|
||||
status, err := widget.client.MonStatus()
|
||||
|
||||
if err != nil || len(status.Lines) == 0 {
|
||||
return fmt.Sprintf(" [red]Error: %v[white]\n", err.Error())
|
||||
}
|
||||
|
||||
widget.SetItemCount(len(status.Lines))
|
||||
|
||||
var (
|
||||
global []string
|
||||
globalWarn []string
|
||||
globalRed []string
|
||||
ok []string
|
||||
warn []string
|
||||
red []string
|
||||
)
|
||||
|
||||
for _, line := range status.Lines {
|
||||
if line.Status == sdk.MonitoringStatusWarn && strings.Contains(line.Component, "Global") {
|
||||
globalWarn = append(globalWarn, line.String())
|
||||
} else if line.Status != sdk.MonitoringStatusOK && strings.Contains(line.Component, "Global") {
|
||||
globalRed = append(globalRed, line.String())
|
||||
} else if strings.Contains(line.Component, "Global") {
|
||||
global = append(global, line.String())
|
||||
} else if line.Status == sdk.MonitoringStatusWarn {
|
||||
warn = append(warn, line.String())
|
||||
} else if line.Status == sdk.MonitoringStatusOK {
|
||||
ok = append(ok, line.String())
|
||||
} else {
|
||||
red = append(red, line.String())
|
||||
}
|
||||
}
|
||||
var idx int
|
||||
var content string
|
||||
for _, v := range globalRed {
|
||||
content += fmt.Sprintf("[grey][\"%d\"][red]%s\n", idx, v)
|
||||
idx++
|
||||
}
|
||||
for _, v := range globalWarn {
|
||||
content += fmt.Sprintf("[grey][\"%d\"][yellow]%s\n", idx, v)
|
||||
idx++
|
||||
}
|
||||
for _, v := range global {
|
||||
content += fmt.Sprintf("[grey][\"%d\"][grey]%s\n", idx, v)
|
||||
idx++
|
||||
}
|
||||
for _, v := range red {
|
||||
content += fmt.Sprintf("[grey][\"%d\"][red]%s\n", idx, v)
|
||||
idx++
|
||||
}
|
||||
for _, v := range warn {
|
||||
content += fmt.Sprintf("[grey][\"%d\"][yellow]%s\n", idx, v)
|
||||
idx++
|
||||
}
|
||||
for _, v := range ok {
|
||||
content += fmt.Sprintf("[grey][\"%d\"][grey]%s\n", idx, v)
|
||||
idx++
|
||||
}
|
||||
return content
|
||||
}
|
||||
|
||||
func getStatusColor(status string) string {
|
||||
switch status {
|
||||
case sdk.StatusSuccess:
|
||||
return "green"
|
||||
case sdk.StatusBuilding, sdk.StatusWaiting:
|
||||
return "blue"
|
||||
case sdk.StatusFail:
|
||||
return "red"
|
||||
case sdk.StatusStopped:
|
||||
return "red"
|
||||
case sdk.StatusSkipped:
|
||||
return "grey"
|
||||
case sdk.StatusDisabled:
|
||||
return "grey"
|
||||
}
|
||||
return "red"
|
||||
}
|
||||
|
||||
func pad(t string, size int) string {
|
||||
if len(t) > size {
|
||||
return t[0:size-3] + "..."
|
||||
}
|
||||
return t + strings.Repeat(" ", size-len(t))
|
||||
}
|
||||
|
||||
func getVarsInPbj(key string, ps []sdk.Parameter) string {
|
||||
for _, p := range ps {
|
||||
if p.Name == key {
|
||||
return p.Value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
18
modules/cds/status/keyboard.go
Normal file
18
modules/cds/status/keyboard.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package cdsstatus
|
||||
|
||||
import (
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
func (widget *Widget) initializeKeyboardControls() {
|
||||
widget.InitializeCommonControls(widget.Refresh)
|
||||
|
||||
widget.SetKeyboardChar("j", widget.Next, "Select next line")
|
||||
widget.SetKeyboardChar("k", widget.Prev, "Select previous line")
|
||||
widget.SetKeyboardChar("o", widget.openWorkflow, "Open status in browser")
|
||||
|
||||
widget.SetKeyboardKey(tcell.KeyDown, widget.Next, "Select next line")
|
||||
widget.SetKeyboardKey(tcell.KeyUp, widget.Prev, "Select previous line")
|
||||
widget.SetKeyboardKey(tcell.KeyEnter, widget.openWorkflow, "Open status in browser")
|
||||
widget.SetKeyboardKey(tcell.KeyEsc, widget.Unselect, "Clear selection")
|
||||
}
|
||||
31
modules/cds/status/settings.go
Normal file
31
modules/cds/status/settings.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package cdsstatus
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/olebedev/config"
|
||||
"github.com/wtfutil/wtf/cfg"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultFocusable = true
|
||||
defaultTitle = "CDS Status"
|
||||
)
|
||||
|
||||
// Settings defines the configuration properties for this module
|
||||
type Settings struct {
|
||||
common *cfg.Common
|
||||
token string `help:"Your CDS API token."`
|
||||
apiURL string `help:"Your CDS API URL."`
|
||||
uiURL string
|
||||
}
|
||||
|
||||
// NewSettingsFromYAML creates a new settings instance from a YAML config block
|
||||
func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
|
||||
settings := Settings{
|
||||
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
|
||||
token: ymlConfig.UString("token", ymlConfig.UString("token", os.Getenv("CDS_TOKEN"))),
|
||||
apiURL: ymlConfig.UString("apiURL", os.Getenv("CDS_API_URL")),
|
||||
}
|
||||
return &settings
|
||||
}
|
||||
141
modules/cds/status/widget.go
Normal file
141
modules/cds/status/widget.go
Normal file
@@ -0,0 +1,141 @@
|
||||
package cdsstatus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/ovh/cds/sdk"
|
||||
"github.com/ovh/cds/sdk/cdsclient"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/utils"
|
||||
"github.com/wtfutil/wtf/view"
|
||||
)
|
||||
|
||||
// Widget define wtf widget to register widget later
|
||||
type Widget struct {
|
||||
view.MultiSourceWidget
|
||||
view.KeyboardWidget
|
||||
view.TextWidget
|
||||
|
||||
jobs []sdk.WorkflowNodeJobRun
|
||||
filters []string
|
||||
|
||||
client cdsclient.Interface
|
||||
|
||||
settings *Settings
|
||||
Selected int
|
||||
maxItems int
|
||||
Items []sdk.MonitoringStatusLine
|
||||
}
|
||||
|
||||
// NewWidget creates a new instance of the widget
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
KeyboardWidget: view.NewKeyboardWidget(app, pages, settings.common),
|
||||
MultiSourceWidget: view.NewMultiSourceWidget(settings.common, "workflow", "workflows"),
|
||||
TextWidget: view.NewTextWidget(app, settings.common),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
||||
widget.initializeKeyboardControls()
|
||||
widget.View.SetRegions(true)
|
||||
widget.View.SetInputCapture(widget.InputCapture)
|
||||
widget.SetDisplayFunction(widget.display)
|
||||
|
||||
widget.Unselect()
|
||||
widget.filters = []string{sdk.StatusWaiting, sdk.StatusBuilding}
|
||||
|
||||
widget.KeyboardWidget.SetView(widget.View)
|
||||
|
||||
widget.client = cdsclient.New(cdsclient.Config{
|
||||
Host: settings.apiURL,
|
||||
BuitinConsumerAuthenticationToken: settings.token,
|
||||
})
|
||||
|
||||
config, _ := widget.client.ConfigUser()
|
||||
|
||||
if config.URLUI != "" {
|
||||
widget.settings.uiURL = config.URLUI
|
||||
}
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
// SetItemCount sets the amount of line throughout the widgets display creation
|
||||
func (widget *Widget) SetItemCount(items int) {
|
||||
widget.maxItems = items
|
||||
}
|
||||
|
||||
// GetItemCount returns the amount of line calculated so far as an int
|
||||
func (widget *Widget) GetItemCount() int {
|
||||
return widget.maxItems
|
||||
}
|
||||
|
||||
// GetSelected returns the index of the currently highlighted item as an int
|
||||
func (widget *Widget) GetSelected() int {
|
||||
if widget.Selected < 0 {
|
||||
return 0
|
||||
}
|
||||
return widget.Selected
|
||||
}
|
||||
|
||||
// Next cycles the currently highlighted text down
|
||||
func (widget *Widget) Next() {
|
||||
widget.Selected++
|
||||
if widget.Selected >= widget.maxItems {
|
||||
widget.Selected = 0
|
||||
}
|
||||
widget.View.Highlight(strconv.Itoa(widget.Selected)).ScrollToHighlight()
|
||||
}
|
||||
|
||||
// Prev cycles the currently highlighted text up
|
||||
func (widget *Widget) Prev() {
|
||||
widget.Selected--
|
||||
if widget.Selected < 0 {
|
||||
widget.Selected = widget.maxItems - 1
|
||||
}
|
||||
widget.View.Highlight(strconv.Itoa(widget.Selected)).ScrollToHighlight()
|
||||
}
|
||||
|
||||
// Unselect stops highlighting the text and jumps the scroll position to the top
|
||||
func (widget *Widget) Unselect() {
|
||||
widget.Selected = -1
|
||||
widget.View.Highlight()
|
||||
widget.View.ScrollToBeginning()
|
||||
}
|
||||
|
||||
// Refresh reloads the data
|
||||
func (widget *Widget) Refresh() {
|
||||
widget.display()
|
||||
}
|
||||
|
||||
// HelpText displays the widgets controls
|
||||
func (widget *Widget) HelpText() string {
|
||||
return widget.KeyboardWidget.HelpText()
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) currentFilter() string {
|
||||
if len(widget.filters) == 0 {
|
||||
return sdk.StatusWaiting
|
||||
}
|
||||
|
||||
if widget.Idx < 0 || widget.Idx >= len(widget.filters) {
|
||||
widget.Idx = 0
|
||||
return sdk.StatusWaiting
|
||||
}
|
||||
|
||||
return widget.filters[widget.Idx]
|
||||
}
|
||||
|
||||
func (widget *Widget) openWorkflow() {
|
||||
currentSelection := widget.View.GetHighlights()
|
||||
if widget.Selected >= 0 && currentSelection[0] != "" {
|
||||
url := fmt.Sprintf("%s/admin/services", widget.settings.uiURL)
|
||||
utils.OpenFile(url)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user