cli: allow relative paths in !include directives

GitOrigin-RevId: c4928e8a1e8918b1a24774ccafbcc5839574f5d7
This commit is contained in:
Aravind K P 2021-03-09 11:26:48 +05:30 committed by hasura-bot
parent 8357142001
commit d18e172662
16 changed files with 157 additions and 42 deletions

View File

@ -9,6 +9,8 @@
- server/mssql: support tracking and querying from views
- server: inherited roles for PG queries and subscription
- cli: add support for rest endpoints
- cli: support mssql sources
- cli: use relative paths in metadata !include directives
## v2.0.0-alpha.3
@ -21,7 +23,6 @@
- server/mssql: fix text values erroneously being parsed as varchar
- server: improve errors messages for inconsistent sources
- console: add relationship tab for mssql tables (#677)
- cli: support mssql sources
- build: fix the packaging of static console assets (fix #6610)

View File

@ -8,5 +8,5 @@
idle_timeout: 180
max_connections: 50
retries: 1
tables: []
functions: []
tables: "!include default/tables/tables.yaml"
functions: "!include default/functions/functions.yaml"

View File

@ -8,6 +8,5 @@
idle_timeout: 180
max_connections: 50
retries: 1
tables:
- "!include public_test.yaml"
functions: []
tables: "!include default/tables/tables.yaml"
functions: "!include default/functions/functions.yaml"

View File

@ -0,0 +1 @@
- "!include public_test.yaml"

View File

@ -7,8 +7,6 @@ import (
"os"
"path/filepath"
"github.com/hasura/graphql-engine/cli/metadata/tables"
"github.com/sirupsen/logrus"
"github.com/goccy/go-yaml"
@ -100,7 +98,7 @@ func (t *SourceConfig) Build(metadata *goyaml.MapSlice) error {
var tablesKey interface{}
err = v3yaml.Unmarshal(tableNodeBytes, newSourcesYamlDecoder(
sourcesYamlDecoderOpts{
IncludeTagBaseDirectory: filepath.Join(t.MetadataDir, sourcesDirectory, source.Name, tablesDirectory),
IncludeTagBaseDirectory: filepath.Join(t.MetadataDir, sourcesDirectory),
},
&tablesKey,
))
@ -126,7 +124,7 @@ func (t *SourceConfig) Build(metadata *goyaml.MapSlice) error {
var functionsKey interface{}
err = v3yaml.Unmarshal(functionsNodeBytes, newSourcesYamlDecoder(
sourcesYamlDecoderOpts{
IncludeTagBaseDirectory: filepath.Join(t.MetadataDir, sourcesDirectory, source.Name, functionsDirectory),
IncludeTagBaseDirectory: filepath.Join(t.MetadataDir, sourcesDirectory),
},
&functionsKey,
))
@ -240,8 +238,22 @@ func (t *SourceConfig) Export(metadata goyaml.MapSlice) (map[string][]byte, erro
functionFilePath := filepath.Join(t.MetadataDir, sourcesDirectory, source.Name, functionsDirectory, functionFileName)
files[functionFilePath] = b
}
source.Tables = tableTags
source.Functions = functionTags
tableTagsFilePath := filepath.Join(t.MetadataDir, sourcesDirectory, source.Name, tablesDirectory, "tables.yaml")
tableTagsBytes, err := yaml.Marshal(tableTags)
if err != nil {
return nil, fmt.Errorf("building contents for %v: %w", tableTagsFilePath, err)
}
files[tableTagsFilePath] = tableTagsBytes
functionsTagsFilePath := filepath.Join(t.MetadataDir, sourcesDirectory, source.Name, functionsDirectory, "functions.yaml")
functionTagsBytes, err := yaml.Marshal(functionTags)
if err != nil {
return nil, fmt.Errorf("building contents for %v: %w", functionsTagsFilePath, err)
}
files[functionsTagsFilePath] = functionTagsBytes
source.Tables = fmt.Sprintf("!include %s", filepath.Join(source.Name, tablesDirectory, "tables.yaml"))
source.Functions = fmt.Sprintf("!include %s", filepath.Join(source.Name, functionsDirectory, "functions.yaml"))
}
sourcesYamlBytes, err := yaml.Marshal(sources)
@ -249,15 +261,6 @@ func (t *SourceConfig) Export(metadata goyaml.MapSlice) (map[string][]byte, erro
return nil, err
}
files[filepath.Join(t.MetadataDir, sourcesDirectory, fileName)] = sourcesYamlBytes
// clear old tables.yaml and functions.yaml files if exists
if f, _ := os.Stat(filepath.Join(t.MetadataDir, tables.MetadataFilename)); f != nil {
os.Remove(filepath.Join(t.MetadataDir, tables.MetadataFilename))
}
if f, _ := os.Stat(filepath.Join(t.MetadataDir, tables.MetadataFilename)); f != nil {
os.Remove(filepath.Join(t.MetadataDir, tables.MetadataFilename))
}
return files, nil
}

View File

@ -51,12 +51,8 @@ func TestSourceConfig_Export(t *testing.T) {
idle_timeout: 180
max_connections: 50
retries: 1
tables:
- "!include public_t1.yaml"
- "!include public_t2.yaml"
functions:
- "!include public_get_t1.yaml"
- "!include public_get_t2.yaml"
tables: "!include default/tables/tables.yaml"
functions: "!include default/functions/functions.yaml"
`),
"metadata/databases/default/tables/public_t1.yaml": []byte(`table:
name: t1
@ -65,6 +61,12 @@ func TestSourceConfig_Export(t *testing.T) {
"metadata/databases/default/tables/public_t2.yaml": []byte(`table:
name: t2
schema: public
`),
"metadata/databases/default/tables/tables.yaml": []byte(`- "!include public_t1.yaml"
- "!include public_t2.yaml"
`),
"metadata/databases/default/functions/functions.yaml": []byte(`- "!include public_get_t1.yaml"
- "!include public_get_t2.yaml"
`),
"metadata/databases/default/functions/public_get_t1.yaml": []byte(`function:
name: get_t1

View File

@ -8,12 +8,8 @@
idle_timeout: 180
max_connections: 50
retries: 1
tables:
- !include "public_t1.yaml"
- !include "public_t2.yaml"
functions:
- !include "public_get_t1.yaml"
- !include "public_get_t2.yaml"
tables: !include "s1/tables/tables.yaml"
functions: !include "s1/functions/functions.yaml"
- name: s2
kind: postgres
configuration:
@ -24,9 +20,5 @@
idle_timeout: 180
max_connections: 50
retries: 1
tables:
- !include "public_t1.yaml"
- !include "public_t2.yaml"
functions:
- !include "public_get_t1.yaml"
- !include "public_get_t2.yaml"
tables: !include "s2/tables/tables.yaml"
functions: !include "s2/functions/functions.yaml"

View File

@ -0,0 +1,2 @@
- !include "public_get_t1.yaml"
- !include "public_get_t2.yaml"

View File

@ -0,0 +1,2 @@
- !include "public_t1.yaml"
- !include "public_t2.yaml"

View File

@ -0,0 +1,2 @@
- !include "public_get_t1.yaml"
- !include "public_get_t2.yaml"

View File

@ -0,0 +1,2 @@
- !include "public_t1.yaml"
- !include "public_t2.yaml"

View File

@ -66,7 +66,12 @@ func resolveTags(ctx map[string]string, node *yaml.Node) (*yaml.Node, error) {
if err != nil {
return nil, err
}
var f = newFragment(ctx)
newctx := map[string]string{}
for k, v := range ctx {
newctx[k] = v
}
newctx[includeTag] = filepath.Dir(filepath.Join(baseDir, node.Value))
var f = newFragment(newctx)
err = yaml.Unmarshal(file, f)
if err != nil {
return nil, err

View File

@ -1,6 +1,7 @@
package sources
import (
"io/ioutil"
"testing"
"github.com/stretchr/testify/assert"
@ -32,7 +33,14 @@ actions: !include "actions.yaml"
return v
}(),
},
"actions:\n actions: []\n custom_types:\n enums: []\n input_objects: []\n objects: []\n scalars: []\n",
`actions:
actions: []
custom_types:
enums: []
input_objects: []
objects: []
scalars: []
`,
false,
},
{
@ -48,7 +56,100 @@ actions: '!include "actions.yaml"'
return v
}(),
},
"actions:\n actions: []\n custom_types:\n enums: []\n input_objects: []\n objects: []\n scalars: []\n",
`actions:
actions: []
custom_types:
enums: []
input_objects: []
objects: []
scalars: []
`,
false,
},
{
"can resolve !include tags in strings",
args{
ctx: map[string]string{includeTag: "testdata/metadata/"},
node: func() *yaml.Node {
v := new(yaml.Node)
b := []byte(`
actions: "!include actions.yaml"
`)
assert.NoError(t, yaml.Unmarshal(b, v))
return v
}(),
},
`actions:
actions: []
custom_types:
enums: []
input_objects: []
objects: []
scalars: []
`,
false,
},
{
"can resolve !include tags with relative paths",
args{
ctx: map[string]string{includeTag: "testdata/metadata/databases"},
node: func() *yaml.Node {
v := new(yaml.Node)
b, err := ioutil.ReadFile("testdata/metadata/databases/databases.yaml")
assert.Nil(t, err)
assert.NoError(t, yaml.Unmarshal(b, v))
return v
}(),
},
`- name: s1
kind: postgres
configuration:
connection_info:
database_url:
from_env: HASURA_GRAPHQL_DATABASE_URL
pool_settings:
idle_timeout: 180
max_connections: 50
retries: 1
tables:
- table:
name: t1
schema: public
- table:
name: t2
schema: public
functions:
- function:
name: get_t1
schema: public
- function:
name: get_t2
schema: public
- name: s2
kind: postgres
configuration:
connection_info:
database_url:
from_env: HASURA_GRAPHQL_DATABASE_URL
pool_settings:
idle_timeout: 180
max_connections: 50
retries: 1
tables:
- table:
name: t1
schema: public
- table:
name: t2
schema: public
functions:
- function:
name: get_t1
schema: public
- function:
name: get_t2
schema: public
`,
false,
},
}