Config file TOML parsing with basic tests

This commit is contained in:
Imbus 2024-02-20 20:13:53 +01:00
parent c3457011b1
commit 973ab1db1d
4 changed files with 127 additions and 0 deletions

View file

@ -6,3 +6,5 @@ require (
github.com/jmoiron/sqlx v1.3.5 github.com/jmoiron/sqlx v1.3.5
github.com/mattn/go-sqlite3 v1.14.22 github.com/mattn/go-sqlite3 v1.14.22
) )
require github.com/BurntSushi/toml v1.3.2 // indirect

View file

@ -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/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=

View file

@ -0,0 +1,55 @@
package config
import (
"os"
"github.com/BurntSushi/toml"
)
// Config is the configuration for the application
// It defines how the TOML file should be structured
type Config struct {
// The port to listen on
Port int `toml:"port"`
// The path to the SQLite database
DbPath string `toml:"db_path"`
// The username to use for the database
DbUser string `toml:"db_user"`
// The password to use for the database
DbPass string `toml:"db_pass"`
// The name of the database
DbName string `toml:"db_name"`
}
// WriteConfigToFile writes a Config to a file
func WriteConfigToFile(c *Config, filename string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
return toml.NewEncoder(f).Encode(c)
}
// ReadConfigFromFile reads a Config from a file
func ReadConfigFromFile(filename string) (*Config, error) {
c := NewConfig()
_, err := toml.DecodeFile(filename, c)
if err != nil {
return nil, err
}
return c, nil
}
// NewConfig returns a new Config
func NewConfig() *Config {
return &Config{
Port: 8080,
DbPath: "./db.sqlite3",
DbUser: "username",
DbPass: "password",
DbName: "ttime",
}
}

View file

@ -0,0 +1,68 @@
package config
import (
"os"
"testing"
)
func TestNewConfig(t *testing.T) {
c := NewConfig()
if c.Port != 8080 {
t.Errorf("Expected port to be 8080, got %d", c.Port)
}
if c.DbPath != "./db.sqlite3" {
t.Errorf("Expected db path to be ./db.sqlite3, got %s", c.DbPath)
}
if c.DbUser != "username" {
t.Errorf("Expected db user to be username, got %s", c.DbUser)
}
if c.DbPass != "password" {
t.Errorf("Expected db pass to be password, got %s", c.DbPass)
}
if c.DbName != "ttime" {
t.Errorf("Expected db name to be ttime, got %s", c.DbName)
}
}
func TestWriteConfig(t *testing.T) {
c := NewConfig()
err := WriteConfigToFile(c, "test.toml")
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
// Remove the file after the test
_ = os.Remove("test.toml")
}
func TestReadConfig(t *testing.T) {
c := NewConfig()
err := WriteConfigToFile(c, "test.toml")
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
c2, err := ReadConfigFromFile("test.toml")
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
if c.Port != c2.Port {
t.Errorf("Expected port to be %d, got %d", c.Port, c2.Port)
}
if c.DbPath != c2.DbPath {
t.Errorf("Expected db path to be %s, got %s", c.DbPath, c2.DbPath)
}
if c.DbUser != c2.DbUser {
t.Errorf("Expected db user to be %s, got %s", c.DbUser, c2.DbUser)
}
if c.DbPass != c2.DbPass {
t.Errorf("Expected db pass to be %s, got %s", c.DbPass, c2.DbPass)
}
if c.DbName != c2.DbName {
t.Errorf("Expected db name to be %s, got %s", c.DbName, c2.DbName)
}
// Remove the file after the test
_ = os.Remove("test.toml")
}