2020-06-16 12:08:36 +03:00
|
|
|
package crontriggers
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/version"
|
2020-06-16 12:08:36 +03:00
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
2020-06-16 12:08:36 +03:00
|
|
|
"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
|
|
|
|
}
|
2021-07-23 12:49:44 +03:00
|
|
|
err = ioutil.WriteFile(filepath.Join(c.MetadataDir, c.Filename()), data, 0644)
|
2020-06-16 12:08:36 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
func (c *CronTriggers) Build(metadata *yaml.MapSlice) metadataobject.ErrParsingMetadataObject {
|
|
|
|
data, err := ioutil.ReadFile(filepath.Join(c.MetadataDir, c.Filename()))
|
2020-06-16 12:08:36 +03:00
|
|
|
if err != nil {
|
2021-07-23 12:49:44 +03:00
|
|
|
return c.error(err)
|
2020-06-16 12:08:36 +03:00
|
|
|
}
|
|
|
|
|
2021-05-14 22:09:01 +03:00
|
|
|
var obj []yaml.MapSlice
|
|
|
|
err = yaml.Unmarshal(data, &obj)
|
2020-06-16 12:08:36 +03:00
|
|
|
if err != nil {
|
2021-07-23 12:49:44 +03:00
|
|
|
return c.error(err)
|
2020-06-16 12:08:36 +03:00
|
|
|
}
|
2021-05-14 22:09:01 +03:00
|
|
|
if len(obj) > 0 {
|
|
|
|
item := yaml.MapItem{
|
2021-07-23 12:49:44 +03:00
|
|
|
Key: c.Key(),
|
2021-05-14 22:09:01 +03:00
|
|
|
Value: obj,
|
|
|
|
}
|
|
|
|
*metadata = append(*metadata, item)
|
|
|
|
}
|
2020-06-16 12:08:36 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
func (c *CronTriggers) Export(metadata yaml.MapSlice) (map[string][]byte, metadataobject.ErrParsingMetadataObject) {
|
2020-06-16 12:08:36 +03:00
|
|
|
var cronTriggers interface{}
|
|
|
|
for _, item := range metadata {
|
|
|
|
k, ok := item.Key.(string)
|
2021-07-23 12:49:44 +03:00
|
|
|
if !ok || k != c.Key() {
|
2020-06-16 12:08:36 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
cronTriggers = item.Value
|
|
|
|
}
|
|
|
|
if cronTriggers == nil {
|
|
|
|
cronTriggers = make([]interface{}, 0)
|
|
|
|
}
|
|
|
|
data, err := yaml.Marshal(cronTriggers)
|
|
|
|
if err != nil {
|
2021-07-23 12:49:44 +03:00
|
|
|
return nil, c.error(err)
|
2020-06-16 12:08:36 +03:00
|
|
|
}
|
|
|
|
return map[string][]byte{
|
2021-07-29 10:21:25 +03:00
|
|
|
filepath.ToSlash(filepath.Join(c.MetadataDir, c.Filename())): data,
|
2020-06-16 12:08:36 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
func (c *CronTriggers) Key() string {
|
|
|
|
return "cron_triggers"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CronTriggers) Filename() string {
|
|
|
|
return "cron_triggers.yaml"
|
2020-06-16 12:08:36 +03:00
|
|
|
}
|
2021-06-18 20:38:29 +03:00
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
func (c *CronTriggers) error(err error, additionalContext ...string) metadataobject.ErrParsingMetadataObject {
|
|
|
|
return metadataobject.NewErrParsingMetadataObject(c, err, additionalContext...)
|
2021-06-18 20:38:29 +03:00
|
|
|
}
|