2020-02-24 19:14:46 +03:00
|
|
|
package tables
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/metadataobject"
|
2021-06-18 20:38:29 +03:00
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
2022-03-10 11:12:55 +03:00
|
|
|
"gopkg.in/yaml.v3"
|
2020-02-24 19:14:46 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type TableConfig struct {
|
|
|
|
MetadataDir string
|
|
|
|
|
|
|
|
logger *logrus.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(ec *cli.ExecutionContext, baseDir string) *TableConfig {
|
|
|
|
return &TableConfig{
|
|
|
|
MetadataDir: baseDir,
|
|
|
|
logger: ec.Logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TableConfig) Validate() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TableConfig) CreateFiles() error {
|
|
|
|
v := make([]interface{}, 0)
|
|
|
|
data, err := yaml.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-23 12:49:44 +03:00
|
|
|
err = ioutil.WriteFile(filepath.Join(t.MetadataDir, t.Filename()), data, 0644)
|
2020-02-24 19:14:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-10 11:12:55 +03:00
|
|
|
func (t *TableConfig) Build() (map[string]interface{}, metadataobject.ErrParsingMetadataObject) {
|
2022-06-15 18:44:36 +03:00
|
|
|
data, err := metadataobject.ReadMetadataFile(filepath.Join(t.MetadataDir, t.Filename()))
|
2020-02-24 19:14:46 +03:00
|
|
|
if err != nil {
|
2022-03-10 11:12:55 +03:00
|
|
|
return nil, t.error(err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2022-03-10 11:12:55 +03:00
|
|
|
var obj []yaml.Node
|
|
|
|
err = yaml.Unmarshal(data, &obj)
|
2020-02-24 19:14:46 +03:00
|
|
|
if err != nil {
|
2022-03-10 11:12:55 +03:00
|
|
|
return nil, t.error(err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2022-03-10 11:12:55 +03:00
|
|
|
return map[string]interface{}{t.Key(): obj}, nil
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
|
2022-03-10 11:12:55 +03:00
|
|
|
func (t *TableConfig) Export(metadata map[string]yaml.Node) (map[string][]byte, metadataobject.ErrParsingMetadataObject) {
|
|
|
|
return metadataobject.DefaultExport(t, metadata, t.error, metadataobject.DefaultObjectTypeSequence)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
func (t *TableConfig) Filename() string {
|
|
|
|
return "tables.yaml"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TableConfig) Key() string {
|
2022-03-10 11:12:55 +03:00
|
|
|
return metadataobject.TablesKey
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2021-06-18 20:38:29 +03:00
|
|
|
|
2021-12-09 20:25:54 +03:00
|
|
|
func (t *TableConfig) GetFiles() ([]string, metadataobject.ErrParsingMetadataObject) {
|
|
|
|
rootFile := filepath.Join(t.BaseDirectory(), t.Filename())
|
|
|
|
files, err := metadataobject.DefaultGetFiles(rootFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, t.error(err)
|
|
|
|
}
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TableConfig) WriteDiff(opts metadataobject.WriteDiffOpts) metadataobject.ErrParsingMetadataObject {
|
|
|
|
err := metadataobject.DefaultWriteDiff(metadataobject.DefaultWriteDiffOpts{From: t, WriteDiffOpts: opts})
|
|
|
|
if err != nil {
|
|
|
|
return t.error(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TableConfig) BaseDirectory() string {
|
|
|
|
return t.MetadataDir
|
|
|
|
}
|
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
func (t *TableConfig) error(err error, additionalContext ...string) metadataobject.ErrParsingMetadataObject {
|
|
|
|
return metadataobject.NewErrParsingMetadataObject(t, err, additionalContext...)
|
2021-06-18 20:38:29 +03:00
|
|
|
}
|