Skip to content

Commit

Permalink
spawn ocm-container in a new terminal (#59)
Browse files Browse the repository at this point in the history
* ocm-container is spawned in a new terminal

* error handling fixed for TUI

* refactored cluster login to work with multiple terminal emulators

* error handling fixed

* added two methods of cluster login, via emulator and local shell

* incidents page fix

* shell cluster login refactored
  • Loading branch information
Supreeth Basabattini authored Oct 28, 2021
1 parent 8c6f62a commit c732b81
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 16 deletions.
11 changes: 11 additions & 0 deletions cmd/pdcli/alerts/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ func alertsHandler(cmd *cobra.Command, args []string) error {
// Fetch the currently logged in user's ID.
user, err := client.GetCurrentUser(pdApi.GetCurrentUserOptions{})

if err != nil {
return err
}

// UI internals
tui.Client = client
tui.Username = user.Name
Expand Down Expand Up @@ -217,6 +221,13 @@ func alertsHandler(cmd *cobra.Command, args []string) error {
tui.AssginedTo = options.assignment
tui.FetchedAlerts = strconv.Itoa(len(alerts))

// Determine terminal emulator for cluster login
pdcli.InitTerminalEmulator()

if pdcli.Terminal != "" {
tui.HasEmulator = true
}

// Setup TUI
tui.Init()
initAlertsUI(&tui, alerts, ui.AlertsTableTitle)
Expand Down
79 changes: 67 additions & 12 deletions pkg/pdcli/alerts/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type Alert struct {
WebURL string
}

var Terminal string

// GetIncidents returns a slice of pagerduty incidents.
func GetIncidents(c client.PagerDutyClient, opts *pdApi.ListIncidentsOptions) ([]pdApi.Incident, error) {

Expand Down Expand Up @@ -267,32 +269,85 @@ func ParseAlertMetaData(alert Alert) string {
return alertData
}

// ClusterLogin spawns an instance of ocm-container.
func ClusterLogin(clusterID string) (bool, error) {
// InitTerminalEmulator tries to set a terminal emulator by trying some known terminal emulators.
func InitTerminalEmulator() {
emulators := []string{
"x-terminal-emulator",
"mate-terminal",
"gnome-terminal",
"terminator",
"xfce4-terminal",
"urxvt",
"rxvt",
"termit",
"Eterm",
"aterm",
"uxterm",
"xterm",
"roxterm",
"termite",
"kitty",
"hyper",
}

for _, t := range emulators {
cmd := exec.Command("command", "-v", t)

output, _ := cmd.CombinedOutput()

cmd.ProcessState.Exited()

term := string(output)

term = strings.TrimSpace(term)

if term != "" {
Terminal = term
}
}
}

// ClusterLoginEmulator spawns an instance of ocm-container in a new terminal.
func ClusterLoginEmulator(clusterID string) error {

var cmd *exec.Cmd

// Check if ocm-container is installed locally
ocmContainer, err := exec.LookPath("ocm-container")

if err != nil {
fmt.Println("ocm-container is not found.\nPlease install it via:", constants.OcmContainerURL)
return errors.New("ocm-container is not found.\nPlease install it via: " + constants.OcmContainerURL)
}

cmd := exec.Command(ocmContainer, clusterID)
// OCM container command to be executed for cluster login
ocmCommand := ocmContainer + " " + clusterID

cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd = exec.Command(Terminal, "-e", ocmCommand)

err = cmd.Run()

if err != nil {
return false, err
return err
}

// If the command exits, switch control flow back to the UI.
if cmd.ProcessState.Exited() {
return true, nil
return nil
}

// ClusterLoginShell spawns an instance of ocm-container in the same shell.
func ClusterLoginShell(clusterID string) *exec.Cmd {

// Check if ocm-container is installed locally
ocmContainer, err := exec.LookPath("ocm-container")

if err != nil {
fmt.Println("ocm-container is not found.\nPlease install it via:", constants.OcmContainerURL)
}

return false, nil
cmd := exec.Command(ocmContainer, clusterID)

cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

return cmd
}
27 changes: 23 additions & 4 deletions pkg/ui/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,38 @@ func (tui *TUI) initKeyboard() {
}

tui.AlertMetadata.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {

if event.Rune() == 'Y' || event.Rune() == 'y' {
tui.App.Stop()

hasExited, _ := pdcli.ClusterLogin(tui.ClusterID)
if tui.HasEmulator {
err := pdcli.ClusterLoginEmulator(tui.ClusterID)

if err != nil {
tui.showError(err.Error())
}
} else {
tui.App.Stop()

cmd := pdcli.ClusterLoginShell(tui.ClusterID)

err := cmd.Run()

if err != nil {
tui.showError(err.Error())
}

cmd.Wait()

if hasExited {
tui.Init()
tui.Pages.AddAndSwitchToPage(AlertsPageTitle, tui.Table, true)
err := tui.StartApp()
tui.Pages.AddPage(AckIncidentsPageTitle, tui.IncidentsTable, true, false)

err = tui.StartApp()

if err != nil {
panic(err)
}

}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/ui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type TUI struct {
Client client.PagerDutyClient
Primary string
Secondary string
HasEmulator bool
}

const (
Expand Down

0 comments on commit c732b81

Please sign in to comment.