Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/reference/monitor_clusters.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ NOTE: The `monitor cluster` command is currently experimental only and may be ch
* <<get-panels, `cohctl get panels`>> - displays the panels that have been created
* <<add-panel, `cohctl add panel`>> - adds a panel to the list of panels that can be displayed
* <<remove-panel, `cohctl remove panel`>> - removes a panel that has been created
* <<set-default-style, `cohctl set default-style`>> - sets the default style for monitor clusters command
* <<get-default-style, `cohctl get default-style`>> - gets the default style for monitor clusters command

[#monitor-cluster]
==== Monitor Cluster
Expand Down Expand Up @@ -243,6 +245,28 @@ Are you sure you want to remove the panel my-panel? (y/n) y
panel my-panel was removed
----

[#set-default-style]
==== Set Default Style

include::../../build/_output/docs-gen/set_default_style.adoc[tag=text]

[source,bash]
----
cohctl set default style ice
Default style is now set to ice
----

[#get-default-style]
==== Get Default Style

include::../../build/_output/docs-gen/get_default_style.adoc[tag=text]

[source,bash]
----
cohctl get default style ice
Default style is: ice
----

=== See Also

* xref:clusters.adoc[Clusters]
Expand Down
166 changes: 140 additions & 26 deletions pkg/cmd/monitor_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/oracle/coherence-cli/pkg/fetcher"
"github.com/oracle/coherence-cli/pkg/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
"strings"
"sync"
Expand All @@ -23,15 +24,17 @@ import (

const (
defaultLayoutName = "default"
pressAdditional = "(press key in [] or mouse to toggle expand, ? = help)"
pressAdditionalReset = "(press key in [] or mouse/ESC to exit expand)"
pressAdditional = "(press key in [] to toggle expand, ? = help)"
pressAdditionalReset = "(press key in [] ESC to exit expand)"
noContent = " No Content"
errorContent = "Unable to retrieve data"
unableToFindPanel = "unable to find panel [%v], use --help or --show-panels to see all options"
serviceNameToken = "%SERVICE"
cacheNameToken = "%CACHE"
topicNameToken = "%TOPIC"
subscriberToken = "%SUBSCRIBER"
setDefaultStyleMsg = "Default style is now set to "
getDefaultStyleMsg = "Default style is: "
)

var (
Expand All @@ -55,6 +58,7 @@ var (
setMaxHeight int
originalMaxHeight int
padMaxHeightParam = true
colorStyleParam string
showAllPanels bool
ignoreRESTErrors bool
disablePadding bool
Expand All @@ -74,8 +78,62 @@ var (
heightAdjust int
noContentArray = []string{" ", noContent, " "}
drawnPositions map[rune]position

// color styles
boxStyle = tcell.StyleDefault
titleStyle = tcell.StyleDefault
textStyle = tcell.StyleDefault
)

type StyleConfig struct {
TextStyle tcell.Style
TitleStyle tcell.Style
BoxStyle tcell.Style
}

var styleConfigsMap = map[string]StyleConfig{
"default": {
TextStyle: tcell.StyleDefault,
TitleStyle: tcell.StyleDefault,
BoxStyle: tcell.StyleDefault,
},
"vintage": {
TextStyle: tcell.StyleDefault.Foreground(tcell.ColorWheat),
TitleStyle: tcell.StyleDefault.Foreground(tcell.ColorCoral),
BoxStyle: tcell.StyleDefault.Foreground(tcell.ColorWhite),
},
"oceanic": {
TextStyle: tcell.StyleDefault.Foreground(tcell.ColorLightCyan),
TitleStyle: tcell.StyleDefault.Foreground(tcell.ColorTurquoise),
BoxStyle: tcell.StyleDefault.Foreground(tcell.ColorNavy),
},
"tty": {
TextStyle: tcell.StyleDefault.Foreground(tcell.ColorGreen),
TitleStyle: tcell.StyleDefault.Foreground(tcell.ColorWhite),
BoxStyle: tcell.StyleDefault.Foreground(tcell.ColorGray),
},
"monokai": {
TextStyle: tcell.StyleDefault.Foreground(tcell.ColorDarkKhaki),
TitleStyle: tcell.StyleDefault.Foreground(tcell.ColorOrangeRed),
BoxStyle: tcell.StyleDefault.Foreground(tcell.ColorSlateGray),
},
"ice": {
TextStyle: tcell.StyleDefault.Foreground(tcell.ColorLightSteelBlue),
TitleStyle: tcell.StyleDefault.Foreground(tcell.ColorCoral),
BoxStyle: tcell.StyleDefault.Foreground(tcell.ColorSlateGray),
},
"high-contrast": {
TextStyle: tcell.StyleDefault.Foreground(tcell.ColorWhite),
TitleStyle: tcell.StyleDefault.Foreground(tcell.ColorRed),
BoxStyle: tcell.StyleDefault.Foreground(tcell.ColorWhite),
},
"light-mode": {
TextStyle: tcell.StyleDefault.Foreground(tcell.ColorBlack),
TitleStyle: tcell.StyleDefault.Foreground(tcell.ColorDarkBlue),
BoxStyle: tcell.StyleDefault.Foreground(tcell.ColorDarkGray),
},
}

var validPanels = []panelImpl{
createContentPanel(7, "caches", "Caches", "show caches", cachesContent, cachesPanelData, servicesPanelData),
createContentPanel(7, "cache-access", "Cache Access (%SERVICE/%CACHE)", "show cache access", cacheAccessContent, cachesPanelData, servicesPanelData),
Expand Down Expand Up @@ -169,6 +227,10 @@ Use --show-panels to show all available panels.`,
return err
}

if err = setColorStyle(); err != nil {
return err
}

if disablePadding {
padMaxHeightParam = false
}
Expand Down Expand Up @@ -214,7 +276,6 @@ Use --show-panels to show all available panels.`,
return err
}
defer screen.Fini()
screen.EnableMouse(tcell.MouseButtonEvents)

screen.SetStyle(tcell.StyleDefault)

Expand Down Expand Up @@ -265,20 +326,6 @@ Use --show-panels to show all available panels.`,
panic(err)
}
screen.Sync()
case *tcell.EventMouse:
if ev.Buttons() == tcell.Button1 {
if expandedPanel == "" {
x, y := ev.Position()
for r, pos := range drawnPositions {
if x >= pos.x && x < pos.x+pos.width && y >= pos.y && y < pos.y+pos.height {
updateExpanded(r, screen, dataFetcher, parsedLayout)
break
}
}
} else {
updateExpanded(rune('0'), screen, dataFetcher, parsedLayout)
}
}
case *tcell.EventKey:
pressedKey := ev.Rune()
// Exit for 'q', ESC, or CTRL-C
Expand Down Expand Up @@ -320,6 +367,72 @@ Use --show-panels to show all available panels.`,
},
}

// setDefaultStyleCmd represents the set default-style or command.
var setDefaultStyleCmd = &cobra.Command{
Use: "default-style style",
Short: "set default style for monitor clusters command",
Long: `The 'set default-style' command sets the default style for monitor clusters command.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
value := args[0]

_, err := getStyleValue(value)
if err != nil {
return err
}

viper.Set(defaultStyleKey, value)
err = WriteConfig()
if err != nil {
return err
}
cmd.Println(setDefaultStyleMsg + value)
return nil
},
}

// getDefaultStyleCmd represents the get default-style command.
var getDefaultStyleCmd = &cobra.Command{
Use: "default-style",
Short: "display the current default style for monitor clusters",
Long: `The 'get default-style' command displays the current style for monitor clusters.`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, _ []string) error {
cmd.Printf("%s%v\n", getDefaultStyleMsg, Config.DefaultStyle)
return nil
},
}

func setColorStyle() error {
// use default style if none specified
if colorStyleParam == "" {
colorStyleParam = Config.DefaultStyle
}
style, err := getStyleValue(colorStyleParam)
if err != nil {
return err
}

textStyle = style.TextStyle
titleStyle = style.TitleStyle
boxStyle = style.BoxStyle

return nil
}

func getStyleValue(styleValue string) (StyleConfig, error) {
style, ok := styleConfigsMap[styleValue]
if !ok {
valid := make([]string, 0, len(styleConfigsMap))
for k := range styleConfigsMap {
valid = append(valid, k)
}
return StyleConfig{}, fmt.Errorf("invalid color style %s, valid values are %v", colorStyleParam, valid)
}

return style, nil
}

func updateExpanded(pressedKey rune, screen tcell.Screen, dataFetcher fetcher.Fetcher, parsedLayout []string) {
if expandedPanel != "" {
expandedPanel = ""
Expand Down Expand Up @@ -383,7 +496,7 @@ func showHelp(screen tcell.Screen) {
" - '+' to increase max height of all panels",
" - '-' to decrease max height of all panels",
" - '0' to reset max height of all panels",
" - Key in [] or click mouse to expand that panel",
" - Key in [] to expand that panel",
" - ESC / CTRL-C to exit monitoring",
" ",
" Press any key to exit help.",
Expand All @@ -397,10 +510,10 @@ func showHelp(screen tcell.Screen) {
x := w/2 - 25
y := h/2 - lenHelp

drawBox(screen, x, y, x+53, y+lenHelp+2, tcell.StyleDefault, "Help")
drawBox(screen, x, y, x+53, y+lenHelp+2, boxStyle, "Help")

for line := 1; line <= lenHelp; line++ {
drawText(screen, x+1, y+line, x+w-1, y+h-1, tcell.StyleDefault, help[line-1])
drawText(screen, x+1, y+line, x+w-1, y+h-1, textStyle, help[line-1])
}
screen.Show()
_ = screen.PollEvent()
Expand All @@ -422,7 +535,7 @@ func updateScreen(screen tcell.Screen, dataFetcher fetcher.Fetcher, parsedLayout
if refresh {
startTime := time.Now()
if initialRefresh {
drawText(screen, w-20, 0, w, 0, tcell.StyleDefault, " Retrieving data...")
drawText(screen, w-20, 0, w, 0, textStyle, " Retrieving data...")
screen.Show()
initialRefresh = false
}
Expand All @@ -449,7 +562,7 @@ func updateScreen(screen tcell.Screen, dataFetcher fetcher.Fetcher, parsedLayout

drawHeader(screen, w, h, cluster, dataFetcher)

// re-create the list of drawn positions, so we can determine mouse click location
// re-create the list of drawn positions
drawnPositions = make(map[rune]position, 0)

var (
Expand Down Expand Up @@ -1309,10 +1422,10 @@ func drawContent(screen tcell.Screen, dataFetcher fetcher.Fetcher, panel panelIm
trimmedText = fmt.Sprintf("%v%s", string(tcell.RuneHLine), "(trimmed)")
}

drawBox(screen, x, y, x+w-1, y+h, tcell.StyleDefault, fmt.Sprintf("%s[%v]%s", parseTitle(title), string(code), trimmedText))
drawBox(screen, x, y, x+w-1, y+h, boxStyle, fmt.Sprintf("%s[%v]%s", parseTitle(title), string(code), trimmedText))

for line := 1; line <= rows; line++ {
drawText(screen, x+1, y+line, x+w-1, y+h-1, tcell.StyleDefault, content[line-1])
drawText(screen, x+1, y+line, x+w-1, y+h-1, textStyle, content[line-1])
}

return rows + 2, nil
Expand Down Expand Up @@ -1364,7 +1477,7 @@ func drawHeader(screen tcell.Screen, w, h int, cluster config.Cluster, dataFetch
title = fmt.Sprintf("%s%-*s", title, w-titleLen-2, " ")
}
}
drawText(screen, 1, 0, w-1, h-1, tcell.StyleDefault.Reverse(true), title)
drawText(screen, 1, 0, w-1, h-1, textStyle.Reverse(true), title)
}

// drawText draws text on the screen.
Expand Down Expand Up @@ -1416,7 +1529,7 @@ func drawBox(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, title string
s.SetContent(x2, y2, tcell.RuneLRCorner, nil, style)
}

drawText(s, x1+2, y1, x2-1, y2-1, style, title)
drawText(s, x1+2, y1, x2-1, y2-1, titleStyle, title)
}

// getValidPanelTypes returns the list of panels for the --help command.
Expand Down Expand Up @@ -1462,6 +1575,7 @@ func init() {
monitorClusterCmd.Flags().StringVarP(&serviceName, serviceNameOption, "S", "", serviceNameDescription)
monitorClusterCmd.Flags().StringVarP(&selectedCache, "cache-name", "C", "", "cache name")
monitorClusterCmd.Flags().StringVarP(&selectedTopic, "topic-name", "T", "", "topic name")
monitorClusterCmd.Flags().StringVarP(&colorStyleParam, "style", "", "", "color style")
monitorClusterCmd.Flags().Int64VarP(&subscriber, "subscriber-id", "B", 0, "subscriber")
monitorClusterCmd.Flags().IntVarP(&setMaxHeight, "max-height", "M", 0, "override max height for all panels")
monitorClusterCmd.Flags().BoolVarP(&ignoreSpecialCaches, "ignore-special", "", false, ignoreCachesDescription)
Expand Down
13 changes: 11 additions & 2 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const (
currentContextKey = "currentContext"
debugContextKey = "debug"
colorContextKey = "color"
defaultStyleKey = "defaultStyle"
useGradleContextKey = "useGradle"
ignoreCertsContextKey = "ignoreInvalidCerts"
requestTimeoutKey = "requestTimeout"
Expand Down Expand Up @@ -193,6 +194,7 @@ type CoherenceCLIConfig struct {
UseGradle bool `json:"useGradle"`
Profiles []ProfileValue `mapstructure:"profiles"`
Panels []Panel `mapstructure:"panels"`
DefaultStyle string `json:"defaultStyle"`
}

// ProfileValue describes a profile to be used for creating and starting clusters.
Expand Down Expand Up @@ -366,7 +368,7 @@ func initConfig() {

viper.AutomaticEnv() // read in environment variables that match

if err := viper.ReadInConfig(); err != nil {
if err = viper.ReadInConfig(); err != nil {
_, foundError := err.(viper.ConfigFileNotFoundError)
_, pathError := err.(*os.PathError)

Expand Down Expand Up @@ -403,7 +405,7 @@ func initConfig() {
}
} else {
// load the config
if err := viper.Unmarshal(&Config); err != nil {
if err = viper.Unmarshal(&Config); err != nil {
rootCmd.Println(err)
os.Exit(1)
}
Expand All @@ -419,6 +421,11 @@ func initConfig() {
}
}

// default defaultStyle
if Config.DefaultStyle == "" {
Config.DefaultStyle = "default"
}

// setup logs directory
logsDirectory = filepath.Join(cfgDirectory, logsDirName)
err = ensureLogsDir()
Expand Down Expand Up @@ -578,6 +585,7 @@ func Initialize(command *cobra.Command) *cobra.Command {
getCmd.AddCommand(getFederationIncomingCmd)
getCmd.AddCommand(getFederationOutgoingCmd)
getCmd.AddCommand(getPanelsCmd)
getCmd.AddCommand(getDefaultStyleCmd)

// set command
command.AddCommand(setCmd)
Expand All @@ -598,6 +606,7 @@ func Initialize(command *cobra.Command) *cobra.Command {
setCmd.AddCommand(setFederationCmd)
setCmd.AddCommand(setColorCmd)
setCmd.AddCommand(setClusterCmd)
setCmd.AddCommand(setDefaultStyleCmd)

// run command
command.AddCommand(runCmd)
Expand Down
2 changes: 2 additions & 0 deletions scripts/generate-doc-snippets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ create_doc $DOCS_DIR/discover_clusters "${COHCTL} discover clusters --help"
create_doc $DOCS_DIR/get_cluster_config "${COHCTL} get cluster-config --help"
create_doc $DOCS_DIR/get_cluster_description "${COHCTL} get cluster-description --help"
create_doc $DOCS_DIR/monitor_cluster "${COHCTL} monitor cluster --help"
create_doc $DOCS_DIR/set_default_style "${COHCTL} set default-style --help"
create_doc $DOCS_DIR/get_default_style "${COHCTL} get default-style --help"
create_doc $DOCS_DIR/add_panel "${COHCTL} add panel --help"
create_doc $DOCS_DIR/get_panels "${COHCTL} get panels --help"
create_doc $DOCS_DIR/remove_panel "${COHCTL} remove panel --help"
Expand Down
Loading