Files
kubeshark/config/configStructs/scriptingConfig.go
M. Mert Yildiran a6dd98d241 Log the found scripts
2023-02-06 15:27:07 +03:00

79 lines
1.4 KiB
Go

package configStructs
import (
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"github.com/robertkrimen/otto/ast"
"github.com/robertkrimen/otto/file"
"github.com/robertkrimen/otto/parser"
"github.com/rs/zerolog/log"
)
type ScriptingConfig struct {
Consts map[string]interface{} `yaml:"consts"`
Source string `yaml:"source" default:""`
}
type Script struct {
Title string `json:"title"`
Code string `json:"code"`
}
func (config *ScriptingConfig) GetScripts() (scripts []*Script, err error) {
if config.Source == "" {
return
}
var files []fs.FileInfo
files, err = ioutil.ReadDir(config.Source)
if err != nil {
return
}
for _, f := range files {
if f.IsDir() {
continue
}
filename := f.Name()
var body []byte
path := filepath.Join(config.Source, filename)
body, err = os.ReadFile(path)
if err != nil {
return
}
content := string(body)
var program *ast.Program
program, err = parser.ParseFile(nil, filename, content, parser.StoreComments)
if err != nil {
return
}
var title string
code := content
var idx0 file.Idx
for node, comments := range program.Comments {
if (idx0 > 0 && node.Idx0() > idx0) || len(comments) == 0 {
continue
}
idx0 = node.Idx0()
title = comments[0].Text
}
scripts = append(scripts, &Script{
Title: title,
Code: code,
})
log.Info().Str("path", path).Msg("Found script:")
}
return
}