add git clone

This commit is contained in:
Kasper Juul Hermansen 2022-11-02 20:25:55 +01:00
parent ebc2d7aa8f
commit bace568ad5
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
12 changed files with 125 additions and 22 deletions

View File

@ -3,27 +3,42 @@ package char
import (
"fmt"
"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"
"github.com/spf13/cobra"
)
func NewLsCommand() *cobra.Command {
gpp := provider.NewGitPluginProvider()
cmd := &cobra.Command{
Use: "ls",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
_, err := schema.ParseFile(ctx, ".char.yml")
s, err := schema.ParseFile(ctx, ".char.yml")
if err != nil {
return err
}
r, err := register.
NewPluginRegisterBuilder().
Add("gocli", "plugins/gocli/main.go").
Add("rust", "plugins/rust/main.go").
Build(ctx)
plugins, err := s.GetPlugins(ctx)
if err != nil {
return err
}
err = gpp.FetchPlugins(ctx, s.Registry, plugins)
if err != nil {
return 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 err
}

View File

@ -1 +0,0 @@
../../../../

View File

@ -1 +0,0 @@
../../../../

1
examples/basic/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.char/plugins/

Binary file not shown.

View File

@ -0,0 +1,79 @@
package provider
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"strings"
"git.front.kjuulh.io/kjuulh/char/pkg/schema"
"golang.org/x/sync/errgroup"
)
type GitPluginProvider struct{}
func NewGitPluginProvider() *GitPluginProvider {
return &GitPluginProvider{}
}
func (gpp *GitPluginProvider) FetchPlugins(ctx context.Context, registry string, plugins schema.CharSchemaPlugins) error {
errgroup, ctx := errgroup.WithContext(ctx)
baseDir := ".char/plugins"
if os.Getenv("CHAR_DEV_MODE") == "true" {
if err := os.RemoveAll(baseDir); err != nil {
return err
}
}
if err := os.MkdirAll(baseDir, 0755); err != nil {
return err
}
for n, plugin := range plugins {
n, plugin := n, plugin
errgroup.Go(func() error {
log.Printf("fetching git plugin repo: %s", n)
return gpp.FetchPlugin(
ctx,
registry,
plugin,
fmt.Sprintf(
"%s/%s",
strings.TrimRight(baseDir, "/"), n.Hash(),
),
)
})
}
if err := errgroup.Wait(); err != nil {
return err
}
return nil
}
func (gpp *GitPluginProvider) FetchPlugin(ctx context.Context, registry string, plugin *schema.CharSchemaPlugin, dest string) error {
cloneUrl, err := plugin.Opts.GetCloneUrl(ctx, registry, &schema.CloneUrlOpt{
Protocol: schema.GitProtocolSsh,
SshUser: "git",
},
)
if err != nil {
return err
}
output, err := exec.Command(
"git",
"clone",
cloneUrl,
dest,
).CombinedOutput()
if len(output) > 0 {
log.Print(string(output))
}
if err != nil {
return err
}
return nil
}

View File

@ -12,7 +12,7 @@ type PluginBuilder struct {
serveConfig *plugin.ServeConfig
}
func NewPluginBuilder(name string, p Plugin) *PluginBuilder {
func NewPluginBuilder(p Plugin) *PluginBuilder {
logger := hclog.New(&hclog.LoggerOptions{
Level: hclog.Error,
Output: os.Stderr,
@ -20,7 +20,7 @@ func NewPluginBuilder(name string, p Plugin) *PluginBuilder {
})
var pluginMap = map[string]plugin.Plugin{
name: &PluginAPI{
"plugin": &PluginAPI{
Impl: p,
},
}

View File

@ -7,6 +7,7 @@ import (
"log"
"os"
"os/exec"
"strings"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
@ -43,7 +44,7 @@ func (pr *PluginRegisterBuilder) Build(ctx context.Context) (*PluginRegister, er
name, p := name, p
errgroup.Go(func() error {
pluginPath := fmt.Sprintf(".char/plugins/%s/dist/%s", name, name)
pluginPath := fmt.Sprintf(".char/plugins/%s/dist/cmd", name)
_, err := os.Stat(pluginPath)
if err != nil || os.Getenv("CHAR_DEV_MODE") == "true" {
@ -52,10 +53,15 @@ func (pr *PluginRegisterBuilder) Build(ctx context.Context) (*PluginRegister, er
"sh",
"-c",
fmt.Sprintf(
"(cd .char/plugins/%s; go build -o dist/%s %s)",
name,
"(cd .char/plugins/%s; go build -o dist/plugin %s/main.go)",
name,
strings.TrimSuffix(
strings.TrimSuffix(
p.path,
"main.go",
),
"/",
),
),
)
output, err := cmd.CombinedOutput()
@ -80,8 +86,7 @@ func (pr *PluginRegisterBuilder) Build(ctx context.Context) (*PluginRegister, er
}),
Cmd: exec.Command(
fmt.Sprintf(
".char/plugins/%s/dist/%s",
name,
".char/plugins/%s/dist/plugin",
name,
),
),
@ -95,7 +100,7 @@ func (pr *PluginRegisterBuilder) Build(ctx context.Context) (*PluginRegister, er
return err
}
raw, err := rpcClient.Dispense(name)
raw, err := rpcClient.Dispense("plugin")
if err != nil {
return err
}

View File

@ -2,6 +2,8 @@ package schema
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"regexp"
@ -10,6 +12,11 @@ import (
type CharSchemaPluginName string
func (cspn CharSchemaPluginName) Hash() string {
bytes := sha256.Sum256([]byte(cspn))
return hex.EncodeToString(bytes[:])
}
type PluginOps struct {
Org string
RepositoryName string

View File

@ -55,16 +55,16 @@ func TestGetPlugins(t *testing.T) {
input: `
registry: git.front.kjuulh.io
plugins:
"kjuulh/char#plugins/gocli": {}
"kjuulh/char#plugins/gocli@v1.9.0": {}
"kjuulh/char#plugins/rust": {}
`,
expected: map[schema.CharSchemaPluginName]*schema.CharSchemaPlugin{
"kjuulh/char#plugins/gocli": {
"kjuulh/char#plugins/gocli@v1.9.0": {
Opts: &schema.PluginOps{
Org: "kjuulh",
RepositoryName: "char",
Path: "plugins/gocli",
Version: "",
Version: "v1.9.0",
},
},
"kjuulh/char#plugins/rust": {

View File

@ -22,7 +22,6 @@ var _ register.Plugin = &GoCliPlugin{}
func main() {
if err := register.
NewPluginBuilder(
"gocli",
&GoCliPlugin{},
).
Serve(context.Background()); err != nil {

View File

@ -22,7 +22,6 @@ var _ register.Plugin = &GoCliPlugin{}
func main() {
if err := register.
NewPluginBuilder(
"rust",
&GoCliPlugin{},
).
Serve(context.Background()); err != nil {