char/pkg/schema/schema.go

51 lines
1.1 KiB
Go
Raw Normal View History

2022-11-02 01:56:57 +01:00
package schema
2022-11-02 16:31:12 +01:00
import (
"context"
"errors"
"fmt"
"os"
"gopkg.in/yaml.v3"
)
2022-11-02 01:56:57 +01:00
type CharSchema struct {
Registry string `json:"registry" yaml:"registry"`
Plugins CharSchemaPlugins `json:"plugins" yaml:"plugins"`
}
2022-11-02 16:31:12 +01:00
func ParseFile(ctx context.Context, path string) (*CharSchema, error) {
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("could not parse file, as it is not found or permitted: %s", path)
}
file, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("could not read file: %w", err)
}
2022-11-02 16:48:33 +01:00
return Parse(file)
}
func Parse(content []byte) (*CharSchema, error) {
2022-11-02 16:31:12 +01:00
var schema CharSchema
2022-11-02 16:48:33 +01:00
if err := yaml.Unmarshal(content, &schema); err != nil {
return nil, fmt.Errorf("could not deserialize yaml into CharSchema: %w", err)
2022-11-02 16:31:12 +01:00
}
return &schema, nil
}
2022-11-02 16:48:33 +01:00
func (cs *CharSchema) GetPlugins(ctx context.Context) (CharSchemaPlugins, error) {
plugins := make(map[CharSchemaPluginName]*CharSchemaPlugin, len(cs.Plugins))
for n, plugin := range cs.Plugins {
po, err := n.Get()
if err != nil {
return nil, err
}
plugin.Opts = po
plugins[n] = plugin
}
return plugins, nil
2022-11-02 16:31:12 +01:00
}