mirror of
https://github.com/sbrow/thor.git
synced 2026-08-26 11:23:32 -04:00
refactor: flags are now parsed before creating the consoe_logger.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "base:runtime"
|
import "base:runtime"
|
||||||
|
import "core:flags"
|
||||||
import "core:log"
|
import "core:log"
|
||||||
import "core:mem"
|
import "core:mem"
|
||||||
import "core:os"
|
import "core:os"
|
||||||
@@ -44,19 +45,20 @@ main :: proc() {
|
|||||||
defer spall.buffer_destroy(&spall_ctx, &spall_buffer)
|
defer spall.buffer_destroy(&spall_ctx, &spall_buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cli_flags: Flags
|
||||||
|
flags.parse_or_exit(&cli_flags, os.args, .Odin)
|
||||||
|
|
||||||
|
level: log.Level
|
||||||
|
switch {
|
||||||
|
case cli_flags.quiet:
|
||||||
|
level = .Warning
|
||||||
|
case cli_flags.verbose:
|
||||||
|
level = .Debug
|
||||||
|
case:
|
||||||
|
level = .Info
|
||||||
|
}
|
||||||
logger_opts: log.Options =
|
logger_opts: log.Options =
|
||||||
(log.Default_Console_Logger_Opts - log.Full_Timestamp_Opts - {.Short_File_Path})
|
(log.Default_Console_Logger_Opts - log.Full_Timestamp_Opts - {.Short_File_Path})
|
||||||
level := log.Level.Info
|
|
||||||
for arg in os.args {
|
|
||||||
switch (arg) {
|
|
||||||
case "-verbose":
|
|
||||||
level = .Debug;
|
|
||||||
break
|
|
||||||
case "-quiet":
|
|
||||||
level = .Warning;
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
console_logger := log.create_console_logger(level, logger_opts)
|
console_logger := log.create_console_logger(level, logger_opts)
|
||||||
context.logger = console_logger
|
context.logger = console_logger
|
||||||
defer log.destroy_console_logger(console_logger)
|
defer log.destroy_console_logger(console_logger)
|
||||||
@@ -75,7 +77,7 @@ main :: proc() {
|
|||||||
)
|
)
|
||||||
tick := time.tick_now()
|
tick := time.tick_now()
|
||||||
site: Site
|
site: Site
|
||||||
init_site(&site, os.args)
|
init_site(&site, cli_flags)
|
||||||
defer destroy_site(&site)
|
defer destroy_site(&site)
|
||||||
// TODO: Make it so this isn't necessary
|
// TODO: Make it so this isn't necessary
|
||||||
context.allocator = site_allocator(&site)
|
context.allocator = site_allocator(&site)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "core:encoding/json"
|
import "core:encoding/json"
|
||||||
import "core:flags"
|
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "core:log"
|
import "core:log"
|
||||||
import "core:mem"
|
import "core:mem"
|
||||||
@@ -94,7 +93,7 @@ Flags :: struct {
|
|||||||
md_disable: string `args:"name=no-ext" usage:"Disable markdown extensions (comma-separated: emoji,sidenotes,alerts,highlight,sections)"`,
|
md_disable: string `args:"name=no-ext" usage:"Disable markdown extensions (comma-separated: emoji,sidenotes,alerts,highlight,sections)"`,
|
||||||
}
|
}
|
||||||
|
|
||||||
init_site :: proc(site: ^Site, args: []string) {
|
init_site :: proc(site: ^Site, flags: Flags) {
|
||||||
mem.dynamic_arena_init(&site.arena)
|
mem.dynamic_arena_init(&site.arena)
|
||||||
alloc := site_allocator(site)
|
alloc := site_allocator(site)
|
||||||
|
|
||||||
@@ -102,10 +101,7 @@ init_site :: proc(site: ^Site, args: []string) {
|
|||||||
site.base_url = "http://localhost:8080"
|
site.base_url = "http://localhost:8080"
|
||||||
site.markdown_extensions = md.DEFAULT_EXTENSIONS
|
site.markdown_extensions = md.DEFAULT_EXTENSIONS
|
||||||
|
|
||||||
_flags: Flags
|
path := flags.config_path
|
||||||
flags.parse_or_exit(&_flags, args, .Odin, alloc)
|
|
||||||
|
|
||||||
path := _flags.config_path
|
|
||||||
if path == "" {
|
if path == "" {
|
||||||
found, ok := find_config("thor.json")
|
found, ok := find_config("thor.json")
|
||||||
if ok {
|
if ok {
|
||||||
@@ -130,7 +126,7 @@ init_site :: proc(site: ^Site, args: []string) {
|
|||||||
site_apply_path_defaults(site, config_dir)
|
site_apply_path_defaults(site, config_dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
site_apply_cli_flags(site, _flags)
|
site_apply_cli_flags(site, flags)
|
||||||
site.config_path = path
|
site.config_path = path
|
||||||
|
|
||||||
site.og = og_for_site(site)
|
site.og = og_for_site(site)
|
||||||
|
|||||||
+20
-13
@@ -3,6 +3,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "core:encoding/json"
|
import "core:encoding/json"
|
||||||
|
import "core:flags"
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "core:log"
|
import "core:log"
|
||||||
import "core:os"
|
import "core:os"
|
||||||
@@ -108,8 +109,9 @@ test_init_site_defaults_no_config :: proc(t: ^testing.T) {
|
|||||||
context.logger = log.nil_logger()
|
context.logger = log.nil_logger()
|
||||||
|
|
||||||
site: Site
|
site: Site
|
||||||
args := []string{"thor", "-config:./nonexistent.json"}
|
_flags: Flags
|
||||||
init_site(&site, args)
|
flags.parse_or_exit(&_flags, []string{"thor", "-config:./nonexistent.json"}, .Odin)
|
||||||
|
init_site(&site, _flags)
|
||||||
defer destroy_site(&site)
|
defer destroy_site(&site)
|
||||||
|
|
||||||
testing.expect_value(t, site.content_dir, "./content")
|
testing.expect_value(t, site.content_dir, "./content")
|
||||||
@@ -127,8 +129,9 @@ test_init_site_config_dir_relative :: proc(t: ^testing.T) {
|
|||||||
context.logger = log.nil_logger()
|
context.logger = log.nil_logger()
|
||||||
|
|
||||||
site: Site
|
site: Site
|
||||||
args := []string{"thor", "-config:./sub/nonexistent.json"}
|
_flags: Flags
|
||||||
init_site(&site, args)
|
flags.parse_or_exit(&_flags, []string{"thor", "-config:./sub/nonexistent.json"}, .Odin)
|
||||||
|
init_site(&site, _flags)
|
||||||
defer destroy_site(&site)
|
defer destroy_site(&site)
|
||||||
|
|
||||||
testing.expect_value(t, site.content_dir, "./sub/content")
|
testing.expect_value(t, site.content_dir, "./sub/content")
|
||||||
@@ -142,8 +145,9 @@ test_init_site_flag_overrides_default :: proc(t: ^testing.T) {
|
|||||||
context.logger = log.nil_logger()
|
context.logger = log.nil_logger()
|
||||||
|
|
||||||
site: Site
|
site: Site
|
||||||
args := []string{"thor", "-config:./nonexistent.json", "-drafts", "-base-url:https://flag.com"}
|
_flags: Flags
|
||||||
init_site(&site, args)
|
flags.parse_or_exit(&_flags, []string{"thor", "-config:./nonexistent.json", "-drafts", "-base-url:https://flag.com"}, .Odin)
|
||||||
|
init_site(&site, _flags)
|
||||||
defer destroy_site(&site)
|
defer destroy_site(&site)
|
||||||
|
|
||||||
testing.expect(t, .Drafts in site.features)
|
testing.expect(t, .Drafts in site.features)
|
||||||
@@ -161,8 +165,9 @@ test_init_site_full_pipeline :: proc(t: ^testing.T) {
|
|||||||
defer os.remove(path)
|
defer os.remove(path)
|
||||||
|
|
||||||
site: Site
|
site: Site
|
||||||
args := []string{"thor", fmt.tprintf("-config:%s", path), "-drafts"}
|
_flags: Flags
|
||||||
init_site(&site, args)
|
flags.parse_or_exit(&_flags, []string{"thor", fmt.tprintf("-config:%s", path), "-drafts"}, .Odin)
|
||||||
|
init_site(&site, _flags)
|
||||||
defer destroy_site(&site)
|
defer destroy_site(&site)
|
||||||
|
|
||||||
testing.expect_value(t, site.title, "Pipeline Test")
|
testing.expect_value(t, site.title, "Pipeline Test")
|
||||||
@@ -176,13 +181,14 @@ test_init_site_md_enable_disable :: proc(t: ^testing.T) {
|
|||||||
context.logger = log.nil_logger()
|
context.logger = log.nil_logger()
|
||||||
|
|
||||||
site: Site
|
site: Site
|
||||||
args := []string {
|
_flags: Flags
|
||||||
|
flags.parse_or_exit(&_flags, []string {
|
||||||
"thor",
|
"thor",
|
||||||
"-config:./nonexistent.json",
|
"-config:./nonexistent.json",
|
||||||
"-ext:highlight,sections",
|
"-ext:highlight,sections",
|
||||||
"-no-ext:emoji",
|
"-no-ext:emoji",
|
||||||
}
|
}, .Odin)
|
||||||
init_site(&site, args)
|
init_site(&site, _flags)
|
||||||
defer destroy_site(&site)
|
defer destroy_site(&site)
|
||||||
|
|
||||||
testing.expect(t, .Highlight in site.markdown_extensions)
|
testing.expect(t, .Highlight in site.markdown_extensions)
|
||||||
@@ -207,8 +213,9 @@ test_init_site_config_paths :: proc(t: ^testing.T) {
|
|||||||
defer os.remove(path)
|
defer os.remove(path)
|
||||||
|
|
||||||
site: Site
|
site: Site
|
||||||
args := []string{"thor", fmt.tprintf("-config:%s", path)}
|
_flags: Flags
|
||||||
init_site(&site, args)
|
flags.parse_or_exit(&_flags, []string{"thor", fmt.tprintf("-config:%s", path)}, .Odin)
|
||||||
|
init_site(&site, _flags)
|
||||||
defer destroy_site(&site)
|
defer destroy_site(&site)
|
||||||
|
|
||||||
testing.expect_value(t, site.content_dir, "/custom/content")
|
testing.expect_value(t, site.content_dir, "/custom/content")
|
||||||
|
|||||||
Reference in New Issue
Block a user