From 9eea48e89de63ddb384d9b7b59c78c63171cb51f Mon Sep 17 00:00:00 2001 From: matheusgomes28 Date: Tue, 20 Feb 2024 12:00:25 +0000 Subject: [PATCH] Adding a simple toml parser and cmd-line parser --- cmd/urchin/main.go | 53 ++++++++++++++++++++++++++++++++++++++---- common/app_settings.go | 24 ++++++++++++++----- go.mod | 1 + go.sum | 2 ++ 4 files changed, 70 insertions(+), 10 deletions(-) diff --git a/cmd/urchin/main.go b/cmd/urchin/main.go index 5335563..41cc755 100644 --- a/cmd/urchin/main.go +++ b/cmd/urchin/main.go @@ -1,6 +1,8 @@ package main import ( + "os" + _ "github.com/go-sql-driver/mysql" "github.com/matheusgomes28/urchin/app" "github.com/matheusgomes28/urchin/common" @@ -8,12 +10,53 @@ import ( "github.com/rs/zerolog/log" ) +type programArgs struct { + config string +} + +func readArguments(args []string) programArgs { + + var program_args programArgs + arg_index := 0 + + for ; arg_index < len(args); arg_index++ { + arg := args[arg_index] + arg_val := "" + if arg == "--config" { + if argval_index := arg_index + 1; argval_index < len(args) { + arg_val = args[argval_index] + arg_index++ + } + program_args.config = arg_val + } + arg_index++ + } + + return program_args +} + func main() { /// Load global application settings - app_settings, err := common.LoadSettings() - if err != nil { - log.Error().Msgf("could not get app settings: %v\n", err) - return + + var app_settings common.AppSettings + arguments := readArguments(os.Args) + if arguments.config != "" { + log.Info().Msgf("reading config file %s", arguments.config) + settings, err := common.ReadConfigJson(arguments.config) + if err != nil { + log.Error().Msgf("could not read config file: %v", err) + os.Exit(-1) + } + + app_settings = settings + } else { + log.Info().Msgf("no config file, reading environment variables") + settings, err := common.LoadSettings() + if err != nil { + log.Error().Msgf("could not load settings: %v", err) + os.Exit(-1) + } + app_settings = settings } db_connection, err := database.MakeSqlConnection( @@ -25,9 +68,11 @@ func main() { ) if err != nil { log.Error().Msgf("could not create database connection: %v", err) + os.Exit(-1) } if err = app.Run(app_settings, &db_connection); err != nil { log.Error().Msgf("could not run app: %v", err) + os.Exit(-1) } } diff --git a/common/app_settings.go b/common/app_settings.go index 4f2b767..7c29ac8 100644 --- a/common/app_settings.go +++ b/common/app_settings.go @@ -4,15 +4,17 @@ import ( "fmt" "os" "strconv" + + "github.com/BurntSushi/toml" ) type AppSettings struct { - DatabaseAddress string - DatabasePort int - DatabaseUser string - DatabasePassword string - DatabaseName string - WebserverPort string + DatabaseAddress string `toml:"database_address"` + DatabasePort int `toml:"database_port"` + DatabaseUser string `toml:"database_user"` + DatabasePassword string `toml:"database_password"` + DatabaseName string `toml:"database_name"` + WebserverPort string `toml:"webserver_port"` } func LoadSettings() (AppSettings, error) { @@ -61,3 +63,13 @@ func LoadSettings() (AppSettings, error) { WebserverPort: webserver_port, }, nil } + +func ReadConfigJson(filepath string) (AppSettings, error) { + var config AppSettings + _, err := toml.DecodeFile(filepath, &config) + if err != nil { + return AppSettings{}, err + } + + return config, nil +} diff --git a/go.mod b/go.mod index 5ad8dd5..b880266 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( ) require ( + github.com/BurntSushi/toml v1.3.2 // indirect github.com/bytedance/sonic v1.9.1 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect diff --git a/go.sum b/go.sum index 04e3ac7..a66ad19 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= +github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/a-h/templ v0.2.543 h1:8YyLvyUtf0/IE2nIwZ62Z/m2o2NqwhnMynzOL78Lzbk= github.com/a-h/templ v0.2.543/go.mod h1:jP908DQCwI08IrnTalhzSEH9WJqG/Q94+EODQcJGFUA= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=