lang: parse, core: world: Add a collect package
Some checks failed
/ Test (basic) on ubuntu-latest with golang 1.23 (push) Has been cancelled
/ Test (race) on ubuntu-latest with golang 1.23 (push) Has been cancelled
/ Test (shell) on ubuntu-latest with golang 1.23 (push) Has been cancelled

This lets us look at the available resource data for collection, and to
filter it so we can decide what we want to collect on our machine.

Other types of collect functions could be added in the future.
This commit is contained in:
James Shubin
2025-04-05 17:00:53 -04:00
parent af04d364d0
commit 9c1c587f7b
6 changed files with 374 additions and 1 deletions

View File

@@ -0,0 +1,52 @@
import "fmt"
import "golang/strings" as golang_strings
import "iter"
import "world/collect"
file "/tmp/foo" {
state => $const.res.file.state.exists,
content => "i am foo\n",
Meta:export => ["*",],
#Meta:export => ["${hostname}",], # alternatively
}
file "/tmp/fah" {
state => $const.res.file.state.exists,
content => "i am fah\n",
#Meta:export => ["*",],
Meta:export => ["${hostname}",],
}
$all = collect.res("file") # []struct{name str; host str;}
# any kind of filter code that you want!
$fn = func($st) {
golang_strings.has_prefix($st->name, "/tmp/fo")
}
$filtered = iter.filter($all, $fn)
print "all" {
msg => fmt.printf("%v", $all),
Meta:autogroup => false,
}
print "filtered" {
msg => fmt.printf("%v", $filtered),
Meta:autogroup => false,
}
collect file $filtered {
Meta:hidden => true, # let's not apply these for now
}
# you can do it manually like this
#$collected = [
# struct{name => "/tmp/foo", host => "${hostname}",},
#]
#
#collect file $collected {
# Meta:hidden => false,
#}