Skip to content

Commit

Permalink
Separate flag and config init to fix --version. (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh authored Sep 26, 2024
1 parent 198068c commit e7df3a1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 6 additions & 4 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ var (
efs embed.FS
)

func initConfig(ko *koanf.Koanf) {
lo.Info("buildstring", "value", buildString)

func initFlags(ko *koanf.Koanf) {
// Command line flags.
f := flag.NewFlagSet("config", flag.ContinueOnError)
f.Usage = func() {
Expand All @@ -51,9 +49,13 @@ func initConfig(ko *koanf.Koanf) {

// Load commandline params.
ko.Load(posflag.Provider(f, ".", ko), nil)
}

func initConfig(ko *koanf.Koanf) {
lo.Info("buildstring", "value", buildString)

// Generate new config file.
if ok, _ := f.GetBool("new-config"); ok {
if ok := ko.Bool("new-config"); ok {
if err := generateConfig(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
7 changes: 5 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"fmt"
"log/slog"
"os"
"strings"
Expand All @@ -24,13 +25,15 @@ var (
)

func main() {
initConfig(ko)
initFlags(ko)

if ko.Bool("version") {
lo.Info("version", "value", buildString)
fmt.Println(buildString)
os.Exit(0)
}

initConfig(ko)

// Load environment variables and merge into the loaded config.
if err := ko.Load(env.Provider("DUNGBEETLE_", ".", func(s string) string {
return strings.Replace(strings.ToLower(strings.TrimPrefix(s, "DUNGBEETLE_")), "__", ".", -1)
Expand Down

0 comments on commit e7df3a1

Please sign in to comment.