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 24, 2024
1 parent 082e970 commit 7dde715
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ tmp_dir = "tmp"
cmd = "make build"
args_bin = []
bin = "./tmp/urchin"
full_bin = "URCHIN_WEBSERVER_PORT=8080 URCHIN_DATABASE_ADDRESS=mariadb URCHIN_DATABASE_PORT=3306 URCHIN_DATABASE_USER=root URCHIN_DATABASE_PASSWORD=root URCHIN_DATABASE_NAME=urchin ./tmp/urchin"
full_bin = "./tmp/urchin --config urchin_config.toml"
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
Expand Down
27 changes: 22 additions & 5 deletions cmd/urchin/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"fmt"
"os"

Expand All @@ -12,11 +13,27 @@ import (
)

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)
os.Exit(-1)
config_toml := flag.String("config", "", "path to the config to be used")
flag.Parse()

var app_settings common.AppSettings
if (*config_toml) != "" {
log.Info().Msgf("reading config file %s", *config_toml)
settings, err := common.ReadConfigToml(*config_toml)
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 Down
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 int
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 int `toml:"webserver_port"`
}

func LoadSettings() (AppSettings, error) {
Expand Down Expand Up @@ -66,3 +68,13 @@ func LoadSettings() (AppSettings, error) {
WebserverPort: webserver_port,
}, nil
}

func ReadConfigToml(filepath string) (AppSettings, error) {
var config AppSettings
_, err := toml.DecodeFile(filepath, &config)
if err != nil {
return AppSettings{}, err
}

return config, nil
}
254 changes: 254 additions & 0 deletions common/app_settings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
package common

import (
"errors"
"os"
"testing"

"github.com/pelletier/go-toml/v2"
"github.com/stretchr/testify/assert"
)

// Writes the contents into a temporary
// toml file
func writeToml(contents []byte) (s string, err error) {
file, err := os.CreateTemp(os.TempDir(), "*.toml")
if err != nil {
return "", err
}
defer func() {
err = errors.Join(file.Close(), err)
}()

_, err = file.Write(contents)
if err != nil {
return "", err
}
return file.Name(), nil
}

func TestCorrectToml(t *testing.T) {
expected := AppSettings{
DatabaseAddress: "test_database_address",
DatabaseUser: "test_database_user",
DatabasePassword: "test_database_password",
DatabaseName: "test_database_name",
WebserverPort: 99999,
DatabasePort: 666,
}
bytes, err := toml.Marshal(expected)
assert.Nil(t, err)

filepath, err := writeToml(bytes)
assert.Nil(t, err)

actual, err := ReadConfigToml(filepath)
assert.Nil(t, err)
assert.Equal(t, actual, expected)
}

func TestMissingDatabaseAddress(t *testing.T) {

missing_database_address := struct {
DatabaseUser string `toml:"database_user"`
DatabasePassword string `toml:"database_password"`
DatabaseName string `toml:"database_name"`
WebserverPort string `toml:"webserver_port"`
DatabasePort string `toml:"database_port"`
}{
DatabaseUser: "test_database_user",
DatabasePassword: "test_database_password",
DatabaseName: "test_database_name",
WebserverPort: "99999",
DatabasePort: "666",
}

bytes, err := toml.Marshal(missing_database_address)
assert.Nil(t, err)

filepath, err := writeToml(bytes)
assert.Nil(t, err)

_, err = ReadConfigToml(filepath)
assert.NotNil(t, err)
}

func TestMissingDatabaseUser(t *testing.T) {

missing_database_user := struct {
DatabaseAddress string `toml:"database_address"`
DatabasePassword string `toml:"database_password"`
DatabaseName string `toml:"database_name"`
WebserverPort string `toml:"webserver_port"`
DatabasePort string `toml:"database_port"`
}{
DatabaseAddress: "test_database_address",
DatabasePassword: "test_database_password",
DatabaseName: "test_database_name",
WebserverPort: "99999",
DatabasePort: "666",
}

bytes, err := toml.Marshal(missing_database_user)
assert.Nil(t, err)

filepath, err := writeToml(bytes)
assert.Nil(t, err)

_, err = ReadConfigToml(filepath)
assert.NotNil(t, err)
}

func TestMissingDatabasePassword(t *testing.T) {
missing_database_password := struct {
DatabaseAddress string `toml:"database_address"`
DatabaseUser string `toml:"database_user"`
DatabaseName string `toml:"database_name"`
WebserverPort string `toml:"webserver_port"`
DatabasePort string `toml:"database_port"`
}{
DatabaseAddress: "test_database_address",
DatabaseUser: "test_database_user",
DatabaseName: "test_database_name",
WebserverPort: "99999",
DatabasePort: "666",
}

bytes, err := toml.Marshal(missing_database_password)
assert.Nil(t, err)

filepath, err := writeToml(bytes)
assert.Nil(t, err)

_, err = ReadConfigToml(filepath)
assert.NotNil(t, err)
}

