Skip to content

Commit

Permalink
Minor code refactoring, update help message and doc.
Browse files Browse the repository at this point in the history
  • Loading branch information
pehlicd committed Sep 18, 2023
1 parent f8b0865 commit 49335b2
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 19 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 0 additions & 6 deletions pkg/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions pkg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`
Expand Down
24 changes: 23 additions & 1 deletion pkg/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pkg

import (
"fmt"
"github.com/spf13/viper"
"os"
"testing"

Expand All @@ -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) {
Expand All @@ -23,10 +27,28 @@ 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) {
// Test case 3: Test with invalid flags
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)
})
}
6 changes: 0 additions & 6 deletions pkg/silences.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions pkg/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions pkg/tui.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pkg

import (
"log"
"strings"

"github.com/gdamore/tcell/v2"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 49335b2

Please sign in to comment.