From a81c6de597eb88dd4f16440a522bcd04d7af67f3 Mon Sep 17 00:00:00 2001 From: Moritz Biering Date: Fri, 22 Dec 2023 23:03:24 +0100 Subject: [PATCH] Add version information in build and improve arg parsing --- cmd/main.go | 11 +++++------ main.go | 39 +++++++++++++++++++++++++++++++++++++-- pkg/config/config.go | 23 ++--------------------- 3 files changed, 44 insertions(+), 29 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 55aac23..e8a458c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -11,12 +11,11 @@ import ( "github.com/zMoooooritz/nachrichten/pkg/util" ) -func Run() { +func Run(configFile string, logFile string) { - cfg := config.Init() - configuration := cfg.Load() - if cfg.LogFile != "" { - err := util.SetLogFile(cfg.LogFile) + configuration := config.Load(configFile) + if logFile != "" { + err := util.SetLogFile(logFile) log.Fatalln("Error occoured while setting up logger: ", err) } util.Logger.Println("Application started.") @@ -26,7 +25,7 @@ func Run() { // tea.WithMouseCellMotion(), ) if _, err := p.Run(); err != nil { - fmt.Printf("Alas, there's been an error: %v", err) + fmt.Printf("There's been an error: %v", err) os.Exit(1) } } diff --git a/main.go b/main.go index d5ceb9d..2e853b6 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,42 @@ package main -import "github.com/zMoooooritz/nachrichten/cmd" +import ( + "flag" + "fmt" + "os" + + "github.com/zMoooooritz/nachrichten/cmd" +) + +var ( + // Set via ldflags when building + Version = "" + CommitSHA = "" + + configFile = flag.String("config", "", "Path to configuration file") + logFile = flag.String("debug", "", "Path to log file") + version = flag.Bool("version", false, "Display version") +) func main() { - cmd.Run() + flag.Parse() + + if *version { + if len(CommitSHA) > 7 { + CommitSHA = CommitSHA[:7] + } + if Version == "" { + Version = "(built from source)" + } + + fmt.Printf("nachrichten %s", Version) + if len(CommitSHA) > 0 { + fmt.Printf(" (%s)", CommitSHA) + } + + fmt.Println() + os.Exit(0) + } + + cmd.Run(*configFile, *logFile) } diff --git a/pkg/config/config.go b/pkg/config/config.go index e7b1f97..3e1f507 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,7 +1,6 @@ package config import ( - "flag" "os" "gopkg.in/yaml.v3" @@ -50,28 +49,10 @@ const ( TypeHTML ) -type Config struct { - ConfigFile string - LogFile string -} - -func Init() Config { - - configFile := flag.String("config", "", "Path to configuration file") - logFile := flag.String("debug", "", "Path to log file") - - flag.Parse() - cfg := Config{ - ConfigFile: *configFile, - LogFile: *logFile, - } - return cfg -} - -func (c Config) Load() Configuration { +func Load(configFile string) Configuration { config := defaultConfiguration() - data, err := os.ReadFile(c.ConfigFile) + data, err := os.ReadFile(configFile) if err != nil { return config }