Skip to content

Commit

Permalink
Add version information in build and improve arg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
zMoooooritz committed Dec 22, 2023
1 parent 6b1e0a4 commit a81c6de
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
11 changes: 5 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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)
}
}
39 changes: 37 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -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)
}
23 changes: 2 additions & 21 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"flag"
"os"

"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit a81c6de

Please sign in to comment.