gopp/tool.go

45 lines
797 B
Go
Raw Normal View History

package tpp
2024-02-26 00:22:09 +03:00
import (
//"github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/stdlib"
2024-02-26 00:22:09 +03:00
"vultras.su/core/cli/mtool"
//"fmt"
2024-02-26 00:22:09 +03:00
"os"
"log"
"path/filepath"
"context"
2024-02-26 00:22:09 +03:00
)
var Tool = mtool.T("pp").Func(func(flags *mtool.Flags){
t := NewTengo().
SetPreCompile(func(s *Script){
s.SetImports(stdlib.GetModuleMap(
stdlib.AllModuleNames()...,
))
s.EnableFileImport(true)
s.SetImportDir(".")
})
pp := New(t)
2024-02-26 00:22:09 +03:00
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,
)
2024-02-26 00:22:09 +03:00
if err != nil {
log.Println("pp error:", err)
continue
}
os.Stdout.Write(out)
2024-02-26 00:22:09 +03:00
}
})