mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-22 06:51:32 +03:00
bb63d7e60e
Co-authored-by: Rishichandra Wawhal <rishichandra.wawhal@gmail.com> Co-authored-by: Rikin Kachhia <54616969+rikinsk@users.noreply.github.com> Co-authored-by: Aravind <aravindkp@outlook.in> Co-authored-by: Anon Ray <ecthiender@users.noreply.github.com> Co-authored-by: Shahidh K Muhammed <muhammedshahid.k@gmail.com>
117 lines
2.6 KiB
Go
117 lines
2.6 KiB
Go
package metadata
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
"github.com/hasura/graphql-engine/cli"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type MetadataConfig struct {
|
|
MigrationsDirectory string
|
|
MetadataFiles []string
|
|
|
|
logger *logrus.Logger
|
|
}
|
|
|
|
func New(ec *cli.ExecutionContext, baseDir string) *MetadataConfig {
|
|
return &MetadataConfig{
|
|
MigrationsDirectory: baseDir,
|
|
MetadataFiles: []string{filepath.Join(baseDir, "metadata.yaml"), filepath.Join(baseDir, "metadata.json")},
|
|
logger: ec.Logger,
|
|
}
|
|
}
|
|
|
|
func (m *MetadataConfig) Validate() error {
|
|
return nil
|
|
}
|
|
|
|
func (m *MetadataConfig) CreateFiles() error {
|
|
return nil
|
|
}
|
|
|
|
func (m *MetadataConfig) Build(metadata *yaml.MapSlice) error {
|
|
var metadataContent []byte
|
|
for _, format := range []string{"yaml", "json"} {
|
|
metadataPath, err := m.GetMetadataFilePath(format)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
metadataContent, err = ioutil.ReadFile(metadataPath)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
continue
|
|
}
|
|
return err
|
|
}
|
|
break
|
|
}
|
|
if metadataContent == nil {
|
|
return errors.New("Unable to locate metadata.[yaml|json] file under migrations directory")
|
|
}
|
|
var item []yaml.MapItem
|
|
err := yaml.Unmarshal(metadataContent, &item)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*metadata = append(*metadata, item...)
|
|
return nil
|
|
}
|
|
|
|
func (m *MetadataConfig) Export(metadata yaml.MapSlice) (map[string][]byte, error) {
|
|
metaByt, err := yaml.Marshal(metadata)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "cannot marshal metadata")
|
|
}
|
|
metadataPath, err := m.GetMetadataFilePath("yaml")
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "cannot save metadata")
|
|
}
|
|
return map[string][]byte{
|
|
metadataPath: metaByt,
|
|
}, nil
|
|
}
|
|
|
|
func (m *MetadataConfig) GetMetadataFilePath(format string) (string, error) {
|
|
ext := fmt.Sprintf(".%s", format)
|
|
for _, filePath := range m.MetadataFiles {
|
|
switch p := filepath.Ext(filePath); p {
|
|
case ext:
|
|
return filePath, nil
|
|
}
|
|
}
|
|
return "", errors.New("unsupported file type")
|
|
}
|
|
|
|
func (m *MetadataConfig) Name() string {
|
|
return "singleFileMetadata"
|
|
}
|
|
|
|
// GetExistingMetadataFile returns the path to the default metadata file that
|
|
// also exists, json or yaml
|
|
func (m *MetadataConfig) GetExistingMetadataFile() (string, error) {
|
|
filename := ""
|
|
for _, format := range []string{"yaml", "json"} {
|
|
f, err := m.GetMetadataFilePath(format)
|
|
if err != nil {
|
|
return "", errors.Wrap(err, "cannot get metadata file")
|
|
}
|
|
|
|
filename = f
|
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
|
continue
|
|
}
|
|
break
|
|
}
|
|
|
|
return filename, nil
|
|
}
|