diff --git a/README.md b/README.md index d1a062a..2bf09c4 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,12 @@ You can also specify connection details using command-line flags: amtui --host 127.0.0.1 --port 9093 --scheme http ``` +AMTUI also supports basic authentication. You can specify the username and password using the `--username` and `--password` flags: + +```bash +amtui --host 127.0.0.1 --port 9093 --scheme http --username admin --password admin +``` + ## Dependencies AMTUI uses the following dependencies: diff --git a/pkg/alerts.go b/pkg/alerts.go index 7ceb691..0fb9421 100644 --- a/pkg/alerts.go +++ b/pkg/alerts.go @@ -26,12 +26,6 @@ func (tui *TUI) getFilteredAlerts(filter []string) { } func (tui *TUI) alerts(params *alert.GetAlertsParams) { - err := tui.checkConn() - if err != nil { - tui.Errorf("%s", err) - return - } - alerts, err := tui.amClient().Alert.GetAlerts(params) if err != nil { tui.Errorf("Error fetching alerts data: %s", err) diff --git a/pkg/config.go b/pkg/config.go index 7ade759..2c77a4b 100644 --- a/pkg/config.go +++ b/pkg/config.go @@ -18,6 +18,8 @@ Options: --host Alertmanager host -p, --port Alertmanager port -s, --scheme Alertmanager scheme (http or https) + --username Alertmanager username for basic auth + --password Alertmanager password for basic auth -v, --version Show version -h, --help Help ` diff --git a/pkg/config_test.go b/pkg/config_test.go index d4b3cec..b125ede 100644 --- a/pkg/config_test.go +++ b/pkg/config_test.go @@ -1,6 +1,8 @@ package pkg import ( + "fmt" + "github.com/spf13/viper" "os" "testing" @@ -9,11 +11,13 @@ import ( func TestInitConfigDefault(t *testing.T) { // Test case 1: Test with default values - os.Args = []string{"amtui", "--host", "localhost", "--port", "9093", "--scheme", "http"} + os.Args = []string{"amtui", "--host", "localhost", "--port", "9093", "--scheme", "http", "--username", "admin", "--password", "admin"} config := initConfig() assert.Equal(t, "localhost", config.Host) assert.Equal(t, "9093", config.Port) assert.Equal(t, "http", config.Scheme) + assert.Equal(t, "admin", config.Auth.Username) + assert.Equal(t, "admin", config.Auth.Password) } func TestInitConfigCustom(t *testing.T) { @@ -23,6 +27,9 @@ func TestInitConfigCustom(t *testing.T) { assert.Equal(t, "example.com", config.Host) assert.Equal(t, "9090", config.Port) assert.Equal(t, "https", config.Scheme) + assert.Equal(t, "example.com", viper.Get("host")) + assert.Equal(t, "9090", viper.Get("port")) + assert.Equal(t, "https", viper.Get("scheme")) } func TestInitInvalid(t *testing.T) { @@ -30,3 +37,18 @@ func TestInitInvalid(t *testing.T) { os.Args = []string{"amtui", "--invalid-flag"} assert.Panics(t, func() { printHelp(os.Stderr) }) } + +func TestInitHelp(t *testing.T) { + // Test case 4: Test with help flag + os.Args = []string{"amtui", "--help"} + assert.Panics(t, func() { printHelp(os.Stderr) }) +} + +func TestInitVersion(t *testing.T) { + // Test case 5: Test with version flag + os.Args = []string{"amtui", "--version"} + assert.Panics(t, func() { + fmt.Printf("Version: %s\nBuild Date: %s\nBuild Commit: %s\n", versionString, buildDate, buildCommit) + os.Exit(0) + }) +} diff --git a/pkg/silences.go b/pkg/silences.go index d54e066..0cb1516 100644 --- a/pkg/silences.go +++ b/pkg/silences.go @@ -25,12 +25,6 @@ func (tui *TUI) getFilteredSilences(filter []string) { } func (tui *TUI) silences(params *silence.GetSilencesParams) { - err := tui.checkConn() - if err != nil { - tui.Errorf("%s", err) - return - } - silences, err := tui.amClient().Silence.GetSilences(params) if err != nil { tui.Errorf("Error fetching silences data: %s", err) diff --git a/pkg/status.go b/pkg/status.go index 66d8118..d2131e9 100644 --- a/pkg/status.go +++ b/pkg/status.go @@ -12,12 +12,6 @@ import ( // fetch status data from alertmanager api func (tui *TUI) getStatus() { - err := tui.checkConn() - if err != nil { - tui.Errorf("%s", err) - return - } - params := general.NewGetStatusParams().WithTimeout(5 * time.Second).WithContext(context.Background()) status, err := tui.amClient().General.GetStatus(params) if err != nil { diff --git a/pkg/tui.go b/pkg/tui.go index 3c8f25e..360da34 100644 --- a/pkg/tui.go +++ b/pkg/tui.go @@ -1,6 +1,7 @@ package pkg import ( + "log" "strings" "github.com/gdamore/tcell/v2" @@ -79,6 +80,12 @@ func InitTUI() *TUI { // configuration management tui.Config = initConfig() + // check if alertmanager host is up or not + err := tui.checkConn() + if err != nil { + log.Panicf("Error connecting to alertmanager %s\n", err) + } + // listen for keyboard events and if q pressed, exit if l pressed in SidebarList focus on PreviewList if h is pressed in PreviewList focus on SidebarList tui.App.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { if event.Key() == tcell.KeyRune && tui.App.GetFocus() != tui.Filter {