From 9b95267eeb5170bd96ea5c561061b7f7f9f8fe0d Mon Sep 17 00:00:00 2001 From: kjuulh Date: Wed, 2 Nov 2022 22:49:32 +0100 Subject: [PATCH] fix minor bugs and add do --- cmd/char/do.go | 67 +++++++++++++++++++++++++++++++++ cmd/char/ls.go | 3 +- cmd/char/root.go | 1 + examples/basic/testdo.sh | 10 +++++ pkg/charcontext/char_context.go | 4 ++ pkg/plugins/provider/git.go | 28 +++++++++----- pkg/register/plugin_register.go | 2 +- 7 files changed, 103 insertions(+), 12 deletions(-) create mode 100644 cmd/char/do.go create mode 100755 examples/basic/testdo.sh diff --git a/cmd/char/do.go b/cmd/char/do.go new file mode 100644 index 0000000..2c0ce8b --- /dev/null +++ b/cmd/char/do.go @@ -0,0 +1,67 @@ +package char + +import ( + "context" + "log" + + "git.front.kjuulh.io/kjuulh/char/pkg/charcontext" + "github.com/spf13/cobra" +) + +type RequiredArg struct { + Required bool + Value string +} + +func NewDoCommand(charctx *charcontext.CharContext) *cobra.Command { + cmd := &cobra.Command{ + Use: "do", + } + about, err := charctx.About(context.Background()) + if err != nil { + log.Fatal(err) + } + + for _, a := range about { + for _, c := range a.Commands { + requiredArgs := make(map[string]*RequiredArg, len(c.Args)) + for _, arg := range c.Args { + requiredArgs[arg] = &RequiredArg{ + Required: false, + } + } + for _, required := range c.Required { + if _, ok := requiredArgs[required]; ok { + requiredArg := requiredArgs[required] + requiredArg.Required = true + } + } + + doCmd := &cobra.Command{ + Use: c.Name, + RunE: func(cmd *cobra.Command, args []string) error { + if err := cmd.ParseFlags(args); err != nil { + return err + } + + if err := charctx.Do(cmd.Context(), a.Name, c.Name); err != nil { + return err + } + + return nil + }, + } + + for argName, argValue := range requiredArgs { + doCmd.PersistentFlags().StringVar(&argValue.Value, argName, "", "") + if argValue.Required { + doCmd.MarkPersistentFlagRequired(argName) + } + } + + cmd.AddCommand(doCmd) + } + } + + return cmd +} diff --git a/cmd/char/ls.go b/cmd/char/ls.go index 4f50d44..8c713ce 100644 --- a/cmd/char/ls.go +++ b/cmd/char/ls.go @@ -32,10 +32,11 @@ func NewLsCommand(charctx *charcontext.CharContext) *cobra.Command { if len(a.Commands) > 0 { fmt.Println("\tCommands:") for _, ac := range a.Commands { - fmt.Printf("\t\t%s", ac) + fmt.Printf("\t\t%s\n", ac.Name) if len(ac.Args) == 0 { continue } + fmt.Println("\t\tArgs") for _, aca := range ac.Args { isrequired := false for _, acr := range ac.Required { diff --git a/cmd/char/root.go b/cmd/char/root.go index f872cf2..b29dc08 100644 --- a/cmd/char/root.go +++ b/cmd/char/root.go @@ -12,6 +12,7 @@ func NewCharCmd(charctx *charcontext.CharContext) *cobra.Command { cmd.AddCommand( NewLsCommand(charctx), + NewDoCommand(charctx), ) return cmd diff --git a/examples/basic/testdo.sh b/examples/basic/testdo.sh new file mode 100755 index 0000000..9a7f06b --- /dev/null +++ b/examples/basic/testdo.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +set -e + +go build -o char ../../main.go + +echo "base" +CHAR_DEV_MODE=true ./char do -h +echo "local_up" +CHAR_DEV_MODE=false ./char do local_up -h diff --git a/pkg/charcontext/char_context.go b/pkg/charcontext/char_context.go index ddb30c5..8da8083 100644 --- a/pkg/charcontext/char_context.go +++ b/pkg/charcontext/char_context.go @@ -64,3 +64,7 @@ func (cc *CharContext) Close() { func (cc *CharContext) About(ctx context.Context) ([]register.AboutItem, error) { return cc.pluginRegister.About(ctx) } + +func (cc *CharContext) Do(ctx context.Context, argName string, commandName string) error { + return nil +} diff --git a/pkg/plugins/provider/git.go b/pkg/plugins/provider/git.go index 54b4489..bf26be7 100644 --- a/pkg/plugins/provider/git.go +++ b/pkg/plugins/provider/git.go @@ -2,6 +2,7 @@ package provider import ( "context" + "errors" "fmt" "log" "os" @@ -27,22 +28,28 @@ func (gpp *GitPluginProvider) FetchPlugins(ctx context.Context, registry string, } } if err := os.MkdirAll(baseDir, 0755); err != nil { - return err + return fmt.Errorf("path already exists cannot create: %w", 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(), - ), + dest := fmt.Sprintf( + "%s/%s", + strings.TrimRight(baseDir, "/"), n.Hash(), ) + 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 + }) } @@ -66,6 +73,7 @@ func (gpp *GitPluginProvider) FetchPlugin(ctx context.Context, registry string, output, err := exec.Command( "git", "clone", + "--depth=1", cloneUrl, dest, ).CombinedOutput() diff --git a/pkg/register/plugin_register.go b/pkg/register/plugin_register.go index dd32646..38b60f1 100644 --- a/pkg/register/plugin_register.go +++ b/pkg/register/plugin_register.go @@ -44,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/cmd", name) + pluginPath := fmt.Sprintf(".char/plugins/%s/dist/plugin", name) _, err := os.Stat(pluginPath) if err != nil || os.Getenv("CHAR_DEV_MODE") == "true" {