graphql-engine/cli/internal/metadataobject/functions/v3metadata.go
Aravind K P b906410b20 cli: fix missing usages of errors package
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7424
GitOrigin-RevId: 59fccdd90437fc39e4c2cd2581676b67dad87ac9
2023-01-16 09:42:31 +00:00

56 lines
1.4 KiB
Go

package functions
import (
"bytes"
"path/filepath"
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
"github.com/hasura/graphql-engine/cli/v2/internal/metadataobject"
"github.com/hasura/graphql-engine/cli/v2"
"gopkg.in/yaml.v3"
)
type V3MetadataFunctionConfig struct {
*FunctionConfig
}
func NewV3MetadataFunctionConfig(ec *cli.ExecutionContext, baseDir string) *V3MetadataFunctionConfig {
return &V3MetadataFunctionConfig{
&FunctionConfig{
MetadataDir: baseDir,
logger: ec.Logger,
},
}
}
func (t *V3MetadataFunctionConfig) Export(md map[string]yaml.Node) (map[string][]byte, error) {
var op errors.Op = "functions.V3MetadataFunctionConfig.Export"
metadataBytes, err := yaml.Marshal(md)
if err != nil {
return nil, errors.E(op, t.error(err))
}
var metadata struct {
Sources []struct {
Name string `yaml:"name"`
Functions []yaml.Node `yaml:"functions"`
} `yaml:"sources"`
}
var functions []yaml.Node
if err := yaml.Unmarshal(metadataBytes, &metadata); err != nil {
return nil, errors.E(op, t.error(err))
}
if len(metadata.Sources) > 0 {
// use functions of first source
functions = metadata.Sources[0].Functions
}
var buf bytes.Buffer
err = metadataobject.GetEncoder(&buf).Encode(functions)
if err != nil {
return nil, errors.E(op, t.error(err))
}
return map[string][]byte{
filepath.ToSlash(filepath.Join(t.MetadataDir, t.Filename())): buf.Bytes(),
}, nil
}