Skip to content

Commit

Permalink
Minor formatting refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Mar 13, 2024
1 parent 161e7a0 commit 781049d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 75 deletions.
68 changes: 66 additions & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import (
"net/http"
"os"

"github.com/go-chi/chi/v5"
bredis "github.com/kalbhor/tasqueue/v2/brokers/redis"
rredis "github.com/kalbhor/tasqueue/v2/results/redis"

"github.com/go-chi/chi/v5"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/posflag"
"github.com/knadh/koanf/v2"
flag "github.com/spf13/pflag"
"github.com/zerodha/dungbeetle/internal/core"
"github.com/zerodha/dungbeetle/internal/dbpool"
"github.com/zerodha/dungbeetle/internal/resultbackends/sqldb"
Expand All @@ -24,6 +27,67 @@ var (
efs embed.FS
)

func initConfig() {
lo.Info("buildstring", "value", buildString)

// Command line flags.
f := flag.NewFlagSet("config", flag.ContinueOnError)
f.Usage = func() {
lo.Info("DungBeetle")
lo.Info(f.FlagUsages())
os.Exit(0)
}

f.Bool("new-config", false, "generate a new sample config.toml file.")
f.String("config", "config.toml", "path to the TOML configuration file")
f.String("server", "127.0.0.1:6060", "web server address to bind on")
f.StringSlice("sql-directory", []string{"./sql"}, "path to directory with .sql scripts. Can be specified multiple times")
f.String("queue", "default", "name of the job queue to accept jobs from")
f.String("worker-name", "default", "name of this worker instance")
f.Int("worker-concurrency", 10, "number of concurrent worker threads to run")
f.Bool("worker-only", false, "don't start the web server and run in worker-only mode")
f.Bool("version", false, "show current version and build")
f.Parse(os.Args[1:])

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

// Generate new config file.
if ok, _ := f.GetBool("new-config"); ok {
if err := generateConfig(); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("config.toml generated. Edit and run --install.")
os.Exit(0)
}

// Load the config file.
if err := ko.Load(file.Provider(ko.String("config")), toml.Parser()); err != nil {
slog.Error("error reading config", "error", err)
return
}

var (
level = ko.MustString("app.log_level")
opts = &slog.HandlerOptions{}
)
switch level {
case "DEBUG":
opts.Level = slog.LevelDebug
case "INFO":
opts.Level = slog.LevelInfo
case "ERROR":
opts.Level = slog.LevelError
default:
lo.Error("incorrect log level in app")
os.Exit(1)
}

// Override the logger according to level
lo = slog.New(slog.NewTextHandler(os.Stdout, opts))
}

func generateConfig() error {
if _, err := os.Stat("config.toml"); !os.IsNotExist(err) {
return errors.New("config.toml exists. Remove it to generate a new one")
Expand Down
76 changes: 3 additions & 73 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@ package main

import (
"context"
"fmt"
"log/slog"
"os"
"strings"

flag "github.com/spf13/pflag"

"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/posflag"
"github.com/knadh/koanf/v2"

// Clickhouse, MySQL and Postgres drivers.
Expand All @@ -29,78 +23,15 @@ var (
ko = koanf.New(".")
)

func init() {
lo.Info("buildstring", "value", buildString)

// Command line flags.
f := flag.NewFlagSet("config", flag.ContinueOnError)
f.Usage = func() {
lo.Info("DungBeetle")
lo.Info(f.FlagUsages())
os.Exit(0)
}

f.Bool("new-config", false, "generate a new sample config.toml file.")
f.String("config", "config.toml", "path to the TOML configuration file")
f.String("server", "127.0.0.1:6060", "web server address to bind on")
f.StringSlice("sql-directory", []string{"./sql"}, "path to directory with .sql scripts. Can be specified multiple times")
f.String("queue", "default", "name of the job queue to accept jobs from")
f.String("worker-name", "default", "name of this worker instance")
f.Int("worker-concurrency", 10, "number of concurrent worker threads to run")
f.Bool("worker-only", false, "don't start the web server and run in worker-only mode")
f.Bool("version", false, "show current version and build")
f.Parse(os.Args[1:])

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

// Generate new config file.
if ok, _ := f.GetBool("new-config"); ok {
if err := generateConfig(); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("config.toml generated. Edit and run --install.")
os.Exit(0)
}

// Load the config file.
if err := ko.Load(file.Provider(ko.String("config")), toml.Parser()); err != nil {
slog.Error("error reading config", "error", err)
return
}

var (
level = ko.MustString("app.log_level")
opts = &slog.HandlerOptions{}
)
switch level {
case "DEBUG":
opts.Level = slog.LevelDebug
case "INFO":
opts.Level = slog.LevelInfo
case "ERROR":
opts.Level = slog.LevelError
default:
lo.Error("incorrect log level in app")
os.Exit(1)
}

// Override the logger according to level
lo = slog.New(slog.NewTextHandler(os.Stdout, opts))
}

func main() {
// Display version.
if ko.Bool("version") {
lo.Info("version", "value", buildString)
os.Exit(0)
}

// Load environment variables and merge into the loaded config.
if err := ko.Load(env.Provider("DUNG_BEETLE", ".", func(s string) string {
return strings.Replace(
strings.ToLower(strings.TrimPrefix(s, "DUNG_BEETLE")), "__", ".", -1)
if err := ko.Load(env.Provider("DUNGBEETLE_", ".", func(s string) string {
return strings.Replace(strings.ToLower(strings.TrimPrefix(s, "DUNGBEETLE_")), "__", ".", -1)
}), nil); err != nil {
lo.Error("error loading config from env", "error", err)
return
Expand All @@ -111,8 +42,7 @@ func main() {
mode = "worker only"
}

lo.Info("starting server", "queue", ko.MustString("queue"), "mode", mode,
"worker-name", ko.MustString("worker-name"))
lo.Info("starting server", "queue", ko.MustString("queue"), "mode", mode, "worker-name", ko.MustString("worker-name"))

// Initialize the core.
co, err := initCore(ko)
Expand Down

0 comments on commit 781049d

Please sign in to comment.