diff --git a/pkg/schema/schema.go b/pkg/schema/schema.go index 76372ee..8c2d0ba 100644 --- a/pkg/schema/schema.go +++ b/pkg/schema/schema.go @@ -24,14 +24,27 @@ func ParseFile(ctx context.Context, path string) (*CharSchema, error) { return nil, fmt.Errorf("could not read file: %w", err) } + return Parse(file) +} + +func Parse(content []byte) (*CharSchema, error) { var schema CharSchema - if err = yaml.Unmarshal(file, &schema); err != nil { - return nil, fmt.Errorf("could not deserialize yaml file into CharSchema: %w", err) + if err := yaml.Unmarshal(content, &schema); err != nil { + return nil, fmt.Errorf("could not deserialize yaml into CharSchema: %w", err) } return &schema, nil } -func (cs *CharSchema) GetPlugins(ctx context.Context) (*PluginOps, error) { - return nil, nil +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 } diff --git a/pkg/schema/schema_plugin.go b/pkg/schema/schema_plugin.go index 57e9764..6f083c9 100644 --- a/pkg/schema/schema_plugin.go +++ b/pkg/schema/schema_plugin.go @@ -87,7 +87,8 @@ func (cspn CharSchemaPluginName) Get() (*PluginOps, error) { return po, nil } -type CharSchemaPlugins map[CharSchemaPluginName]CharSchemaPlugin +type CharSchemaPlugins map[CharSchemaPluginName]*CharSchemaPlugin type CharSchemaPlugin struct { + Opts *PluginOps } diff --git a/pkg/schema/schema_test.go b/pkg/schema/schema_test.go new file mode 100644 index 0000000..2e5187c --- /dev/null +++ b/pkg/schema/schema_test.go @@ -0,0 +1,93 @@ +package schema_test + +import ( + "context" + "testing" + + "git.front.kjuulh.io/kjuulh/char/pkg/schema" + "github.com/stretchr/testify/require" +) + +func TestSchemaParse(t *testing.T) { + t.Parallel() + tt := []struct { + name string + input string + expected *schema.CharSchema + }{ + { + name: "with plugins", + input: ` +registry: git.front.kjuulh.io +plugins: + "kjuulh/char#plugins/gocli": {} + "kjuulh/char#plugins/rust": {} +`, + expected: &schema.CharSchema{ + Registry: "git.front.kjuulh.io", + Plugins: map[schema.CharSchemaPluginName]*schema.CharSchemaPlugin{ + "kjuulh/char#plugins/gocli": {}, + "kjuulh/char#plugins/rust": {}, + }, + }, + }, + } + + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + s, err := schema.Parse([]byte(tc.input)) + + require.NoError(t, err) + require.Equal(t, tc.expected, s) + }) + } +} + +func TestGetPlugins(t *testing.T) { + t.Parallel() + tt := []struct { + name string + input string + expected schema.CharSchemaPlugins + }{ + { + name: "with plugins", + input: ` +registry: git.front.kjuulh.io +plugins: + "kjuulh/char#plugins/gocli": {} + "kjuulh/char#plugins/rust": {} +`, + expected: map[schema.CharSchemaPluginName]*schema.CharSchemaPlugin{ + "kjuulh/char#plugins/gocli": { + Opts: &schema.PluginOps{ + Org: "kjuulh", + RepositoryName: "char", + Path: "plugins/gocli", + Version: "", + }, + }, + "kjuulh/char#plugins/rust": { + Opts: &schema.PluginOps{ + Org: "kjuulh", + RepositoryName: "char", + Path: "plugins/rust", + Version: "", + }, + }, + }, + }, + } + + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + s, err := schema.Parse([]byte(tc.input)) + require.NoError(t, err) + + plugins, err := s.GetPlugins(context.Background()) + require.NoError(t, err) + + require.Equal(t, tc.expected, plugins) + }) + } +}