Skip to content

Commit

Permalink
enabled support for custom commands to open cluster; still need to al…
Browse files Browse the repository at this point in the history
…low passing of the custom commands in config and cli

Signed-off-by: Chris Collins <[email protected]>
  • Loading branch information
clcollins committed Feb 9, 2024
1 parent c65605c commit 188b472
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.vscode/*
build/*
hack/*
log/*
*.log

1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/spf13/viper"
)

// TODO https://github.com/clcollins/srepd/issues/6 - allow custom commands
const cfgFile = "srepd.yaml"
const cfgFilePath = ".config/srepd/"
const defaultEditor = "/usr/bin/vim"
Expand Down
23 changes: 13 additions & 10 deletions pkg/tui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type waitForSelectedIncidentThenDoMsg struct {
msg tea.Msg
}

//lint:ignore U1000 - future proofing
type waitForSelectedIncidentThenRenderMsg string

func renderIncidentMarkdown(content string) (string, error) {
Expand All @@ -43,6 +44,7 @@ func renderIncidentMarkdown(content string) (string, error) {
return str, nil
}

//lint:ignore U1000 - future proofing
func updateSelectedIncident(p *pd.Config, id string) tea.Cmd {
return tea.Sequence(
getIncident(p, id),
Expand All @@ -61,7 +63,6 @@ func updateIncidentList(p *pd.Config) tea.Cmd {
return func() tea.Msg {
opts := pd.NewListIncidentOptsFromDefaults()
opts.TeamIDs = getTeamsAsStrings(p)

i, err := pd.GetIncidents(p.Client, opts)
return updatedIncidentListMsg{i, err}
}
Expand Down Expand Up @@ -180,16 +181,17 @@ func openEditorCmd(editor string) tea.Cmd {
})
}

type openOCMContainerMsg string
type ocmContainerFinishedMsg error
type openClusterMsg string
type openClusterFinishedMsg error

const term = "/usr/bin/gnome-terminal"
const clusterCmd = "o"
// Must not be aliases - must be real commands or links
const defaultTerm = "/usr/bin/gnome-terminal"
const defaultShell = "/bin/bash"
const defaultClusterCmd = "srepd_exec_ocm_container"

// TODO https://github.com/clcollins/srepd/issues/6 - allow custom commands
func openOCMContainer(cluster string) tea.Cmd {
debug("openOCMContainer")
c := exec.Command(term, "--", "/bin/bash", "srepd_exec_ocm_container", cluster)
func openCluster(cluster string) tea.Cmd {
debug("openCluster")
c := exec.Command(defaultTerm, "--", defaultShell, defaultClusterCmd, cluster)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
Expand All @@ -203,7 +205,7 @@ func openOCMContainer(cluster string) tea.Cmd {
}
}
return func() tea.Msg {
return ocmContainerFinishedMsg(nil)
return openClusterFinishedMsg(nil)
}
}

Expand Down Expand Up @@ -272,6 +274,7 @@ func silenceIncidents(i []*pagerduty.Incident, u []*pagerduty.User) tea.Cmd {
}
}

//lint:ignore U1000 - future proofing
type addIncidentNoteMsg string
type waitForSelectedIncidentsThenAnnotateMsg string
type addedIncidentNoteMsg struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/tui/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tui

import "testing"

//lint:ignore U1000 - future proofing
func testUpdateIncidentList(t *testing.T) {
// updateIncidentList accepts a pointer to a pd.Config struct
// and returns a tea.Cmd function. The tea.Cmd function returns
Expand Down
27 changes: 14 additions & 13 deletions pkg/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

// Set the selected incident to the incident returned from the getIncident command
case gotIncidentMsg:
debug("gotIncidentMsg", fmt.Sprint("TRUNCATED"))
debug("gotIncidentMsg", "TRUNCATED")
if msg.err != nil {
m.setStatus(msg.err.Error())
log.Fatal(msg.err)
Expand All @@ -204,7 +204,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
)

case gotIncidentNotesMsg:
debug("gotIncidentNotesMsg", fmt.Sprint("TRUNCATED"))
debug("gotIncidentNotesMsg", "TRUNCATED")
if msg.err != nil {
m.setStatus(msg.err.Error())
log.Fatal(msg.err)
Expand All @@ -220,7 +220,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

case gotIncidentAlertsMsg:
debug("gotIncidentAlertsMsg", fmt.Sprint("TRUNCATED"))
debug("gotIncidentAlertsMsg", "TRUNCATED")
if msg.err != nil {
m.setStatus(msg.err.Error())
log.Fatal(msg.err)
Expand Down Expand Up @@ -261,7 +261,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = append(cmds, updateIncidentList(m.config))

case updatedIncidentListMsg:
debug("updatedIncidentListMsg", fmt.Sprint("TRUNCATED"))
debug("updatedIncidentListMsg", "TRUNCATED")
if msg.err != nil {
m.setStatus(msg.err.Error())
log.Fatal(msg.err)
Expand Down Expand Up @@ -324,23 +324,23 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
cmds = append(cmds, func() tea.Msg { return getIncidentMsg(m.selectedIncident.ID) })

case openOCMContainerMsg:
debug("openOCMContainerMsg", fmt.Sprint(msg))
case openClusterMsg:
debug("openClusterMsg", fmt.Sprint(msg))
if len(m.selectedIncidentAlerts) == 0 {
debug(fmt.Sprintf("no alerts found for incident %s - requeuing", m.selectedIncident.ID))
return m, func() tea.Msg { return openOCMContainerMsg("sender: openOCMContainerMsg; requeue") }
return m, func() tea.Msg { return openClusterMsg("sender: openClusterMsg; requeue") }

}
if len(m.selectedIncidentAlerts) == 1 {
cluster := getDetailFieldFromAlert("cluster_id", m.selectedIncidentAlerts[0])
m.setStatus(fmt.Sprintf("opening cluster %s", cluster))
return m, func() tea.Msg { return openOCMContainer(cluster) }
return m, func() tea.Msg { return openCluster(cluster) }
}

// TODO https://github.com/clcollins/srepd/issues/1: Figure out how to prompt with list to select from
cluster := getDetailFieldFromAlert("cluster_id", m.selectedIncidentAlerts[0])
m.setStatus(fmt.Sprintf("multiple alerts for incident - opening cluster %s from first alert %s", cluster, m.selectedIncidentAlerts[0].ID))
return m, func() tea.Msg { return openOCMContainer(cluster) }
return m, func() tea.Msg { return openCluster(cluster) }

case waitForSelectedIncidentThenDoMsg:
debug("waitForSelectedIncidentThenDoMsg", fmt.Sprint(msg.action, msg.msg))
Expand Down Expand Up @@ -375,8 +375,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// func() tea.Msg { return clearSelectedIncidentsMsg("sender: acknowledgeIncidentsMsg") },
// )
// case "annotateIncidentsMsg":
case "openOCMContainerMsg":
return m, func() tea.Msg { return openOCMContainerMsg("open") }
// TODO: The "ACTION" here needs to be a tea.Msg, not a string
case "openClusterMsg":
return m, func() tea.Msg { return openClusterMsg("open") }
// case "reassignIncidentsMsg":
case "renderIncidentMsg":
return m, func() tea.Msg { return renderIncidentMsg("render") }
Expand Down Expand Up @@ -577,7 +578,7 @@ func switchTableFocusMode(m model, msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, defaultKeyMap.Open):
return m, tea.Sequence(
func() tea.Msg { return getIncidentMsg(m.table.SelectedRow()[1]) },
func() tea.Msg { return waitForSelectedIncidentThenDoMsg{action: "openOCMContainerMsg", msg: "wait"} },
func() tea.Msg { return waitForSelectedIncidentThenDoMsg{action: "openClusterMsg", msg: "wait"} },
)
}
}
Expand Down Expand Up @@ -628,7 +629,7 @@ func switchIncidentFocusedMode(m model, msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, defaultKeyMap.Open):
return m, tea.Sequence(
func() tea.Msg { return getIncidentMsg(m.table.SelectedRow()[1]) },
func() tea.Msg { return waitForSelectedIncidentThenDoMsg{action: "openOCMContainerMsg", msg: "wait"} },
func() tea.Msg { return waitForSelectedIncidentThenDoMsg{action: "openClusterMsg", msg: "wait"} },
)
}
}
Expand Down

0 comments on commit 188b472

Please sign in to comment.