got dotenv parsing working
This commit is contained in:
@@ -38,8 +38,9 @@ parse :: proc(src: string, allocator := context.allocator) -> map[string]string
|
||||
}
|
||||
|
||||
// real process env vars win over the file
|
||||
if override, found := os.lookup_env(key, context.temp_allocator); found {
|
||||
value = override
|
||||
if override, found := os.lookup_env(key, allocator); found {
|
||||
result[strings.clone(key, allocator)] = override
|
||||
continue
|
||||
}
|
||||
|
||||
// clone so the map outlives the source buffer (e.g. a freed file read)
|
||||
@@ -63,10 +64,11 @@ parse_file :: proc(filename: string, allocator := context.allocator) -> (map[str
|
||||
|
||||
// destroy frees the cloned keys/values and the map itself. Use it to release
|
||||
// a map returned by parse/parse_file (plain delete does not free the strings).
|
||||
destroy :: proc(env: map[string]string) {
|
||||
// Any allocator passed to parse/parse_file must be passed here too.
|
||||
destroy :: proc(env: map[string]string, allocator := context.allocator) {
|
||||
for key, value in env {
|
||||
delete(key)
|
||||
delete(value)
|
||||
delete(key, allocator)
|
||||
delete(value, allocator)
|
||||
}
|
||||
delete(env)
|
||||
}
|
||||
|
||||
+6
-6
@@ -1,5 +1,6 @@
|
||||
package main
|
||||
|
||||
import "core:log"
|
||||
import "core:os"
|
||||
import dotenv "lib:dotenv/src"
|
||||
|
||||
@@ -15,18 +16,17 @@ get_config :: proc(env_file: ^string = nil) -> (^Config, ^Error) {
|
||||
path = from_env
|
||||
}
|
||||
|
||||
log.debugf("getting config from file: %s", path)
|
||||
|
||||
env, _ := dotenv.parse_file(path, context.temp_allocator)
|
||||
defer dotenv.destroy(env)
|
||||
defer dotenv.destroy(env, context.temp_allocator)
|
||||
|
||||
c := new(Config)
|
||||
|
||||
if env == nil {
|
||||
c.api_url = os.get_env("API_URL", context.temp_allocator)
|
||||
} else if !dotenv.decode(env, c) {
|
||||
err := new(Error)
|
||||
err.type = .Config
|
||||
err.message = "failed to decode .env into Config"
|
||||
return nil, err
|
||||
return nil, new_clone(Error{.Config, "failed to decode .env into Config"})
|
||||
}
|
||||
|
||||
if err := validate_config(c); err != nil {
|
||||
@@ -38,7 +38,7 @@ get_config :: proc(env_file: ^string = nil) -> (^Config, ^Error) {
|
||||
|
||||
validate_config :: proc(c: ^Config) -> (err: ^Error) {
|
||||
if c.api_url == "" {
|
||||
err = &Error{.Config, "'API_URL' is required"}
|
||||
err = new_clone(Error{.Config, "'API_URL' is required"})
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package main
|
||||
|
||||
import "core:net"
|
||||
import "vendor:curl"
|
||||
|
||||
Catalog :: struct {
|
||||
name: string,
|
||||
release: string,
|
||||
@@ -22,6 +25,10 @@ APIError :: struct {
|
||||
}
|
||||
|
||||
get_catalogs :: proc(url: string) -> ([dynamic]Catalog, ^APIError) {
|
||||
ucurl := curl.url()
|
||||
|
||||
|
||||
defer curl.url_cleanup(ucurl)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -28,10 +28,11 @@ rng: rand.Default_Random_State
|
||||
main :: proc() {
|
||||
|
||||
c: ^Config
|
||||
err: ^Error
|
||||
|
||||
s := os.get_env("GUI_ENV_FILE", context.temp_allocator)
|
||||
|
||||
if c, err := get_config(&s); err != nil {
|
||||
if c, err = get_config(&s); err != nil {
|
||||
panic(format_error(err))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user