diff --git a/Makefile b/Makefile index 68cb85d..b99de58 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,8 @@ BINARY=${MYNAME} UNIX_BINARY=${MYNAME} WINDOWS_BINARY=${MYNAME}.exe RMCMD=rm -rf +EXAMPLE01=01-LoadJsonFile +EXAMPLE02=02-TestLuaScript # На windows имя бинарника может зависеть не только от платформы, но и от выбранной цели, для linux-а суффикс .exe # не нужен @@ -58,19 +60,19 @@ clean: ifeq ($(OS),Windows_NT) ifeq ($(SHELL),sh.exe) # # Vanilla cmd.exe / powershell. - if exist 01-LoadJsonFile.exe ${RMCMD} 01-LoadJsonFile.exe - if exist 01-LoadJsonFile ${RMCMD} 01-LoadJsonFile - if exist 02-TestLuaScript.exe ${RMCMD} 02-TestLuaScript.exe - if exist 02-TestLuaScript ${RMCMD} 02-TestLuaScript + if exist ${EXAMPLE01}.exe ${RMCMD} ${EXAMPLE01}.exe + if exist ${EXAMPLE01} ${RMCMD} ${EXAMPLE01} + if exist ${EXAMPLE02}.exe ${RMCMD} ${EXAMPLE02}.exe + if exist ${EXAMPLE02} ${RMCMD} ${EXAMPLE02} else # not ifeq ($(SHELL),sh.exe) - ${RMCMD} ./01-LoadJsonFile.exe - ${RMCMD} ./01-LoadJsonFile - ${RMCMD} ./02-TestLuaScript.exe - ${RMCMD} ./02-TestLuaScript + ${RMCMD} ./${EXAMPLE01}.exe + ${RMCMD} ./${EXAMPLE01} + ${RMCMD} ./${EXAMPLE02}.exe + ${RMCMD} ./${EXAMPLE02} endif # ifeq ($(SHELL),sh.exe) else # not ifeq ($(OS),Windows_NT) - ${RMCMD} ./01-LoadJsonFile - ${RMCMD} ./02-TestLuaScript + ${RMCMD} ${EXAMPLE01} + ${RMCMD} ${EXAMPLE02} endif @@ -80,23 +82,23 @@ upgrade: go mod vendor -01-LoadJsonFile: clean +${EXAMPLE01}: clean ifeq ($(OS),Windows_NT) # Looks like on windows gnu make explicitly set SHELL to sh.exe, if it was not set. ifeq ($(SHELL), sh.exe) # # Vanilla cmd.exe / powershell. SET "CGO_ENABLED=0" - go build ${BUILDOPTS} -o 01-LoadJsonFile.exe ./cmd/01-LoadJsonFile + go build ${BUILDOPTS} -o ${EXAMPLE01}.exe ./cmd/${EXAMPLE01} else ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) # # git-bash - CGO_ENABLED=0 go build ${BUILDOPTS} -o 01-LoadJsonFile.exe ./cmd/01-LoadJsonFile + CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE01}.exe ./cmd/${EXAMPLE01} else # not ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) # # Some other shell. # # TODO: handle it. $(info "-- Dunno how to handle this shell: ${SHELL}") endif # ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) else # not ($(OS),Windows_NT) - CGO_ENABLED=0 go build ${BUILDOPTS} -o 01-LoadJsonFile ./cmd/01-LoadJsonFile + CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE01} ./cmd/${EXAMPLE01} endif # ifeq ($(OS),Windows_NT) @@ -106,17 +108,18 @@ ifeq ($(OS),Windows_NT) ifeq ($(SHELL), sh.exe) # # Vanilla cmd.exe / powershell. SET "CGO_ENABLED=0" - go build ${BUILDOPTS} -o 02-TestLuaScript.exe ./cmd/02-TestLuaScript + go build ${BUILDOPTS} -o ${EXAMPLE02}.exe ./cmd/${EXAMPLE02} else ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) # # git-bash - CGO_ENABLED=0 go build ${BUILDOPTS} -o 02-TestLuaScript.exe ./cmd/02-TestLuaScript + CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE02}.exe ./cmd/${EXAMPLE02} else # not ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) # # Some other shell. # # TODO: handle it. $(info "-- Dunno how to handle this shell: ${SHELL}") endif # ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) else # not ($(OS),Windows_NT) - CGO_ENABLED=0 go build ${BUILDOPTS} -o 02-TestLuaScript ./cmd/02-TestLuaScript + CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE02} ./cmd/${EXAMPLE02} endif # ifeq ($(OS),Windows_NT) -# vim: set ft=make noet ai ts=4 sw=4 sts=4: \ No newline at end of file + +# vim: set ft=make noet ai ts=4 sw=4 sts=4: diff --git a/cmd/01-LoadJsonFile/main.go b/cmd/01-LoadJsonFile/main.go deleted file mode 100644 index 2c34340..0000000 --- a/cmd/01-LoadJsonFile/main.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import ( - "doll/internal/doll" - "doll/internal/log" - "os" - - "github.com/davecgh/go-spew/spew" -) - -func main() { - log.Init("info", os.Stderr) - log.Info("loading config.json") - - data := doll.LoadJsonFile("test_data/config.json") - - log.Info("data fron config.json is") - spew.Dump(data) -} - -/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */ diff --git a/cmd/02-TestLuaScript/main.go b/cmd/02-TestLuaScript/main.go deleted file mode 100644 index a1c40c5..0000000 --- a/cmd/02-TestLuaScript/main.go +++ /dev/null @@ -1,34 +0,0 @@ -package main - -import ( - "doll/internal/log" - "os" - - lua "github.com/yuin/gopher-lua" -) - -func main() { - log.Init("info", os.Stderr) - - L := lua.NewState() - defer L.Close() - - // Load the Lua script file. - log.Info("Loading ./test_data/01-test.lua") - - luaCode, err := os.ReadFile("./test_data/01-test.lua") - - if err != nil { - log.Errorf("Error reading Lua file: %s", err) - - os.Exit(1) - } - - if err := L.DoString(string(luaCode)); err != nil { - log.Errorf("Error evaluating Lua code: %s", err) - - os.Exit(1) - } -} - -/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */ diff --git a/test_data/config.json b/example_data/01-config.json similarity index 96% rename from test_data/config.json rename to example_data/01-config.json index b4b932e..6326a35 100644 --- a/test_data/config.json +++ b/example_data/01-config.json @@ -1,4 +1,4 @@ { "loglevel": "info", "logfile": "/var/log/messages" -} \ No newline at end of file +} diff --git a/example_data/02-test.lua b/example_data/02-test.lua new file mode 100644 index 0000000..cfdd965 --- /dev/null +++ b/example_data/02-test.lua @@ -0,0 +1,3 @@ +print ("Hello World!") + +-- vim: set ft=lua noet ai ts=4 sw=4 sts=4: diff --git a/examples/01-LoadJsonFile/main.go b/examples/01-LoadJsonFile/main.go new file mode 100644 index 0000000..ea8d678 --- /dev/null +++ b/examples/01-LoadJsonFile/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "doll/internal/doll" + "doll/internal/log" + "os" + + "github.com/davecgh/go-spew/spew" +) + +func main() { + // Init log subsystem. + log.Init("info", os.Stderr) + + file := "example_data/01-config.json" + + // Load the Lua script file. + log.Infof("Loading %s", file) + + data, err := doll.LoadJsonFile(file) + + if err != nil { + log.Fatalf("Unable to unmarshal %s to map: %s", file, err) + } + + log.Infof("Map unmarshalled from %s", file) + + spew.Dump(data) +} + +/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */ diff --git a/examples/02-TestLuaScript/main.go b/examples/02-TestLuaScript/main.go new file mode 100644 index 0000000..fbf40c5 --- /dev/null +++ b/examples/02-TestLuaScript/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "doll/internal/log" + "os" + + lua "github.com/yuin/gopher-lua" +) + +func main() { + // Init log subsystem. + log.Init("info", os.Stderr) + + file := "example_data/02-test.lua" + + // Load the Lua script file. + log.Infof("Loading %s", file) + + // We can execute script directly via L.DoFile("./example_data/02-test.lua") but in practice we'll get scripts as + // strings - snippets from templates - so, testing/checking how it works is not use for project. + luaCode, err := os.ReadFile(file) + + if err != nil { + log.Fatalf("Error reading Lua file %s: %s", file, err) + } + + log.Info("Creating new Lua state") + L := lua.NewState() + defer L.Close() + + log.Info("Evaluating Lua code") + if err := L.DoString(string(luaCode)); err != nil { + log.Fatalf("Error evaluating Lua code: %s", err) + } +} + +/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */ diff --git a/internal/doll/jsonfile.go b/internal/doll/jsonfile.go index 7877f10..78040fc 100644 --- a/internal/doll/jsonfile.go +++ b/internal/doll/jsonfile.go @@ -1,30 +1,30 @@ package doll import ( - "doll/internal/log" "encoding/json" + "fmt" "os" ) // LoadJsonFile loads data from json to Instance global variable map. -func LoadJsonFile(file string) map[string]any { +func LoadJsonFile(file string) (map[string]any, error) { var ( - err error - data []byte + err error + data []byte + myMap map[string]any ) data, err = os.ReadFile(file) if err != nil { - log.Fatalf("Unable to load %s: %s", file, err) + return myMap, fmt.Errorf("Unable to load %s: %w", file, err) } - err = json.Unmarshal([]byte(data), &Instance) - if err != nil { - log.Fatalf("Unable to marshal JSON data from %s: %s", file, err) + if err = json.Unmarshal([]byte(data), &myMap); err != nil { + return myMap, fmt.Errorf("Unable to unmarshal JSON data from %s: %w", file, err) } - return Instance + return myMap, err } /* vim: set ft=go noet ai ts=4 sw=4 sts=4: */ diff --git a/test_data/01-test.lua b/test_data/01-test.lua deleted file mode 100644 index 6f298a5..0000000 --- a/test_data/01-test.lua +++ /dev/null @@ -1,3 +0,0 @@ -print ("Hello World!") - --- vim: set ft=go noet ai ts=4 sw=4 sts=4: