Created
April 17, 2024 12:51
-
-
Save fty4/5ef1d2d0a8ab11a568afd63db9bfcb36 to your computer and use it in GitHub Desktop.
yq golang
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
module git.i.mercedes-benz.com/mlechel/test | |
go 1.22.1 | |
require github.com/mikefarah/yq/v4 v4.43.1 | |
require ( | |
github.com/a8m/envsubst v1.4.2 // indirect | |
github.com/alecthomas/participle/v2 v2.1.1 // indirect | |
github.com/dimchansky/utfbom v1.1.1 // indirect | |
github.com/elliotchance/orderedmap v1.5.1 // indirect | |
github.com/fatih/color v1.16.0 // indirect | |
github.com/goccy/go-json v0.10.2 // indirect | |
github.com/goccy/go-yaml v1.11.3 // indirect | |
github.com/jinzhu/copier v0.4.0 // indirect | |
github.com/magiconair/properties v1.8.7 // indirect | |
github.com/mattn/go-colorable v0.1.13 // indirect | |
github.com/mattn/go-isatty v0.0.20 // indirect | |
github.com/pelletier/go-toml/v2 v2.2.0 // indirect | |
github.com/yuin/gopher-lua v1.1.1 // indirect | |
golang.org/x/net v0.22.0 // indirect | |
golang.org/x/sys v0.18.0 // indirect | |
golang.org/x/text v0.14.0 // indirect | |
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect | |
gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473 // indirect | |
gopkg.in/yaml.v3 v3.0.1 // indirect | |
) |
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" | |
"os" | |
"github.com/mikefarah/yq/v4/pkg/yqlib" | |
gologging "gopkg.in/op/go-logging.v1" | |
) | |
func init() { | |
gologging.SetLevel(gologging.CRITICAL, "yq-lib") | |
yqlib.InitExpressionParser() | |
} | |
func main() { | |
expression := ".hello.world" | |
yamlContent := `foo: bar | |
hello: | |
world: tm | |
` | |
stringEvaluator := yqlib.NewStringEvaluator() | |
pref := yqlib.NewDefaultYamlPreferences() | |
out, err := stringEvaluator.Evaluate(expression, yamlContent, yqlib.NewYamlEncoder(pref), yqlib.NewYamlDecoder(pref)) | |
if err != nil { | |
fmt.Println("no matches found") | |
os.Exit(4) | |
} | |
fmt.Println(out) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment