forked from toolshed/abra
refactor: match app/recipe new instead of create
This commit is contained in:
80
cli/recipe/new.go
Normal file
80
cli/recipe/new.go
Normal file
@ -0,0 +1,80 @@
|
||||
package recipe
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"text/template"
|
||||
|
||||
"coopcloud.tech/abra/cli/internal"
|
||||
"coopcloud.tech/abra/pkg/config"
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var recipeNewCommand = &cli.Command{
|
||||
Name: "new",
|
||||
Usage: "Create a new recipe",
|
||||
Aliases: []string{"n"},
|
||||
ArgsUsage: "<recipe>",
|
||||
Action: func(c *cli.Context) error {
|
||||
recipe := internal.ValidateRecipe(c)
|
||||
|
||||
directory := path.Join(config.APPS_DIR, recipe.Name)
|
||||
if _, err := os.Stat(directory); !os.IsNotExist(err) {
|
||||
logrus.Fatalf("'%s' recipe directory already exists?", directory)
|
||||
return nil
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/example.git", config.REPOS_BASE_URL)
|
||||
_, err := git.PlainClone(directory, false, &git.CloneOptions{URL: url, Tags: git.AllTags})
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
gitRepo := path.Join(config.APPS_DIR, recipe.Name, ".git")
|
||||
if err := os.RemoveAll(gitRepo); err != nil {
|
||||
logrus.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
toParse := []string{
|
||||
path.Join(config.APPS_DIR, recipe.Name, "README.md"),
|
||||
path.Join(config.APPS_DIR, recipe.Name, ".env.sample"),
|
||||
path.Join(config.APPS_DIR, recipe.Name, ".drone.yml"),
|
||||
}
|
||||
for _, path := range toParse {
|
||||
file, err := os.OpenFile(path, os.O_RDWR, 0755)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
tpl, err := template.ParseFiles(path)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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
|
||||
}{recipe.Name, "TODO"}); err != nil {
|
||||
logrus.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
logrus.Infof(
|
||||
"New recipe '%s' created in %s, happy hacking!\n",
|
||||
recipe.Name, path.Join(config.APPS_DIR, recipe.Name),
|
||||
)
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
Reference in New Issue
Block a user