Move log to pkg/log and output functions to internal/output

This commit is contained in:
Ian Gulliver
2025-06-27 21:08:37 -07:00
parent 92ba299941
commit 99e17464ea
6 changed files with 38 additions and 33 deletions
+2 -1
View File
@@ -8,6 +8,7 @@ import (
"strings"
"github.com/gopatchy/bkl"
"github.com/gopatchy/bkl/pkg/log"
"github.com/jessevdk/go-flags"
"golang.org/x/exp/constraints"
)
@@ -68,7 +69,7 @@ Related tools:
}
if opts.Verbose {
bkl.Debug = true
log.Debug = true
}
files := make([]string, len(opts.Positional.InputPaths))
+4 -4
View File
@@ -1,10 +1,10 @@
package bkl
package output
import (
"strings"
)
func finalizeOutput(obj any) any {
func FinalizeOutput(obj any) any {
switch obj2 := obj.(type) {
case map[string]any:
return finalizeMap(obj2)
@@ -23,7 +23,7 @@ func finalizeOutput(obj any) any {
func finalizeMap(obj map[string]any) map[string]any {
newObj := make(map[string]any, len(obj))
for k, v := range obj {
newObj[finalizeString(k)] = finalizeOutput(v)
newObj[finalizeString(k)] = FinalizeOutput(v)
}
return newObj
@@ -32,7 +32,7 @@ func finalizeMap(obj map[string]any) map[string]any {
func finalizeList(obj []any) []any {
newList := make([]any, len(obj))
for idx, v := range obj {
newList[idx] = finalizeOutput(v)
newList[idx] = FinalizeOutput(v)
}
return newList
+7 -7
View File
@@ -1,8 +1,8 @@
package bkl
package output
import "github.com/gopatchy/bkl/internal/utils"
func findOutputs(obj any) (any, []any, error) {
func FindOutputs(obj any) (any, []any, error) {
switch obj2 := obj.(type) {
case map[string]any:
return findOutputsMap(obj2)
@@ -25,7 +25,7 @@ func findOutputsMap(obj map[string]any) (any, []any, error) {
}
for k, v := range utils.SortedMap(obj) {
vNew, subOuts, err := findOutputs(v)
vNew, subOuts, err := FindOutputs(v)
if err != nil {
return nil, nil, err
}
@@ -47,7 +47,7 @@ func findOutputsList(obj []any) (any, []any, error) {
}
for _, v := range obj {
vNew, subOuts, err := findOutputs(v)
vNew, subOuts, err := FindOutputs(v)
if err != nil {
return nil, nil, err
}
@@ -63,7 +63,7 @@ func findOutputsList(obj []any) (any, []any, error) {
return ret, outs, nil
}
func filterOutput(obj any) (any, bool, error) {
func FilterOutput(obj any) (any, bool, error) {
switch obj2 := obj.(type) {
case map[string]any:
return filterOutputMap(obj2)
@@ -83,7 +83,7 @@ func filterOutputMap(obj map[string]any) (any, bool, error) {
}
filtered, err := utils.FilterMap(obj, func(k string, v any) (map[string]any, error) {
v2, include, err := filterOutput(v)
v2, include, err := FilterOutput(v)
if err != nil {
return nil, err
}
@@ -109,7 +109,7 @@ func filterOutputList(obj []any) (any, bool, error) {
}
filtered, err := utils.FilterList(obj, func(v any) ([]any, error) {
v2, include, err := filterOutput(v)
v2, include, err := FilterOutput(v)
if err != nil {
return nil, err
}
-13
View File
@@ -1,13 +0,0 @@
package bkl
import (
"log"
)
func debugLog(format string, v ...any) {
if !Debug {
return
}
log.Printf(format, v...)
}
+7 -8
View File
@@ -16,14 +16,13 @@ import (
"github.com/gopatchy/bkl/internal/file"
"github.com/gopatchy/bkl/internal/format"
"github.com/gopatchy/bkl/internal/fsys"
"github.com/gopatchy/bkl/internal/output"
"github.com/gopatchy/bkl/internal/process"
"github.com/gopatchy/bkl/internal/utils"
"github.com/gopatchy/bkl/pkg/errors"
"github.com/gopatchy/bkl/pkg/log"
)
// Debug controls debug log output to stderr for all bkl operations.
var Debug = os.Getenv("BKL_DEBUG") != ""
// A bkl reads input documents, merges layers, and generates outputs.
//
// # Terminology
@@ -196,10 +195,10 @@ func (b *bkl) mergeFiles(fx fs.FS, files []string, ft *format.Format, env map[st
// mergeFile applies an already-parsed file object into the [bkl]'s
// document state.
func (b *bkl) mergeFileObj(f *file.File) error {
debugLog("[%s] merging", f)
log.Debugf("[%s] merging", f)
for _, doc := range f.Docs {
debugLog("[%s] merging", doc)
log.Debugf("[%s] merging", doc)
err := b.mergeDocument(doc)
if err != nil {
@@ -221,7 +220,7 @@ func (b *bkl) outputDocument(doc *document.Document, env map[string]string) ([]a
outs := []any{}
for _, d := range docs {
obj, out, err := findOutputs(d.Data)
obj, out, err := output.FindOutputs(d.Data)
if err != nil {
return nil, err
}
@@ -234,7 +233,7 @@ func (b *bkl) outputDocument(doc *document.Document, env map[string]string) ([]a
}
return utils.FilterList(outs, func(v any) ([]any, error) {
v2, include, err := filterOutput(v)
v2, include, err := output.FilterOutput(v)
if err != nil {
return nil, err
}
@@ -248,7 +247,7 @@ func (b *bkl) outputDocument(doc *document.Document, env map[string]string) ([]a
return nil, err
}
return []any{finalizeOutput(v2)}, nil
return []any{output.FinalizeOutput(v2)}, nil
})
}
+18
View File
@@ -0,0 +1,18 @@
package log
import (
"log"
"os"
)
// Debug controls debug log output. Set by BKL_DEBUG environment variable by default.
var Debug = os.Getenv("BKL_DEBUG") != ""
// Debugf logs a debug message if Debug is true.
func Debugf(format string, v ...any) {
if !Debug {
return
}
log.Printf(format, v...)
}