refactor!: cobra migrate

This commit is contained in:
2024-12-26 17:53:25 +01:00
parent 0df2b15c33
commit 671e1ca276
76 changed files with 12042 additions and 2545 deletions

View File

@ -2,19 +2,17 @@ package recipe
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"path"
"text/template"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/autocomplete"
"coopcloud.tech/abra/pkg/config"
"coopcloud.tech/abra/pkg/git"
"coopcloud.tech/abra/pkg/log"
"coopcloud.tech/abra/pkg/recipe"
"github.com/urfave/cli/v3"
"github.com/spf13/cobra"
)
// recipeMetadata is the recipe metadata for the README.md
@ -31,30 +29,22 @@ type recipeMetadata struct {
SSO string
}
var recipeNewCommand = cli.Command{
Name: "new",
var RecipeNewCommand = &cobra.Command{
Use: "new <recipe> [flags]",
Aliases: []string{"n"},
Flags: []cli.Flag{
internal.GitNameFlag,
internal.GitEmailFlag,
Short: "Create a new recipe",
Long: `A community managed recipe template is used.`,
Args: cobra.ExactArgs(1),
ValidArgsFunction: func(
cmd *cobra.Command,
args []string,
toComplete string) ([]string, cobra.ShellCompDirective) {
return autocomplete.RecipeNameComplete()
},
Before: internal.SubCommandBefore,
Usage: "Create a new recipe",
UsageText: "abra recipe new <recipe> [options]",
Description: `Create a new recipe.
Run: func(cmd *cobra.Command, args []string) {
recipeName := args[0]
Abra uses the built-in example repository which is available here:
https://git.coopcloud.tech/coop-cloud/example`,
HideHelp: true,
Action: func(ctx context.Context, cmd *cli.Command) error {
recipeName := cmd.Args().First()
r := recipe.Get(recipeName)
if recipeName == "" {
internal.ShowSubcommandHelpAndError(cmd, errors.New("no recipe name provided"))
}
if _, err := os.Stat(r.Dir); !os.IsNotExist(err) {
log.Fatalf("%s recipe directory already exists?", r.Dir)
}
@ -89,14 +79,12 @@ Abra uses the built-in example repository which is available here:
}
if err := git.Init(r.Dir, true, internal.GitName, internal.GitEmail); err != nil {
if err := git.Init(r.Dir, true, gitName, gitEmail); err != nil {
log.Fatal(err)
}
log.Infof("new recipe '%s' created: %s", recipeName, path.Join(r.Dir))
log.Info("happy hacking 🎉")
return nil
},
}
@ -115,3 +103,26 @@ func newRecipeMeta(recipeName string) recipeMetadata {
SSO: "No",
}
}
var (
gitName string
gitEmail string
)
func init() {
RecipeNewCommand.Flags().StringVarP(
&gitName,
"git-name",
"N",
"",
"Git (user) name to do commits with",
)
RecipeNewCommand.Flags().StringVarP(
&gitEmail,
"git-email",
"e",
"",
"Git email name to do commits with",
)
}