fix minor bugs and add do
This commit is contained in:
parent
9a0002b34f
commit
9b95267eeb
67
cmd/char/do.go
Normal file
67
cmd/char/do.go
Normal 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
|
||||
}
|
@ -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 {
|
||||
|
@ -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
10
examples/basic/testdo.sh
Executable 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
|
@ -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
|
||||
}
|
||||
|
@ -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()
|
||||
|
@ -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" {
|
||||
|
Loading…
Reference in New Issue
Block a user