1
0
mirror of https://github.com/kubeshark/kubeshark.git synced 2025-04-30 12:56:03 +00:00
kubeshark/misc/scripting.go
M. Mert Yildiran 3d5c999be1
Make the scritps command directly use the K8s API without requiring a connector to Hub ()
* Make the `scritps` command directly use the K8s API without requiring a connector to Hub

* Fix linter

* Fix linter

---------

Co-authored-by: Alon Girmonsky <1990761+alongir@users.noreply.github.com>
2024-09-23 10:11:44 -07:00

68 lines
1.1 KiB
Go

package misc
import (
"os"
"path/filepath"
"github.com/robertkrimen/otto/ast"
"github.com/robertkrimen/otto/file"
"github.com/robertkrimen/otto/parser"
)
type Script struct {
Path string `json:"path"`
Title string `json:"title"`
Code string `json:"code"`
}
type ConfigMapScript struct {
Title string `json:"title"`
Code string `json:"code"`
}
func (s *Script) ConfigMap() ConfigMapScript {
return ConfigMapScript{
Title: s.Title,
Code: s.Code,
}
}
func ReadScriptFile(path string) (script *Script, err error) {
filename := filepath.Base(path)
var body []byte
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
var titleIsSet bool
code := content
var idx0 file.Idx
for node, comments := range program.Comments {
if (titleIsSet && node.Idx0() > idx0) || len(comments) == 0 {
continue
}
idx0 = node.Idx0()
title = comments[0].Text
titleIsSet = true
}
script = &Script{
Path: path,
Title: title,
Code: code,
}
return
}