Bläddra i källkod

switch jsonpath to jq

Alexander Belanger 5 år sedan
förälder
incheckning
394cfb6047
6 ändrade filer med 93 tillägg och 49 borttagningar
  1. 31 26
      cli/cmd/test.go
  2. 2 1
      go.mod
  3. 9 0
      go.sum
  4. 3 2
      internal/templater/form.go
  5. 15 17
      internal/templater/utils/query.go
  6. 33 3
      internal/templater/utils/query_test.go

+ 31 - 26
cli/cmd/test.go

@@ -34,34 +34,39 @@ var testCmd = &cobra.Command{
 
 		form := &models.FormYAML{
 			Tabs: []*models.FormTab{
-				&models.FormTab{
-					Context: &models.FormContext{
-						Type: "helm/values",
-					},
-					Name:  "main",
-					Label: "Main Settings",
-					Sections: []*models.FormSection{
-						&models.FormSection{
-							Name: "section_one",
-							Contents: []*models.FormContent{
-								&models.FormContent{
-									Type:  "number-input",
-									Value: "service.targetPort",
-									Label: "Target Port",
-									Settings: struct {
-										Default interface{} `yaml:"default,omitempty" json:"default,omitempty"`
-										Unit    interface{} `yaml:"unit,omitempty" json:"unit,omitempty"`
-									}{
-										Default: 8000,
-									},
-								},
-							},
-						},
-					},
-				},
+				// &models.FormTab{
+				// 	Context: &models.FormContext{
+				// 		Type: "helm/values",
+				// 	},
+				// 	Name:  "main",
+				// 	Label: "Main Settings",
+				// 	Sections: []*models.FormSection{
+				// 		&models.FormSection{
+				// 			Name: "section_one",
+				// 			Contents: []*models.FormContent{
+				// 				&models.FormContent{
+				// 					Type:  "number-input",
+				// 					Value: "service.targetPort",
+				// 					Label: "Target Port",
+				// 					Settings: struct {
+				// 						Default interface{} `yaml:"default,omitempty" json:"default,omitempty"`
+				// 						Unit    interface{} `yaml:"unit,omitempty" json:"unit,omitempty"`
+				// 					}{
+				// 						Default: 8000,
+				// 					},
+				// 				},
+				// 			},
+				// 		},
+				// 	},
+				// },
 				&models.FormTab{
 					Context: &models.FormContext{
 						Type: "cluster",
+						Config: map[string]string{
+							"group":    "apps",
+							"version":  "v1",
+							"resource": "deployments",
+						},
 					},
 					Name:  "crd",
 					Label: "CRDs",
@@ -71,7 +76,7 @@ var testCmd = &cobra.Command{
 							Contents: []*models.FormContent{
 								&models.FormContent{
 									Type:  "resourcelist",
-									Value: `[{"name": "resource_1"}]`,
+									Value: `[{"name": "certificate_1","namespace": "default","status": "Ready" },{"name": "certificate_2","namespace": "default","status": "Issuing" }]`,
 								},
 							},
 						},

+ 2 - 1
go.mod

@@ -33,6 +33,7 @@ require (
 	github.com/gorilla/websocket v1.4.2
 	github.com/hashicorp/consul/api v1.3.0
 	github.com/imdario/mergo v0.3.11 // indirect
+	github.com/itchyny/gojq v0.11.2
 	github.com/jinzhu/gorm v1.9.16
 	github.com/joeshaw/envdecode v0.0.0-20200121155833-099f1fc765bd
 	github.com/json-iterator/go v1.1.10 // indirect
@@ -49,7 +50,7 @@ require (
 	github.com/stretchr/testify v1.6.1
 	golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0
 	golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43
-	golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6
+	golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f
 	golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
 	google.golang.org/api v0.30.0
 	google.golang.org/genproto v0.0.0-20201014134559-03b6142f0dc9

+ 9 - 0
go.sum

@@ -738,6 +738,7 @@ github.com/hashicorp/serf v0.8.2 h1:YZ7UKsJv+hKjqGVUUbtE3HNj79Eln2oQ75tniF6iPt0=
 github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
 github.com/heketi/heketi v9.0.1-0.20190917153846-c2e2a4ab7ab9+incompatible/go.mod h1:bB9ly3RchcQqsQ9CpyaQwvva7RS5ytVoSoholZQON6o=
 github.com/heketi/tests v0.0.0-20151005000721-f3775cbcefd6/go.mod h1:xGMAM8JLi7UkZt1i4FQeQy0R2T8GLUwQhOP5M1gBhy4=
+github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
 github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
 github.com/huandu/xstrings v1.2.0/go.mod h1:DvyZB1rfVYsBIigL8HwpZgxHwXozlTgGqn63UyNX5k4=
 github.com/huandu/xstrings v1.3.1 h1:4jgBlKK6tLKFvO8u5pmYjG91cqytmDCDvGh7ECVFfFs=
@@ -755,6 +756,12 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
 github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
 github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
 github.com/instrumenta/kubeval v0.0.0-20190918223246-8d013ec9fc56/go.mod h1:bpiMYvNpVxWjdJsS0hDRu9TrobT5GfWCZwJseGUstxE=
+github.com/itchyny/astgen-go v0.0.0-20200815150004-12a293722290/go.mod h1:296z3W7Xsrp2mlIY88ruDKscuvrkL6zXCNRtaYVshzw=
+github.com/itchyny/go-flags v1.5.0/go.mod h1:lenkYuCobuxLBAd/HGFE4LRoW8D3B6iXRQfWYJ+MNbA=
+github.com/itchyny/gojq v0.11.2 h1:lKhMKfH7fTKMWj2Zr8az/9TliCn0TTXVc/BXfQ8Jhfc=
+github.com/itchyny/gojq v0.11.2/go.mod h1:XtmtF1PxeDpwLC1jyz/xAmV78ANlP0S9LVEPsKweK0A=
+github.com/itchyny/timefmt-go v0.1.1 h1:rLpnm9xxb39PEEVzO0n4IRp0q6/RmBc7Dy/rE4HrA0U=
+github.com/itchyny/timefmt-go v0.1.1/go.mod h1:0osSSCQSASBJMsIZnhAaF1C2fCBTJZXrnj37mG8/c+A=
 github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0=
 github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
 github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
@@ -953,6 +960,7 @@ github.com/mattn/go-oci8 v0.0.7/go.mod h1:wjDx6Xm9q7dFtHJvIlrI99JytznLw5wQ4R+9mN
 github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
 github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
 github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
+github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
 github.com/mattn/go-shellwords v1.0.5/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o=
 github.com/mattn/go-shellwords v1.0.10/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
 github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
@@ -1881,6 +1889,7 @@ gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
 gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 gorm.io/driver/postgres v1.0.2 h1:mB5JjD4QglbCTdMT1aZDxQzHr87XDK1qh0MKIU3P96g=
 gorm.io/driver/postgres v1.0.2/go.mod h1:FvRSYfBI9jEp6ZSjlpS9qNcSjxwYxFc03UOTrHdvvYA=
 gorm.io/driver/sqlite v1.1.3 h1:BYfdVuZB5He/u9dt4qDpZqiqDJ6KhPqs5QUqsr/Eeuc=

+ 3 - 2
internal/templater/form.go

@@ -1,7 +1,8 @@
 package templater
 
 import (
-	"k8s.io/client-go/util/jsonpath"
+	// "k8s.io/client-go/util/jsonpath"
+	"github.com/itchyny/gojq"
 )
 
 // OnDataStream is a function that gets called when new data should be
@@ -12,7 +13,7 @@ type TemplateReaderQuery struct {
 	Key         string
 	QueryString string
 
-	Template *jsonpath.JSONPath
+	Template *gojq.Query
 }
 
 // TemplateReader retrieves data from a target data source, registers a set of

+ 15 - 17
internal/templater/utils/query.go

@@ -1,19 +1,14 @@
 package utils
 
 import (
-	"fmt"
-
+	"github.com/itchyny/gojq"
 	"github.com/porter-dev/porter/internal/templater"
-	"k8s.io/client-go/util/jsonpath"
 )
 
 // NewQuery constructs a templater.TemplateReaderQuery by parsing the jsonpath
 // query string
 func NewQuery(key, query string) (*templater.TemplateReaderQuery, error) {
-	j := jsonpath.New(key)
-	j.AllowMissingKeys(true)
-
-	err := j.Parse(query)
+	jquery, err := gojq.Parse(query)
 
 	if err != nil {
 		return nil, err
@@ -22,7 +17,7 @@ func NewQuery(key, query string) (*templater.TemplateReaderQuery, error) {
 	return &templater.TemplateReaderQuery{
 		Key:         key,
 		QueryString: query,
-		Template:    j,
+		Template:    jquery,
 	}, nil
 }
 
@@ -36,19 +31,22 @@ func QueryValues(
 
 	// iterate through all registered queries, add to resulting map
 	for _, query := range queries {
-		fullResults, err := query.Template.FindResults(values)
-
-		if err != nil {
-			fmt.Printf("query error %s", err)
-			continue
-		}
+		iter := query.Template.Run(values)
 
 		queryRes := make([]interface{}, 0)
 
-		for ix := range fullResults {
-			for _, result := range fullResults[ix] {
-				queryRes = append(queryRes, result.Interface())
+		for {
+			v, ok := iter.Next()
+
+			if !ok {
+				break
+			}
+
+			if err, ok := v.(error); ok {
+				return nil, err
 			}
+
+			queryRes = append(queryRes, v)
 		}
 
 		res[query.Key] = queryRes

+ 33 - 3
internal/templater/utils/query_test.go

@@ -14,14 +14,44 @@ type testType struct {
 
 func TestQueryValues(t *testing.T) {
 	vals := map[string]interface{}{
-		"testing": map[string]interface{}{
-			"hello": "there",
+		"items": []interface{}{
+			map[string]interface{}{
+				"metadata": map[string]interface{}{
+					"name":      "a",
+					"namespace": "velero",
+				},
+				"array": []interface{}{
+					"1",
+					"2",
+				},
+			},
+			map[string]interface{}{
+				"metadata": map[string]interface{}{
+					"name":      "b",
+					"namespace": "velero",
+				},
+				"array": []interface{}{
+					"3",
+					"4",
+				},
+			},
 		},
 	}
 
 	queries := make([]*templater.TemplateReaderQuery, 0)
 
-	query, _ := utils.NewQuery("test", `{ .testing }`)
+	// should get turned into type []map[string]interface{} that can be converted to JSON
+	// query, err := utils.NewQuery("test",
+	// 	`
+	// .items[].metadata |
+	// { name: .name, namespace: .namespace }
+	// `)
+
+	query, err := utils.NewQuery("test", `.items`)
+
+	if err != nil {
+		t.Fatalf("%v\n", err)
+	}
 
 	queries = append(queries, query)