func TestMissingDatabaseName(t *testing.T) {
missing_database_name := struct {
DatabaseAddress string `toml:"database_address"`
DatabaseUser string `toml:"database_user"`
DatabasePassword string `toml:"database_password"`
WebserverPort string `toml:"webserver_port"`
DatabasePort string `toml:"database_port"`
}{
DatabaseAddress: "test_database_address",
DatabaseUser: "test_database_user",
DatabasePassword: "test_database_password",
WebserverPort: "99999",
DatabasePort: "666",
}

bytes, err := toml.Marshal(missing_database_name)
assert.Nil(t, err)

filepath, err := writeToml(bytes)
assert.Nil(t, err)

_, err = ReadConfigToml(filepath)
assert.NotNil(t, err)
}

func TestMissingWebserverPort(t *testing.T) {
missing_webserver_port := struct {
DatabaseAddress string `toml:"database_address"`
DatabaseUser string `toml:"database_user"`
DatabasePassword string `toml:"database_password"`
DatabaseName string `toml:"database_name"`
DatabasePort string `toml:"database_port"`
}{
DatabaseAddress: "test_database_address",
DatabaseUser: "test_database_user",
DatabasePassword: "test_database_password",
DatabaseName: "test_database_name",
DatabasePort: "666",
}

bytes, err := toml.Marshal(missing_webserver_port)
assert.Nil(t, err)

filepath, err := writeToml(bytes)
assert.Nil(t, err)

_, err = ReadConfigToml(filepath)
assert.NotNil(t, err)
}

func TestMissingDatabasePort(t *testing.T) {
missing_database_address := struct {
DatabaseAddress string `toml:"database_address"`
DatabaseUser string `toml:"database_user"`
DatabasePassword string `toml:"database_password"`
DatabaseName string `toml:"database_name"`
WebserverPort string `toml:"webserver_port"`
}{
DatabaseAddress: "test_database_address",
DatabaseUser: "test_database_user",
DatabasePassword: "test_database_password",
DatabaseName: "test_database_name",
WebserverPort: "99999",
}

bytes, err := toml.Marshal(missing_database_address)
assert.Nil(t, err)

filepath, err := writeToml(bytes)
assert.Nil(t, err)

_, err = ReadConfigToml(filepath)
assert.NotNil(t, err)
}

func TestWrongDatabasePortValueType(t *testing.T) {
missing_database_address := struct {
DatabaseAddress string `toml:"database_address"`
DatabaseUser string `toml:"database_user"`
DatabasePassword string `toml:"database_password"`
DatabaseName string `toml:"database_name"`
DatabasePort string `toml:"database_port"`
WebserverPort int `toml:"webserver_port"`
}{
DatabaseAddress: "test_database_address",
DatabaseUser: "test_database_user",
DatabasePassword: "test_database_password",
DatabaseName: "test_database_name",
DatabasePort: "String Should Not Work",
WebserverPort: 99999,
}

bytes, err := toml.Marshal(missing_database_address)
assert.Nil(t, err)

filepath, err := writeToml(bytes)
assert.Nil(t, err)

_, err = ReadConfigToml(filepath)
assert.NotNil(t, err)
}

func TestWrongwebserverPortValueType(t *testing.T) {
missing_database_address := struct {
DatabaseAddress string `toml:"database_address"`
DatabaseUser string `toml:"database_user"`
DatabasePassword string `toml:"database_password"`
DatabaseName string `toml:"database_name"`
DatabasePort int `toml:"database_port"`
WebserverPort string `toml:"webserver_port"`
}{
DatabaseAddress: "test_database_address",
DatabaseUser: "test_database_user",
DatabasePassword: "test_database_password",
DatabaseName: "test_database_name",
DatabasePort: 10,
WebserverPort: "99999",
}

bytes, err := toml.Marshal(missing_database_address)
assert.Nil(t, err)

filepath, err := writeToml(bytes)
assert.Nil(t, err)

_, err = ReadConfigToml(filepath)
assert.NotNil(t, err)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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/davecgh/go-spew v1.1.1 // 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
18 changes: 18 additions & 0 deletions urchin_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Address to the MariaDB database
database_address = "localhost"

# User to access datbaase
database_user = "root"

# Password for the database user
database_password = "root"

# port
database_port = 3006

# name of database where urchin's
# migrations was installed
database_name = "urchin"

# port to run the webserver on
webserver_port = 8080

0 comments on commit 7dde715

Please sign in to comment.