Add schema

This commit is contained in:
2022-11-02 16:31:12 +01:00
parent dd225fc130
commit a5859107f1
10 changed files with 158 additions and 10 deletions

View File

@@ -1,6 +1,37 @@
package schema
import (
"context"
"errors"
"fmt"
"os"
"gopkg.in/yaml.v3"
)
type CharSchema struct {
Registry string `json:"registry" yaml:"registry"`
Plugins CharSchemaPlugins `json:"plugins" yaml:"plugins"`
}
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)
}
var schema CharSchema
if err = yaml.Unmarshal(file, &schema); err != nil {
return nil, fmt.Errorf("could not deserialize yaml file into CharSchema: %w", err)
}
return &schema, nil
}
func (cs *CharSchema) GetPlugins(ctx context.Context) (*PluginOps, error) {
return nil, nil
}

View File

@@ -1,6 +1,9 @@
package schema
import (
"context"
"errors"
"fmt"
"regexp"
"strings"
)
@@ -14,7 +17,39 @@ type PluginOps struct {
Version string
}
func (cspn CharSchemaPluginName) Get() *PluginOps {
type GitProtocol string
const (
GitProtocolHttps GitProtocol = "https"
GitProtocolSsh = "ssh"
)
type CloneUrlOpt struct {
Protocol GitProtocol
SshUser string
}
func (po *PluginOps) GetCloneUrl(ctx context.Context, registry string, opt *CloneUrlOpt) (string, error) {
if opt == nil {
return "", errors.New("opt is required")
}
switch opt.Protocol {
case GitProtocolHttps:
return fmt.Sprintf("https://%s/%s/%s.git", registry, po.Org, po.RepositoryName), nil
case GitProtocolSsh:
return fmt.Sprintf("%s@%s:%s/%s.git", opt.SshUser, registry, po.Org, po.RepositoryName), nil
default:
return "", errors.New("protocol not allowed")
}
}
var memo = map[string]*PluginOps{}
func (cspn CharSchemaPluginName) Get() (*PluginOps, error) {
if m, ok := memo[string(cspn)]; ok {
return m, nil
}
po := &PluginOps{}
reg := regexp.MustCompile(
`(?P<org>[\d\w\-_\.]+)\/(?P<repo>[\d\w\-_\.]+)(?P<path>#[\d\w\-_\.\/]+)?(?P<version>@[\d\w\-_\.\/]+)?(?P<path>#[\d\w\-_\.\/]+)?`,
@@ -43,7 +78,13 @@ func (cspn CharSchemaPluginName) Get() *PluginOps {
po.Version = strings.TrimLeft(version, "@")
}
return po
if po.Org == "" || po.RepositoryName == "" {
return nil, errors.New("could not find org or repository name")
}
memo[string(cspn)] = po
return po, nil
}
type CharSchemaPlugins map[CharSchemaPluginName]CharSchemaPlugin

View File

@@ -1,6 +1,7 @@
package schema_test
import (
"context"
"testing"
"git.front.kjuulh.io/kjuulh/char/pkg/schema"
@@ -65,8 +66,61 @@ func TestSchemaNameCanParse(t *testing.T) {
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
actual := tc.inputString.Get()
actual, _ := tc.inputString.Get()
require.Equal(t, tc.expected, *actual)
})
}
}
func TestPluginOpt(t *testing.T) {
t.Parallel()
tt := []struct {
name string
pluginOpt schema.PluginOps
cloneUrlOpt schema.CloneUrlOpt
registry string
expected string
}{
{
name: "ssh values",
pluginOpt: schema.PluginOps{
Org: "kjuulh",
RepositoryName: "char",
Path: "",
Version: "",
},
cloneUrlOpt: schema.CloneUrlOpt{
Protocol: schema.GitProtocolSsh,
SshUser: "git",
},
registry: "git.front.kjuulh.io",
expected: "git@git.front.kjuulh.io:kjuulh/char.git",
},
{
name: "https values",
pluginOpt: schema.PluginOps{
Org: "kjuulh",
RepositoryName: "char",
Path: "",
Version: "",
},
cloneUrlOpt: schema.CloneUrlOpt{
Protocol: schema.GitProtocolHttps,
},
registry: "git.front.kjuulh.io",
expected: "https://git.front.kjuulh.io/kjuulh/char.git",
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
url, err := tc.pluginOpt.GetCloneUrl(context.Background(), tc.registry, &tc.cloneUrlOpt)
require.NoError(t, err)
require.Equal(t, tc.expected, url)
})
}
}