2021-09-05 20:33:07 +00:00
|
|
|
package recipe
|
|
|
|
|
|
|
|
import (
|
2021-11-02 12:29:58 +00:00
|
|
|
"errors"
|
2021-09-05 20:33:07 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"coopcloud.tech/abra/cli/internal"
|
|
|
|
"coopcloud.tech/abra/pkg/config"
|
2021-09-10 22:54:02 +00:00
|
|
|
"coopcloud.tech/abra/pkg/git"
|
2021-09-05 20:33:07 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2021-09-07 05:31:11 +00:00
|
|
|
var recipeNewCommand = &cli.Command{
|
|
|
|
Name: "new",
|
2021-09-05 20:33:07 +00:00
|
|
|
Usage: "Create a new recipe",
|
2021-09-07 05:31:11 +00:00
|
|
|
Aliases: []string{"n"},
|
2021-09-05 20:33:07 +00:00
|
|
|
ArgsUsage: "<recipe>",
|
2021-11-02 12:29:58 +00:00
|
|
|
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/<recipe>.
|
|
|
|
`,
|
2021-09-05 20:33:07 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2021-11-02 12:29:58 +00:00
|
|
|
recipeName := c.Args().First()
|
|
|
|
|
|
|
|
if recipeName == "" {
|
|
|
|
internal.ShowSubcommandHelpAndError(c, errors.New("no recipe provided"))
|
|
|
|
}
|
2021-09-05 20:33:07 +00:00
|
|
|
|
2021-11-02 12:29:58 +00:00
|
|
|
directory := path.Join(config.APPS_DIR, recipeName)
|
2021-09-05 20:33:07 +00:00
|
|
|
if _, err := os.Stat(directory); !os.IsNotExist(err) {
|
2021-12-22 01:50:16 +00:00
|
|
|
logrus.Fatalf("%s recipe directory already exists?", directory)
|
2021-09-05 20:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
url := fmt.Sprintf("%s/example.git", config.REPOS_BASE_URL)
|
2021-09-10 22:54:02 +00:00
|
|
|
if err := git.Clone(directory, url); err != nil {
|
2021-10-25 08:42:39 +00:00
|
|
|
logrus.Fatal(err)
|
2021-09-05 20:33:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 12:29:58 +00:00
|
|
|
gitRepo := path.Join(config.APPS_DIR, recipeName, ".git")
|
2021-09-05 20:33:07 +00:00
|
|
|
if err := os.RemoveAll(gitRepo); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-12-22 01:50:16 +00:00
|
|
|
logrus.Debugf("removed git repo in %s", gitRepo)
|
2021-09-05 20:33:07 +00:00
|
|
|
|
|
|
|
toParse := []string{
|
2021-11-02 12:29:58 +00:00
|
|
|
path.Join(config.APPS_DIR, recipeName, "README.md"),
|
|
|
|
path.Join(config.APPS_DIR, recipeName, ".env.sample"),
|
|
|
|
path.Join(config.APPS_DIR, recipeName, ".drone.yml"),
|
2021-09-05 20:33:07 +00:00
|
|
|
}
|
|
|
|
for _, path := range toParse {
|
2021-12-22 19:08:15 +00:00
|
|
|
file, err := os.OpenFile(path, os.O_RDWR, 0664)
|
2021-09-05 20:33:07 +00:00
|
|
|
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
|
2021-11-02 12:29:58 +00:00
|
|
|
}{recipeName, "TODO"}); err != nil {
|
2021-09-05 20:33:07 +00:00
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 17:54:44 +00:00
|
|
|
newGitRepo := path.Join(config.APPS_DIR, recipeName)
|
|
|
|
if err := git.Init(newGitRepo, true); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-09-05 20:33:07 +00:00
|
|
|
logrus.Infof(
|
2021-12-22 01:50:16 +00:00
|
|
|
"new recipe %s created in %s, happy hacking!\n",
|
2021-11-02 12:29:58 +00:00
|
|
|
recipeName, path.Join(config.APPS_DIR, recipeName),
|
2021-09-05 20:33:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|