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
|
// real process env vars win over the file
|
||||||
if override, found := os.lookup_env(key, context.temp_allocator); found {
|
if override, found := os.lookup_env(key, allocator); found {
|
||||||
value = override
|
result[strings.clone(key, allocator)] = override
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// clone so the map outlives the source buffer (e.g. a freed file read)
|
// 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
|
// 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).
|
// 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 {
|
for key, value in env {
|
||||||
delete(key)
|
delete(key, allocator)
|
||||||
delete(value)
|
delete(value, allocator)
|
||||||
}
|
}
|
||||||
delete(env)
|
delete(env)
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-6
@@ -1,5 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "core:log"
|
||||||
import "core:os"
|
import "core:os"
|
||||||
import dotenv "lib:dotenv/src"
|
import dotenv "lib:dotenv/src"
|
||||||
|
|
||||||
@@ -15,18 +16,17 @@ get_config :: proc(env_file: ^string = nil) -> (^Config, ^Error) {
|
|||||||
path = from_env
|
path = from_env
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.debugf("getting config from file: %s", path)
|
||||||
|
|
||||||
env, _ := dotenv.parse_file(path, context.temp_allocator)
|
env, _ := dotenv.parse_file(path, context.temp_allocator)
|
||||||
defer dotenv.destroy(env)
|
defer dotenv.destroy(env, context.temp_allocator)
|
||||||
|
|
||||||
c := new(Config)
|
c := new(Config)
|
||||||
|
|
||||||
if env == nil {
|
if env == nil {
|
||||||
c.api_url = os.get_env("API_URL", context.temp_allocator)
|
c.api_url = os.get_env("API_URL", context.temp_allocator)
|
||||||
} else if !dotenv.decode(env, c) {
|
} else if !dotenv.decode(env, c) {
|
||||||
err := new(Error)
|
return nil, new_clone(Error{.Config, "failed to decode .env into Config"})
|
||||||
err.type = .Config
|
|
||||||
err.message = "failed to decode .env into Config"
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := validate_config(c); err != nil {
|
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) {
|
validate_config :: proc(c: ^Config) -> (err: ^Error) {
|
||||||
if c.api_url == "" {
|
if c.api_url == "" {
|
||||||
err = &Error{.Config, "'API_URL' is required"}
|
err = new_clone(Error{.Config, "'API_URL' is required"})
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "core:net"
|
||||||
|
import "vendor:curl"
|
||||||
|
|
||||||
Catalog :: struct {
|
Catalog :: struct {
|
||||||
name: string,
|
name: string,
|
||||||
release: string,
|
release: string,
|
||||||
@@ -22,6 +25,10 @@ APIError :: struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get_catalogs :: proc(url: string) -> ([dynamic]Catalog, ^APIError) {
|
get_catalogs :: proc(url: string) -> ([dynamic]Catalog, ^APIError) {
|
||||||
|
ucurl := curl.url()
|
||||||
|
|
||||||
|
|
||||||
|
defer curl.url_cleanup(ucurl)
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -28,10 +28,11 @@ rng: rand.Default_Random_State
|
|||||||
main :: proc() {
|
main :: proc() {
|
||||||
|
|
||||||
c: ^Config
|
c: ^Config
|
||||||
|
err: ^Error
|
||||||
|
|
||||||
s := os.get_env("GUI_ENV_FILE", context.temp_allocator)
|
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))
|
panic(format_error(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user