char/pkg/plugins/provider/git.go

88 lines
1.7 KiB
Go
Raw Normal View History

2022-11-02 20:25:55 +01:00
package provider
import (
"context"
2022-11-02 22:49:32 +01:00
"errors"
2022-11-02 20:25:55 +01:00
"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 {
2022-11-02 22:49:32 +01:00
return fmt.Errorf("path already exists cannot create: %w", err)
2022-11-02 20:25:55 +01:00
}
for n, plugin := range plugins {
n, plugin := n, plugin
errgroup.Go(func() error {
2022-11-02 22:49:32 +01:00
dest := fmt.Sprintf(
"%s/%s",
strings.TrimRight(baseDir, "/"), n.Hash(),
2022-11-02 20:25:55 +01:00
)
2022-11-02 22:49:32 +01:00
if _, err := os.Stat(dest); errors.Is(err, os.ErrNotExist) {
log.Printf("fetching git plugin repo: %s", n)
return gpp.FetchPlugin(
ctx,
registry,
plugin,
dest,
)
}
return nil
2022-11-02 20:25:55 +01:00
})
}
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",
2022-11-02 22:49:32 +01:00
"--depth=1",
2022-11-02 20:25:55 +01:00
cloneUrl,
dest,
).CombinedOutput()
if len(output) > 0 {
log.Print(string(output))
}
if err != nil {
return err
}
return nil
}