Skip to content

Commit

Permalink
Provide "--version" command line parameter, keep version information.
Browse files Browse the repository at this point in the history
  • Loading branch information
haimgel committed Oct 2, 2022
1 parent 060994e commit 4081a26
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
7 changes: 6 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ builds:
goarch:
- amd64
- arm64
main: ./cmd/main.go
mod_timestamp: '{{ .CommitTimestamp }}'
flags:
- -trimpath
ldflags:
# Disables debug symbols (-s, -w), passes version information
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{ .CommitDate }}
archives:
- replacements:
darwin: Darwin
Expand Down
6 changes: 3 additions & 3 deletions cmd/main.go → cmd/app.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"github.com/haimgel/mqtt2cmd/internal/config"
Expand All @@ -8,8 +8,8 @@ import (
"time"
)

func main() {
appConfig, err := config.Load()
func Execute(version string, exit func(int), args []string) {
appConfig, err := config.Load(version, exit, args)
if err != nil {
panic(err)
}
Expand Down
19 changes: 12 additions & 7 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ type ApplicationConfig struct {
}

// Load configuration from command-line options, config file, and environment variables
func Load() (*ApplicationConfig, error) {
processCommandLineArguments()
func Load(version string, exit func(int), args []string) (*ApplicationConfig, error) {
processCommandLineArguments(version, exit, args)

err := viper.BindPFlags(pflag.CommandLine)
if err != nil {
Expand Down Expand Up @@ -61,14 +61,19 @@ func Load() (*ApplicationConfig, error) {
return &config, nil
}

func processCommandLineArguments() {
func processCommandLineArguments(versionStr string, exit func(int), args []string) {
pflag.StringP("mqtt.broker", "b", "", "MQTT broker (example \"tcp://hostname:1883\")")
pflag.StringP("log.path", "l", defaultLogFile(), "Log file path")
help := pflag.BoolP("help", "h", false, "")
pflag.Parse()
if *help {
helpFlag := pflag.BoolP("help", "h", false, "This help message")
versionFlag := pflag.BoolP("version", "v", false, "Show version")
_ = pflag.CommandLine.Parse(args)
if *helpFlag {
pflag.Usage()
os.Exit(2)
exit(2)
}
if *versionFlag {
fmt.Printf("%s version %s\n", AppName, versionStr)
exit(0)
}
}

Expand Down
35 changes: 35 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"github.com/haimgel/mqtt2cmd/cmd"
"os"
"runtime"
)

// nolint: gochecknoglobals
var (
version = "dev"
commit = ""
date = ""
)

func buildVersion(version, commit, date string) string {
result := version
if commit != "" {
result = fmt.Sprintf("%s\ncommit: %s", result, commit)
}
if date != "" {
result = fmt.Sprintf("%s\nbuilt at: %s", result, date)
}
result = fmt.Sprintf("%s\ngoos: %s\ngoarch: %s", result, runtime.GOOS, runtime.GOARCH)
return result
}

func main() {
cmd.Execute(
buildVersion(version, commit, date),
os.Exit,
os.Args[1:],
)
}

0 comments on commit 4081a26

Please sign in to comment.