1
0
mirror of https://github.com/hasura/graphql-engine.git synced 2024-12-16 09:51:59 +03:00
graphql-engine/cli/internal/metadataobject/cron_triggers/cron_triggers.go
Divi 80846d36b7 cli: add support for query_tags metadata object
- add support for query_tags metadata object
- fix filename for cron_triggers metadata object

https://github.com/hasura/graphql-engine-mono/pull/1901

Co-authored-by: Aravind K P <8335904+scriptonist@users.noreply.github.com>
GitOrigin-RevId: d9266d723f06d11d92f156f70660e0122412e41a
2021-07-29 07:22:25 +00:00

101 lines
2.2 KiB
Go

package crontriggers
import (
"io/ioutil"
"path/filepath"
"github.com/hasura/graphql-engine/cli/v2/internal/metadataobject"
"github.com/hasura/graphql-engine/cli/v2/version"
"github.com/hasura/graphql-engine/cli/v2"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
type CronTriggers struct {
MetadataDir string
logger *logrus.Logger
serverFeatureFlags *version.ServerFeatureFlags
}
func New(ec *cli.ExecutionContext, baseDir string) *CronTriggers {
return &CronTriggers{
MetadataDir: baseDir,
logger: ec.Logger,
serverFeatureFlags: ec.Version.ServerFeatureFlags,
}
}
func (c *CronTriggers) Validate() error {
return nil
}
func (c *CronTriggers) CreateFiles() error {
v := make([]interface{}, 0)
data, err := yaml.Marshal(v)
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Join(c.MetadataDir, c.Filename()), data, 0644)
if err != nil {
return err
}
return nil
}
func (c *CronTriggers) Build(metadata *yaml.MapSlice) metadataobject.ErrParsingMetadataObject {
data, err := ioutil.ReadFile(filepath.Join(c.MetadataDir, c.Filename()))
if err != nil {
return c.error(err)
}
var obj []yaml.MapSlice
err = yaml.Unmarshal(data, &obj)
if err != nil {
return c.error(err)
}
if len(obj) > 0 {
item := yaml.MapItem{
Key: c.Key(),
Value: obj,
}
*metadata = append(*metadata, item)
}
return nil
}
func (c *CronTriggers) Export(metadata yaml.MapSlice) (map[string][]byte, metadataobject.ErrParsingMetadataObject) {
var cronTriggers interface{}
for _, item := range metadata {
k, ok := item.Key.(string)
if !ok || k != c.Key() {
continue
}
cronTriggers = item.Value
}
if cronTriggers == nil {
cronTriggers = make([]interface{}, 0)
}
data, err := yaml.Marshal(cronTriggers)
if err != nil {
return nil, c.error(err)
}
return map[string][]byte{
filepath.ToSlash(filepath.Join(c.MetadataDir, c.Filename())): data,
}, nil
}
func (c *CronTriggers) Key() string {
return "cron_triggers"
}
func (c *CronTriggers) Filename() string {
return "cron_triggers.yaml"
}
func (c *CronTriggers) error(err error, additionalContext ...string) metadataobject.ErrParsingMetadataObject {
return metadataobject.NewErrParsingMetadataObject(c, err, additionalContext...)
}