diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
new file mode 100644
index 0000000..a4c482d
--- /dev/null
+++ b/.github/workflows/test.yml
@@ -0,0 +1,16 @@
+name: test
+on:
+ pull_request:
+ branches:
+ - main
+jobs:
+ build:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v3
+ - name: Set up Go
+ uses: actions/setup-go@v4
+ with:
+ go-version: '1.20'
+ - name: Test
+ run: go test -v ./pkg/...
\ No newline at end of file
diff --git a/.goreleaser.yml b/.goreleaser.yml
index 655ee14..cb26e83 100644
--- a/.goreleaser.yml
+++ b/.goreleaser.yml
@@ -7,6 +7,8 @@ builds:
binary: amtui
env:
- CGO_ENABLED=0
+ ldflags:
+ - -s -w -X pkg.versionString={{ .Tag }
goos:
- linux
- darwin
diff --git a/dist/config.yaml b/dist/config.yaml
new file mode 100644
index 0000000..7d04d32
--- /dev/null
+++ b/dist/config.yaml
@@ -0,0 +1,113 @@
+project_name: amtui
+release:
+ github:
+ owner: pehlicd
+ name: amtui
+ name_template: '{{.Tag}}'
+builds:
+ - id: amtui
+ goos:
+ - linux
+ - darwin
+ - windows
+ goarch:
+ - amd64
+ - arm64
+ goarm:
+ - "6"
+ gomips:
+ - hardfloat
+ goamd64:
+ - v1
+ targets:
+ - linux_amd64_v1
+ - linux_arm64
+ - darwin_amd64_v1
+ - darwin_arm64
+ - windows_amd64_v1
+ ignore:
+ - goos: windows
+ goarch: arm64
+ dir: .
+ main: .
+ binary: amtui
+ builder: go
+ gobinary: go
+ command: build
+ ldflags:
+ - -s -w -X pkg.versionString={{ .Tag }
+ env:
+ - CGO_ENABLED=0
+archives:
+ - id: default
+ builds:
+ - amtui
+ name_template: '{{ .ProjectName }}_{{ .Tag }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
+ format: tar.gz
+ wrap_in_directory: "false"
+ files:
+ - src: LICENSE
+snapshot:
+ name_template: '{{ .Version }}-SNAPSHOT-{{ .ShortCommit }}'
+checksum:
+ name_template: '{{ .ProjectName }}_{{ .Version }}_checksums.txt'
+ algorithm: sha256
+dist: dist
+env_files:
+ github_token: ~/.config/goreleaser/github_token
+ gitlab_token: ~/.config/goreleaser/gitlab_token
+ gitea_token: ~/.config/goreleaser/gitea_token
+before:
+ hooks:
+ - go mod download
+source:
+ name_template: '{{ .ProjectName }}-{{ .Version }}'
+ format: tar.gz
+gomod:
+ gobinary: go
+announce:
+ twitter:
+ message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}'
+ mastodon:
+ message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}'
+ server: ""
+ reddit:
+ title_template: '{{ .ProjectName }} {{ .Tag }} is out!'
+ url_template: '{{ .ReleaseURL }}'
+ slack:
+ message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}'
+ username: GoReleaser
+ discord:
+ message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}'
+ author: GoReleaser
+ color: "3888754"
+ icon_url: https://goreleaser.com/static/avatar.png
+ teams:
+ title_template: '{{ .ProjectName }} {{ .Tag }} is out!'
+ message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}'
+ color: '#2D313E'
+ icon_url: https://goreleaser.com/static/avatar.png
+ smtp:
+ subject_template: '{{ .ProjectName }} {{ .Tag }} is out!'
+ body_template: 'You can view details from: {{ .ReleaseURL }}'
+ mattermost:
+ message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}'
+ title_template: '{{ .ProjectName }} {{ .Tag }} is out!'
+ username: GoReleaser
+ linkedin:
+ message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}'
+ telegram:
+ message_template: '{{ .ProjectName }} {{ mdv2escape .Tag }} is out! Check it out at {{ mdv2escape .ReleaseURL }}'
+ parse_mode: MarkdownV2
+ webhook:
+ message_template: '{ "message": "{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}"}'
+ content_type: application/json; charset=utf-8
+ opencollective:
+ title_template: '{{ .Tag }}'
+ message_template: '{{ .ProjectName }} {{ .Tag }} is out!
Check it out at {{ .ReleaseURL }}'
+git:
+ tag_sort: -version:refname
+github_urls:
+ download: https://github.com
+gitlab_urls:
+ download: https://gitlab.com
diff --git a/go.mod b/go.mod
index a23254a..f8eb033 100644
--- a/go.mod
+++ b/go.mod
@@ -9,10 +9,12 @@ require (
github.com/rivo/tview v0.0.0-20230814110005-ccc2c8119703
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.16.0
+ github.com/stretchr/testify v1.8.3
)
require (
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
+ github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
@@ -36,6 +38,7 @@ require (
github.com/oklog/ulid v1.3.1 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
+ github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
diff --git a/pkg/config.go b/pkg/config.go
index d158c95..089214f 100644
--- a/pkg/config.go
+++ b/pkg/config.go
@@ -1,6 +1,8 @@
package pkg
import (
+ "fmt"
+ "io"
"log"
"os"
@@ -8,30 +10,76 @@ import (
"github.com/spf13/viper"
)
+const (
+ helpMessage = `Usage: amtui [options]
+
+Options:
+ --host Alertmanager host
+ -p, --port Alertmanager port
+ -i, --insecure For insecurely connecting to Alertmanager
+ -v, --version Show version
+ -h, --help Help
+`
+ versionString
+)
+
+var (
+ fl = flag.NewFlagSet("amtui", flag.ExitOnError)
+ host = fl.String("host", "localhost", "Alertmanager host")
+ port = fl.StringP("port", "p", "9093", "Alertmanager port")
+ insecure = fl.BoolP("insecure", "i", true, "For insecurely connecting to Alertmanager")
+ help = fl.BoolP("help", "h", false, "Show help")
+ version = fl.BoolP("version", "v", false, "Show version")
+ scheme = "http"
+)
+
+func printHelp(w io.Writer) {
+ _, err := fmt.Fprint(w,
+ helpMessage+"\n")
+ if err != nil {
+ log.Fatalf("Error writing help to stdout: %v", err)
+ }
+ os.Exit(0)
+}
+
type Config struct {
- Host string `yaml:"host"`
- Port string `yaml:"port"`
- Scheme string `yaml:"scheme"`
+ Host string `yaml:"host"`
+ Port string `yaml:"port"`
+ Insecure bool `yaml:"insecure"`
+ Scheme string `yaml:"scheme"`
}
func initConfig() Config {
+ if err := fl.Parse(os.Args[1:]); err != nil {
+ log.Fatalf("Error parsing flags: %v", err)
+ }
+
// Initialize Viper
viper.SetConfigName(".amtui") // Configuration file name without extension
viper.SetConfigType("yaml") // Configuration file type
viper.AddConfigPath(os.Getenv("HOME")) // Search for the configuration file in the $HOME directory
- // Set default values for your configuration struct
- viper.SetDefault("host", "localhost")
- viper.SetDefault("port", "9093")
- viper.SetDefault("scheme", "http")
+ if *insecure {
+ scheme = "http"
+ } else {
+ scheme = "https"
+ }
- var config Config
+ config := Config{
+ Host: *host,
+ Port: *port,
+ Insecure: *insecure,
+ Scheme: scheme,
+ }
- // Allow command-line flags to override the configuration
- flag.StringVar(&config.Host, "host", config.Host, "Alertmanager host")
- flag.StringVar(&config.Port, "port", config.Port, "Alertmanager port")
- flag.StringVar(&config.Scheme, "scheme", config.Scheme, "Alertmanager scheme http or https is supported")
- flag.Parse()
+ if *help {
+ printHelp(os.Stderr)
+ }
+
+ if *version {
+ fmt.Printf("amtui version: %s\n", versionString)
+ os.Exit(0)
+ }
// Bind environment variables (optional)
viper.AutomaticEnv()
diff --git a/pkg/config_test.go b/pkg/config_test.go
new file mode 100644
index 0000000..82c5b1b
--- /dev/null
+++ b/pkg/config_test.go
@@ -0,0 +1,35 @@
+package pkg
+
+import (
+ "os"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestInitConfigDefault(t *testing.T) {
+ // Test case 1: Test with default values
+ os.Args = []string{"amtui"}
+ config := initConfig()
+ assert.Equal(t, "localhost", config.Host)
+ assert.Equal(t, "9093", config.Port)
+ assert.Equal(t, true, config.Insecure)
+ assert.Equal(t, "http", config.Scheme)
+}
+
+func TestInitConfigCustom(t *testing.T) {
+ // Test case 2: Test with custom values
+ os.Args = []string{"amtui", "--host", "example.com", "--port", "9090", "--insecure=false"}
+ config := initConfig()
+ config = initConfig()
+ assert.Equal(t, "example.com", config.Host)
+ assert.Equal(t, "9090", config.Port)
+ assert.Equal(t, false, config.Insecure)
+ assert.Equal(t, "https", config.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) })
+}