Skip to content

Commit

Permalink
update flags and add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
pehlicd committed Sep 10, 2023
1 parent b2d7284 commit 2ce9354
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 13 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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/...
2 changes: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ builds:
binary: amtui
env:
- CGO_ENABLED=0
ldflags:
- -s -w -X pkg.versionString={{ .Tag }
goos:
- linux
- darwin
Expand Down
113 changes: 113 additions & 0 deletions dist/config.yaml
Original file line number Diff line number Diff line change
@@ -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!<br/>Check it out at <a href="{{ .ReleaseURL }}">{{ .ReleaseURL }}</a>'
git:
tag_sort: -version:refname
github_urls:
download: https://github.com
gitlab_urls:
download: https://gitlab.com
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
74 changes: 61 additions & 13 deletions pkg/config.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,85 @@
package pkg

import (
"fmt"
"io"
"log"
"os"

flag "github.com/spf13/pflag"
"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()
Expand Down
35 changes: 35 additions & 0 deletions pkg/config_test.go
Original file line number Diff line number Diff line change
@@ -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) })
}

0 comments on commit 2ce9354

Please sign in to comment.