Created
March 11, 2020 18:00
-
-
Save mkmik/38423a395e5cf8eeca5f64f266873843 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"github.com/google/go-jsonnet" | |
"github.com/google/go-jsonnet/ast" | |
"github.com/google/go-jsonnet/toolutils" | |
) | |
var demo = ` | |
local foo = import "a"; | |
local bar = importstr "b"; | |
{} | |
` | |
func visit(n ast.Node, f func(ast.Node)) { | |
f(n) | |
for _, c := range toolutils.Children(n) { | |
visit(c, f) | |
} | |
} | |
func mainE() error { | |
a, err := jsonnet.SnippetToAST("", demo) | |
if err != nil { | |
return err | |
} | |
var files []string | |
visit(a, func(n ast.Node) { | |
switch i := n.(type) { | |
case *ast.Import: | |
files = append(files, i.File.Value) | |
case *ast.ImportStr: | |
files = append(files, i.File.Value) | |
} | |
}) | |
fmt.Println(files) | |
return nil | |
} | |
func main() { | |
if err := mainE(); err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment