with about plugins

This commit is contained in:
2022-11-02 22:10:15 +01:00
parent e59a87c43a
commit c6fd666f64
13 changed files with 250 additions and 46 deletions

View File

@@ -0,0 +1,66 @@
package charcontext
import (
"context"
"log"
"git.front.kjuulh.io/kjuulh/char/pkg/plugins/provider"
"git.front.kjuulh.io/kjuulh/char/pkg/register"
"git.front.kjuulh.io/kjuulh/char/pkg/schema"
)
type CharContext struct {
contextPath string
pluginRegister *register.PluginRegister
schema *schema.CharSchema
}
func NewCharContext(ctx context.Context) (*CharContext, error) {
localPath, err := FindLocalRoot(ctx)
if err != nil {
return nil, err
}
gpp := provider.NewGitPluginProvider()
s, err := schema.ParseFile(ctx, ".char.yml")
if err != nil {
return nil, err
}
plugins, err := s.GetPlugins(ctx)
if err != nil {
return nil, err
}
err = gpp.FetchPlugins(ctx, s.Registry, plugins)
if err != nil {
return nil, err
}
builder := register.NewPluginRegisterBuilder()
for name, plugin := range plugins {
builder = builder.Add(name.Hash(), plugin.Opts.Path)
}
r, err := builder.Build(ctx)
if err != nil {
return nil, err
}
return &CharContext{
contextPath: localPath,
pluginRegister: r,
schema: s,
}, nil
}
func (cc *CharContext) Close() {
if err := cc.pluginRegister.Close(); err != nil {
log.Fatal(err)
}
}
func (cc *CharContext) About(ctx context.Context) ([]register.AboutItem, error) {
return cc.pluginRegister.About(ctx)
}

View File

@@ -0,0 +1,57 @@
package charcontext
import (
"context"
"errors"
"os"
"path"
)
var ErrNoContextFound = errors.New("could not find project root")
const CharFileName = ".char.yml"
func FindLocalRoot(ctx context.Context) (string, error) {
curdir, err := os.Getwd()
if err != nil {
return "", err
}
return recursiveFindLocalRoot(ctx, curdir)
//output, err := exec.Command("git", "rev-parse", "--show-toplevel").CombinedOutput()
//if err != nil {
// return "", err
//}
//if len(output) == 0 {
// return "", errors.New("could not find absolute path")
//}
//if _, err := os.Stat(string(output)); errors.Is(err, os.ErrNotExist) {
// return "", fmt.Errorf("path does not exist %s", string(output))
//}
//return string(output), nil
}
func recursiveFindLocalRoot(ctx context.Context, localpath string) (string, error) {
entries, err := os.ReadDir(localpath)
if err != nil {
return "", err
}
for _, entry := range entries {
if entry.Name() == CharFileName {
return localpath, nil
}
}
if localpath == "/" {
return "", ErrNoContextFound
}
return recursiveFindLocalRoot(ctx, path.Dir(localpath))
}
func ChangeToPath(_ context.Context, path string) error {
return os.Chdir(path)
}

View File

@@ -2,10 +2,18 @@ package register
import "context"
type AboutCommand struct {
Name string `json:"name" yaml:"name"`
Args []string `json:"args" yaml:"args"`
Required []string `json:"required" yaml:"required"`
}
type About struct {
Name string `json:"name"`
Version string `json:"version"`
About string `json:"about"`
Name string `json:"name"`
Version string `json:"version"`
About string `json:"about"`
Vars []string `json:"vars"`
Commands []*AboutCommand `json:"commands"`
}
type Plugin interface {

View File

@@ -164,10 +164,32 @@ func (pr *PluginRegister) Close() error {
return nil
}
type CommandAboutItem struct {
Name string
Args []string
Required []string
}
type CommandAboutItems []*CommandAboutItem
func FromAboutCommands(commands []*AboutCommand) CommandAboutItems {
cai := make(CommandAboutItems, 0)
for _, command := range commands {
cai = append(cai, &CommandAboutItem{
Name: command.Name,
Args: command.Args,
Required: command.Required,
})
}
return cai
}
type AboutItem struct {
Name string
Version string
About string
Name string
Version string
About string
Vars []string
Commands CommandAboutItems
}
func (pr *PluginRegister) About(ctx context.Context) ([]AboutItem, error) {
@@ -184,9 +206,11 @@ func (pr *PluginRegister) About(ctx context.Context) ([]AboutItem, error) {
}
list = append(list, AboutItem{
Name: about.Name,
Version: about.Version,
About: about.About,
Name: about.Name,
Version: about.Version,
About: about.About,
Vars: about.Vars,
Commands: FromAboutCommands(about.Commands),
})
return nil
})

View File

@@ -95,7 +95,10 @@ func (cspn CharSchemaPluginName) Get() (*PluginOps, error) {
}
type CharSchemaPlugins map[CharSchemaPluginName]*CharSchemaPlugin
type CharSchemaPluginVarName string
type CharSchemaPluginVars map[CharSchemaPluginVarName]string
type CharSchemaPlugin struct {
Opts *PluginOps
Vars CharSchemaPluginVars `json:"vars"`
}