fix minor bugs and add do

This commit is contained in:
Kasper Juul Hermansen 2022-11-02 22:49:32 +01:00
parent 9a0002b34f
commit 9b95267eeb
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
7 changed files with 103 additions and 12 deletions

67
cmd/char/do.go Normal file
View File

@ -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
}

View File

@ -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 {

View File

@ -12,6 +12,7 @@ func NewCharCmd(charctx *charcontext.CharContext) *cobra.Command {
cmd.AddCommand(
NewLsCommand(charctx),
NewDoCommand(charctx),
)
return cmd

10
examples/basic/testdo.sh Executable file
View File

@ -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

View File

@ -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
}

View File

@ -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 {
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,
fmt.Sprintf(
"%s/%s",
strings.TrimRight(baseDir, "/"), n.Hash(),
),
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()

View File

@ -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" {