5 Commits

Author SHA1 Message Date
5d9924ff58 replace exitOnError with a deferred exit function.
this ensures the closer() cleanup function is always called.
2026-02-15 11:08:25 +00:00
00acafa290 fix docstring 2026-02-15 10:51:59 +00:00
f97d241f64 add linter exclusion for test files
set thresholds for issues
2026-02-15 09:54:12 +00:00
87ea26ab03 build-{OS} targets internal 2026-02-14 21:25:39 +00:00
331d4e2f9a add macos target to Taskfile+makefile 2026-02-14 21:18:31 +00:00
4 changed files with 66 additions and 20 deletions

View File

@@ -67,6 +67,7 @@ linters:
- name: unused-parameter
- name: var-declaration
- name: blank-imports
- name: range
# Disabled rules (can be enabled if needed)
# - name: line-length-limit
@@ -81,6 +82,17 @@ linters:
- G104 # Duplicated errcheck checks
- G115 # integer overflow conversion int -> uint32
exclusions:
warn-unused: false
rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- gocyclo
- errcheck
- dupl
- gosec
# Formatters configuration
formatters:
# Enable specific formatters
@@ -106,5 +118,8 @@ formatters:
extra-rules: true # Enable additional formatting rules
issues:
max-same-issues: 0
max-issues-per-linter: 0
# Limit the number of same issues reported to avoid spam
max-same-issues: 50
# Limit the number of issues per linter to keep output manageable
max-issues-per-linter: 100

View File

@@ -9,6 +9,7 @@ vars:
WINDOWS: '{{.BIN_DIR}}/{{.PROGRAM}}_windows_amd64.exe'
LINUX: '{{.BIN_DIR}}/{{.PROGRAM}}_linux_amd64'
MACOS: '{{.BIN_DIR}}/{{.PROGRAM}}_darwin_amd64'
tasks:
default:
@@ -22,7 +23,7 @@ tasks:
cmds:
- task: build-windows
- task: build-linux
- task: build-macos
vet:
desc: Vet the code
deps: [fmt]
@@ -38,11 +39,19 @@ tasks:
desc: Build the vbantxt project for Windows
cmds:
- GOOS=windows GOARCH=amd64 go build -o {{.WINDOWS}} -ldflags="-X main.version={{.VERSION}}" ./cmd/{{.PROGRAM}}/
internal: true
build-linux:
desc: Build the vbantxt project for Linux
cmds:
- GOOS=linux GOARCH=amd64 go build -o {{.LINUX}} -ldflags="-X main.version={{.VERSION}}" ./cmd/{{.PROGRAM}}/
internal: true
build-macos:
desc: Build the vbantxt project for macOS
cmds:
- GOOS=darwin GOARCH=amd64 go build -o {{.MACOS}} -ldflags="-X main.version={{.VERSION}}" ./cmd/{{.PROGRAM}}/
internal: true
test:
desc: Run tests

View File

@@ -47,12 +47,28 @@ func (f *Flags) String() string {
)
}
func exitOnError(err error) {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
func main() {
var exitCode int
// Defer exit with the final exit code
defer func() {
if exitCode != 0 {
os.Exit(exitCode)
}
}()
closer, err := run()
if closer != nil {
defer closer()
}
if err != nil {
log.Error(err)
exitCode = 1
}
}
func main() {
// run contains the main application logic and returns a closer function and any error.
func run() (func(), error) {
var flags Flags
// VBAN specific flags
@@ -66,7 +82,7 @@ func main() {
configDir, err := os.UserConfigDir()
if err != nil {
exitOnError(fmt.Errorf("failed to get user config directory: %w", err))
return nil, fmt.Errorf("failed to get user config directory: %w", err)
}
defaultConfigPath := filepath.Join(configDir, "vbantxt", "config.toml")
@@ -98,7 +114,7 @@ func main() {
fmt.Fprintf(os.Stderr, "%s\n", ffhelp.Flags(fs, "vbantxt [flags] <vban commands>"))
os.Exit(0)
case err != nil:
exitOnError(fmt.Errorf("failed to parse flags: %w", err))
return nil, fmt.Errorf("failed to parse flags: %w", err)
}
if flags.Version {
@@ -108,7 +124,7 @@ func main() {
level, err := log.ParseLevel(flags.Loglevel)
if err != nil {
exitOnError(fmt.Errorf("invalid log level: %s", flags.Loglevel))
return nil, fmt.Errorf("invalid log level %q", flags.Loglevel)
}
log.SetLevel(level)
@@ -116,16 +132,18 @@ func main() {
client, closer, err := createClient(&flags)
if err != nil {
exitOnError(err)
return nil, fmt.Errorf("failed to create VBAN client: %w", err)
}
defer closer()
commands := fs.GetArgs()
if len(commands) == 0 {
exitOnError(errors.New("no VBAN commands provided"))
return closer, errors.New(
"no VBAN commands provided; please provide at least one command as an argument",
)
}
sendCommands(client, commands)
return closer, nil
}
// versionFromBuild retrieves the version information from the build metadata.
@@ -133,7 +151,7 @@ func versionFromBuild() string {
if version == "" {
info, ok := debug.ReadBuildInfo()
if !ok {
exitOnError(errors.New("failed to read build info"))
return "(unable to read build info)"
}
version = strings.Split(info.Main.Version, "-")[0]
}
@@ -162,13 +180,11 @@ func createClient(flags *Flags) (*vbantxt.VbanTxt, func(), error) {
return client, closer, err
}
// sendCommands sends a list of commands to the VBAN client.
// sendCommands sends the provided VBAN commands using the client and logs any errors that occur.
func sendCommands(client *vbantxt.VbanTxt, commands []string) {
for _, cmd := range commands {
err := client.Send(cmd)
if err != nil {
if err := client.Send(cmd); err != nil {
log.Errorf("Failed to send command '%s': %v", cmd, err)
continue
}
}
}

View File

@@ -5,24 +5,27 @@ BIN_DIR := bin
WINDOWS=$(BIN_DIR)/$(PROGRAM)_windows_amd64.exe
LINUX=$(BIN_DIR)/$(PROGRAM)_linux_amd64
MACOS=$(BIN_DIR)/$(PROGRAM)_darwin_amd64
VERSION=$(shell git describe --tags $(shell git rev-list --tags --max-count=1))
.DEFAULT_GOAL := build
.PHONY: fmt vet build windows linux test clean
.PHONY: fmt vet build windows linux macos test clean
fmt:
$(GO) fmt ./...
vet: fmt
$(GO) vet ./...
build: vet windows linux | $(BIN_DIR)
build: vet windows linux macos | $(BIN_DIR)
@echo version: $(VERSION)
windows: $(WINDOWS)
linux: $(LINUX)
macos: $(MACOS)
$(WINDOWS):
env GOOS=windows GOARCH=amd64 go build -v -o $(WINDOWS) -ldflags="-s -w -X main.version=$(VERSION)" ./cmd/$(PROGRAM)/
@@ -30,6 +33,9 @@ $(WINDOWS):
$(LINUX):
env GOOS=linux GOARCH=amd64 go build -v -o $(LINUX) -ldflags="-s -w -X main.version=$(VERSION)" ./cmd/$(PROGRAM)/
$(MACOS):
env GOOS=darwin GOARCH=amd64 go build -v -o $(MACOS) -ldflags="-s -w -X main.version=$(VERSION)" ./cmd/$(PROGRAM)/
test:
$(GO) test ./...