2019-09-27 08:19:24 +02:00
|
|
|
package serv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-05-27 01:41:28 +02:00
|
|
|
"html/template"
|
2019-09-27 08:19:24 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
rice "github.com/GeertJohan/go.rice"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2019-09-28 17:34:03 +02:00
|
|
|
func cmdNew(cmd *cobra.Command, args []string) {
|
2019-09-27 08:19:24 +02:00
|
|
|
if len(args) != 1 {
|
2019-11-28 07:25:46 +01:00
|
|
|
cmd.Help() //nolint: errcheck
|
2019-09-27 08:19:24 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl := newTempl(map[string]string{
|
2020-05-27 01:41:28 +02:00
|
|
|
"AppName": strings.Title(strings.Join(args, " ")),
|
|
|
|
"AppNameSlug": strings.ToLower(strings.Join(args, "_")),
|
2019-09-27 08:19:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// Create app folder and add relevant files
|
|
|
|
|
|
|
|
name := args[0]
|
|
|
|
appPath := path.Join("./", name)
|
|
|
|
|
|
|
|
ifNotExists(appPath, func(p string) error {
|
|
|
|
return os.Mkdir(p, os.ModePerm)
|
|
|
|
})
|
|
|
|
|
2019-09-28 17:34:03 +02:00
|
|
|
ifNotExists(path.Join(appPath, "Dockerfile"), func(p string) error {
|
|
|
|
if v, err := tmpl.get("Dockerfile"); err == nil {
|
2019-09-27 08:19:24 +02:00
|
|
|
return ioutil.WriteFile(p, v, 0644)
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
ifNotExists(path.Join(appPath, "docker-compose.yml"), func(p string) error {
|
|
|
|
if v, err := tmpl.get("docker-compose.yml"); err == nil {
|
|
|
|
return ioutil.WriteFile(p, v, 0644)
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-05-18 01:16:40 +02:00
|
|
|
ifNotExists(path.Join(appPath, "cloudbuild.yaml"), func(p string) error {
|
|
|
|
if v, err := tmpl.get("cloudbuild.yaml"); err == nil {
|
|
|
|
return ioutil.WriteFile(p, v, 0644)
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-09-27 08:19:24 +02:00
|
|
|
// Create app config folder and add relevant files
|
|
|
|
|
|
|
|
appConfigPath := path.Join(appPath, "config")
|
|
|
|
|
|
|
|
ifNotExists(appConfigPath, func(p string) error {
|
|
|
|
return os.Mkdir(p, os.ModePerm)
|
|
|
|
})
|
|
|
|
|
|
|
|
ifNotExists(path.Join(appConfigPath, "dev.yml"), func(p string) error {
|
|
|
|
if v, err := tmpl.get("dev.yml"); err == nil {
|
|
|
|
return ioutil.WriteFile(p, v, 0644)
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
ifNotExists(path.Join(appConfigPath, "prod.yml"), func(p string) error {
|
|
|
|
if v, err := tmpl.get("prod.yml"); err == nil {
|
|
|
|
return ioutil.WriteFile(p, v, 0644)
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-09-28 17:34:03 +02:00
|
|
|
ifNotExists(path.Join(appConfigPath, "seed.js"), func(p string) error {
|
2019-09-29 21:20:59 +02:00
|
|
|
if v, err := tmpl.get("seed.js"); err == nil {
|
2019-09-28 17:34:03 +02:00
|
|
|
return ioutil.WriteFile(p, v, 0644)
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-06-15 09:06:50 +02:00
|
|
|
ifNotExists(path.Join(appConfigPath, "allow.list"), func(p string) error {
|
|
|
|
return ioutil.WriteFile(p, []byte{}, 0644)
|
|
|
|
})
|
|
|
|
|
2019-09-27 08:19:24 +02:00
|
|
|
// Create app migrations folder and add relevant files
|
|
|
|
|
|
|
|
appMigrationsPath := path.Join(appConfigPath, "migrations")
|
|
|
|
|
|
|
|
ifNotExists(appMigrationsPath, func(p string) error {
|
|
|
|
return os.Mkdir(p, os.ModePerm)
|
|
|
|
})
|
|
|
|
|
2020-01-15 05:16:55 +01:00
|
|
|
ifNotExists(path.Join(appMigrationsPath, "0_init.sql"), func(p string) error {
|
|
|
|
if v, err := tmpl.get("0_init.sql"); err == nil {
|
2019-09-27 08:19:24 +02:00
|
|
|
return ioutil.WriteFile(p, v, 0644)
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
log.Printf("INR app '%s' initialized", name)
|
2019-09-27 08:19:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Templ struct {
|
|
|
|
*rice.Box
|
|
|
|
data map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTempl(data map[string]string) *Templ {
|
2020-04-10 08:27:43 +02:00
|
|
|
return &Templ{rice.MustFindBox("./tmpl"), data}
|
2019-09-27 08:19:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Templ) get(name string) ([]byte, error) {
|
|
|
|
v := t.MustString(name)
|
|
|
|
b := bytes.Buffer{}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
tmpl, err := template.New(name).Parse(v)
|
2019-11-28 07:25:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
if err := tmpl.Execute(&b, t.data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-27 08:19:24 +02:00
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ifNotExists(filePath string, doFn func(string) error) {
|
|
|
|
_, err := os.Stat(filePath)
|
|
|
|
|
|
|
|
if err == nil {
|
2020-04-10 08:27:43 +02:00
|
|
|
log.Printf("ERR create skipped '%s' exists", filePath)
|
2019-09-27 08:19:24 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-28 07:25:46 +01:00
|
|
|
if !os.IsNotExist(err) {
|
2020-04-10 08:27:43 +02:00
|
|
|
log.Fatalf("ERR unable to check if '%s' exists", filePath)
|
2019-09-27 08:19:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = doFn(filePath)
|
|
|
|
if err != nil {
|
2020-04-10 08:27:43 +02:00
|
|
|
log.Fatalf("ERR unable to create '%s'", filePath)
|
2019-09-27 08:19:24 +02:00
|
|
|
}
|
2019-10-06 22:28:10 +02:00
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
log.Printf("INR created '%s'", filePath)
|
2019-09-27 08:19:24 +02:00
|
|
|
}
|