package recipe import ( "errors" "fmt" "os" "path" "text/template" "coopcloud.tech/abra/cli/internal" "coopcloud.tech/abra/pkg/config" "coopcloud.tech/abra/pkg/git" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" ) var recipeNewCommand = &cli.Command{ Name: "new", Usage: "Create a new recipe", Aliases: []string{"n"}, ArgsUsage: "", Description: ` This command creates a new recipe. Abra uses our built-in example repository which is available here: https://git.coopcloud.tech/coop-cloud/example Files within the example repository make use of the Golang templating system which Abra uses to inject values into the generated recipe folder (e.g. name of recipe and domain in the sample environment config). The new example repository is cloned to ~/.abra/apps/. `, Action: func(c *cli.Context) error { recipeName := c.Args().First() if recipeName == "" { internal.ShowSubcommandHelpAndError(c, errors.New("no recipe provided")) } directory := path.Join(config.APPS_DIR, recipeName) if _, err := os.Stat(directory); !os.IsNotExist(err) { logrus.Fatalf("%s recipe directory already exists?", directory) } url := fmt.Sprintf("%s/example.git", config.REPOS_BASE_URL) if err := git.Clone(directory, url); err != nil { logrus.Fatal(err) } gitRepo := path.Join(config.APPS_DIR, recipeName, ".git") if err := os.RemoveAll(gitRepo); err != nil { logrus.Fatal(err) } logrus.Debugf("removed git repo in %s", gitRepo) toParse := []string{ path.Join(config.APPS_DIR, recipeName, "README.md"), path.Join(config.APPS_DIR, recipeName, ".env.sample"), path.Join(config.APPS_DIR, recipeName, ".drone.yml"), } for _, path := range toParse { file, err := os.OpenFile(path, os.O_RDWR, 0664) if err != nil { logrus.Fatal(err) } tpl, err := template.ParseFiles(path) if err != nil { logrus.Fatal(err) } // TODO: ask for description and probably other things so that the // template repository is more "ready" to go than the current best-guess // mode of templating if err := tpl.Execute(file, struct { Name string Description string }{recipeName, "TODO"}); err != nil { logrus.Fatal(err) } } newGitRepo := path.Join(config.APPS_DIR, recipeName) if err := git.Init(newGitRepo, true); err != nil { logrus.Fatal(err) } logrus.Infof( "new recipe %s created in %s, happy hacking!\n", recipeName, path.Join(config.APPS_DIR, recipeName), ) return nil }, }