Skip to content

Commit

Permalink
Adding a simple toml parser and cmd-line parser
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusgomes28 committed Feb 20, 2024
1 parent 805ed95 commit 9eea48e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
53 changes: 49 additions & 4 deletions cmd/urchin/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,62 @@
package main

import (
"os"

_ "github.com/go-sql-driver/mysql"
"github.com/matheusgomes28/urchin/app"
"github.com/matheusgomes28/urchin/common"
"github.com/matheusgomes28/urchin/database"
"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(
Expand All @@ -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)
}
}
24 changes: 18 additions & 6 deletions common/app_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down

0 comments on commit 9eea48e

Please sign in to comment.