✨ Load config from TOML
This commit is contained in:
		
					parent
					
						
							
								e7b88bcc09
							
						
					
				
			
			
				commit
				
					
						9ddaa98ff4
					
				
			
		
					 5 changed files with 47 additions and 7 deletions
				
			
		|  | @ -1,16 +1,16 @@ | ||||||
| package config | package config | ||||||
| 
 | 
 | ||||||
| type Config struct { | type Config struct { | ||||||
| 	Name    string | 	Name    string       `toml:"name"` | ||||||
| 	Env     Env | 	Env     Env          `toml:"env"` | ||||||
| 	BaseURL string | 	BaseURL string       `toml:"base_url"` | ||||||
| 	Conn    ConnSettings | 	Conn    ConnSettings `toml:"conn"` | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type ConnSettings struct { | type ConnSettings struct { | ||||||
| 	Store              string | 	Store    string         `toml:"store"` | ||||||
| 	DSN                string | 	DSN      string         `toml:"dsn"` | ||||||
| 	AdditionalSettings map[string]any | 	Settings map[string]any `toml:"settings"` | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (c Config) Environment() Env { | func (c Config) Environment() Env { | ||||||
|  |  | ||||||
							
								
								
									
										8
									
								
								config/load.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								config/load.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,8 @@ | ||||||
|  | package config | ||||||
|  | 
 | ||||||
|  | import "github.com/BurntSushi/toml" | ||||||
|  | 
 | ||||||
|  | func LoadFromToml(path string) (c Config, err error) { | ||||||
|  | 	_, err = toml.DecodeFile(path, &c) | ||||||
|  | 	return | ||||||
|  | } | ||||||
							
								
								
									
										29
									
								
								config/load_test.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								config/load_test.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,29 @@ | ||||||
|  | package config | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"os" | ||||||
|  | 	"testing" | ||||||
|  | 
 | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func TestLoadTomlMissing(t *testing.T) { | ||||||
|  | 	_, e := LoadFromToml("not-a-real-filee") | ||||||
|  | 	assert.Error(t, e) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestLoadTomlGood(t *testing.T) { | ||||||
|  | 	tmp, _ := os.CreateTemp("", "*.toml") | ||||||
|  | 	defer tmp.Close() | ||||||
|  | 	fmt.Fprintln(tmp, `name = "Cool"`) | ||||||
|  | 	fmt.Fprintln(tmp, "[conn]") | ||||||
|  | 	fmt.Fprintln(tmp, `store = "sqlite"`) | ||||||
|  | 	fmt.Fprintln(tmp, "[conn.settings]") | ||||||
|  | 	fmt.Fprintln(tmp, `num = 42`) | ||||||
|  | 	c, e := LoadFromToml(tmp.Name()) | ||||||
|  | 	assert.NoError(t, e) | ||||||
|  | 	assert.Equal(t, "Cool", c.Name) | ||||||
|  | 	assert.Equal(t, "sqlite", c.Conn.Store) | ||||||
|  | 	assert.Equal(t, int64(42), c.Conn.Settings["num"]) | ||||||
|  | } | ||||||
							
								
								
									
										1
									
								
								go.mod
									
										
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
										
									
									
									
								
							|  | @ -3,6 +3,7 @@ module codeberg.org/danjones000/lenore | ||||||
| go 1.23.1 | go 1.23.1 | ||||||
| 
 | 
 | ||||||
| require ( | require ( | ||||||
|  | 	github.com/BurntSushi/toml v1.4.0 | ||||||
| 	github.com/go-ap/activitypub v0.0.0-20240910141749-b4b8c8aa484c | 	github.com/go-ap/activitypub v0.0.0-20240910141749-b4b8c8aa484c | ||||||
| 	github.com/go-ap/client v0.0.0-20240910141951-13a4f3c4fd53 | 	github.com/go-ap/client v0.0.0-20240910141951-13a4f3c4fd53 | ||||||
| 	github.com/go-ap/fedbox v0.0.0-20240910163620-7bcedb2eb399 | 	github.com/go-ap/fedbox v0.0.0-20240910163620-7bcedb2eb399 | ||||||
|  |  | ||||||
							
								
								
									
										2
									
								
								go.sum
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								go.sum
									
										
									
									
									
								
							|  | @ -7,6 +7,8 @@ git.sr.ht/~mariusor/lw v0.0.0-20240906100438-00d2184b2120 h1:OLxL9lel79BV3EHu/AM | ||||||
| git.sr.ht/~mariusor/lw v0.0.0-20240906100438-00d2184b2120/go.mod h1:kXJ4JsgGBu7IVBKlrVvGjSLJmpsAGqZwq/JU/kTUaLw= | git.sr.ht/~mariusor/lw v0.0.0-20240906100438-00d2184b2120/go.mod h1:kXJ4JsgGBu7IVBKlrVvGjSLJmpsAGqZwq/JU/kTUaLw= | ||||||
| git.sr.ht/~mariusor/ssm v0.0.0-20240811085540-34f24cac52b7 h1:bCGvett+MiEDc5L+T9jByp671KnKRG/iJCCm0fc+21s= | git.sr.ht/~mariusor/ssm v0.0.0-20240811085540-34f24cac52b7 h1:bCGvett+MiEDc5L+T9jByp671KnKRG/iJCCm0fc+21s= | ||||||
| git.sr.ht/~mariusor/ssm v0.0.0-20240811085540-34f24cac52b7/go.mod h1:VApG24PG5Ij+tw5zpN5O61FSQU9gJK/cYQwFYM+kkwA= | git.sr.ht/~mariusor/ssm v0.0.0-20240811085540-34f24cac52b7/go.mod h1:VApG24PG5Ij+tw5zpN5O61FSQU9gJK/cYQwFYM+kkwA= | ||||||
|  | github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= | ||||||
|  | github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= | ||||||
| github.com/carlmjohnson/be v0.23.2 h1:1QjPnPJhwGUjsD9+7h98EQlKsxnG5TV+nnEvk0wnkls= | github.com/carlmjohnson/be v0.23.2 h1:1QjPnPJhwGUjsD9+7h98EQlKsxnG5TV+nnEvk0wnkls= | ||||||
| github.com/carlmjohnson/be v0.23.2/go.mod h1:KAgPUh0HpzWYZZI+IABdo80wTgY43YhbdsiLYAaSI/Q= | github.com/carlmjohnson/be v0.23.2/go.mod h1:KAgPUh0HpzWYZZI+IABdo80wTgY43YhbdsiLYAaSI/Q= | ||||||
| github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= | github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue