Skip to content

Commit

Permalink
fixes log messages for ClusterLauncher
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Collins <[email protected]>
  • Loading branch information
clcollins committed Oct 9, 2024
1 parent ef4a8de commit 21d8fb8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ Configuration variables have the following precedence:

* ignoredusers: A list of PagerDuty user IDs to exclude from retrieved incident lists. It's recommended that the "silentuser" ID is in this list.
* editor: Your choice of editor. Defaults to the `$EDITOR` environment variable.
* cluster_login_cmd: Command used to login to a cluster from SREPD. Defaults to `/usr/local/bin/ocm backplane login`
* cluster_login_command: Command used to login to a cluster from SREPD. Defaults to `/usr/local/bin/ocm backplane login`
* terminal: Your choice of terminal to use when launching external commands. Defaults to `/usr/bin/gnome-terminal`.

__NOTE:__ The cluster_login_cmd and terminal accept a variable for `%%CLUSTER_ID%%` to stand in for the Cluster ID in the command. At least one of the two, most likely `cluster_login_cmd` MUST have the `%%CLUSTER_ID%%` placeholder set. See [AUTOMATIC LOGIN FEATURES](#automatic-login-features) for more details about config variables.
__NOTE:__ The cluster_login_command and terminal accept a variable for `%%CLUSTER_ID%%` to stand in for the Cluster ID in the command. At least one of the two, most likely `cluster_login_command` MUST have the `%%CLUSTER_ID%%` placeholder set. See [AUTOMATIC LOGIN FEATURES](#automatic-login-features) for more details about config variables.

An example srepd.yaml file might look like so:

Expand All @@ -56,7 +56,7 @@ editor: vim
# Note the trailing `--` is necessary for gnome-terminal and may be necessary
# for other terminals as well
terminal: /usr/bin/gnome-terminal --
cluster_login_cmd: ocm-container --clusterid %%CLUSTER_ID%%
cluster_login_command: ocm-container --clusterid %%CLUSTER_ID%%
# Note that aliases, etc, are not sourced by the shell command when launching.
# This means, for example, that `ocm-container`, as normally setup using an
Expand Down
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ but rather a simple tool to make on-call tasks easier.`,

launcher, err := launcher.NewClusterLauncher(viper.GetString("terminal"), viper.GetString("cluster_login_command"))
if err != nil {
log.Warn(err)
fmt.Println(err)
log.Fatal(err)
}

m, _ := tui.InitialModel(
Expand All @@ -93,6 +94,7 @@ but rather a simple tool to make on-call tasks easier.`,

_, err = p.Run()
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
},
Expand Down
24 changes: 15 additions & 9 deletions pkg/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package launcher

import (
"fmt"
"github.com/charmbracelet/log"
"strings"

"github.com/charmbracelet/log"
)

const clusterLoginCommandFlag = "cluster_login_command"
const terminalFlag = "terminal"

type ClusterLauncher struct {
Enabled bool
terminal []string
Expand Down Expand Up @@ -37,19 +41,19 @@ func (l *ClusterLauncher) validate() error {
errs := []error{}

if l.terminal == nil || l.terminal[0] == "" {
errs = append(errs, fmt.Errorf("terminal is not set"))
errs = append(errs, fmt.Errorf("%s is not set", terminalFlag))
}

if l.clusterLoginCommand == nil || l.clusterLoginCommand[0] == "" {
errs = append(errs, fmt.Errorf("clusterLoginCommand is not set"))
errs = append(errs, fmt.Errorf("%s is not set", clusterLoginCommandFlag))
}

if len(l.terminal) > 0 && strings.Contains(l.terminal[0], "%%") {
errs = append(errs, fmt.Errorf("first terminal argument cannot have a replaceable"))
errs = append(errs, fmt.Errorf("first terminal argument cannot have a replaceable value"))
}

if (!strings.Contains(strings.Join(l.clusterLoginCommand, " "), "%%CLUSTER_ID%%")) && (!strings.Contains(strings.Join(l.terminal, " "), "%%CLUSTER_ID%%")) {
errs = append(errs, fmt.Errorf("clusterLoginCommand must contain %%CLUSTER_ID%%"))
errs = append(errs, fmt.Errorf("%s must contain %%CLUSTER_ID%%", clusterLoginCommandFlag))
}

if len(errs) > 0 {
Expand All @@ -67,7 +71,7 @@ func (l *ClusterLauncher) BuildLoginCommand(vars map[string]string) []string {
// Handle the Terminal command
// The first arg should not be something replaceable, as checked in the
// validate function
log.Debug("launcher.ClusterLauncher():", "Building command from terminal", "terminal", l.terminal[0])
log.Debug("launcher.ClusterLauncher(): building command from terminal", "terminal", l.terminal[0])
command = append(command, l.terminal[0])

// If there are more than one terminal arguments, replace the vars
Expand All @@ -77,9 +81,9 @@ func (l *ClusterLauncher) BuildLoginCommand(vars map[string]string) []string {
command = append(command, replaceVars(l.terminal[1:], vars)...)
}
command = append(command, replaceVars(l.clusterLoginCommand, vars)...)
log.Debug("launcher.ClusterLauncher():", "Built command", command)
log.Debug("launcher.ClusterLauncher(): built command", "command", command)
for x, i := range command {
log.Debug("launcher.ClusterLauncher():", fmt.Sprintf("Built command argument [%d]", x), i)
log.Debug("launcher.ClusterLauncher(): build command argument", fmt.Sprintf("[%d]", x), i)
}

return command
Expand All @@ -93,10 +97,12 @@ func replaceVars(args []string, vars map[string]string) []string {
str := strings.Join(args, " ")

for k, v := range vars {
log.Debug("ClusterLauncher():", "Replacing vars in string", str, k, v)
log.Debug("launcher.replaceVars(): Replacing vars in string", k, v)
str = strings.Replace(str, k, v, -1)
}

log.Debug("launcher.replaceVars(): Replaced vars in string", "string", str)

transformedArgs := strings.Split(str, " ")
return transformedArgs
}
4 changes: 3 additions & 1 deletion pkg/tui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ func login(vars map[string]string, launcher launcher.ClusterLauncher) tea.Cmd {
}

processErr := c.Wait()

if processErr != nil {
if exitError, ok := processErr.(*exec.ExitError); ok {
execExitErr := &execErr{
Expand All @@ -508,7 +509,8 @@ func login(vars map[string]string, launcher launcher.ClusterLauncher) tea.Cmd {
if err != nil {
log.Warn("tui.login():", "execStdErr", err.Error())
return func() tea.Msg {
return loginFinishedMsg{err}
// Do not return the execStdErr as an error
return loginFinishedMsg{}
}
}

Expand Down

0 comments on commit 21d8fb8

Please sign in to comment.