gopp/tool.go

44 lines
818 B
Go

package tpp
import (
//"github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/stdlib"
"surdeus.su/core/cli/mtool"
//"fmt"
"os"
"log"
"path/filepath"
"context"
)
var Tool = mtool.T("pp").Func(func(flags *mtool.Flags){
t := NewTengo().
SetPreCompile(func(ctx context.Context, s *Script){
s.SetImports(stdlib.GetModuleMap(
stdlib.AllModuleNames()...,
))
s.EnableFileImport(true)
s.SetImportDir(".")
})
pp := New(t)
filePaths := flags.Parse()
for _, filePath := range filePaths {
pth := filepath.FromSlash(filePath)
bts, err := os.ReadFile(pth)
if err != nil {
log.Println("read error:", err)
continue
}
out, err := pp.Process(
context.Background(),
true,
pth,
bts,
)
if err != nil {
log.Println("pp error:", err)
continue
}
os.Stdout.Write(out)
}
})