Add events #777

Merged
konrad merged 47 commits from feature/events into master 2021-02-02 22:48:38 +00:00
1 changed files with 53 additions and 3 deletions
Showing only changes of commit 5dac13c6c8 - Show all commits

View File

@ -24,6 +24,7 @@ import (
"context"
"crypto/sha256"
"fmt"
"github.com/iancoleman/strcase"
"io"
"io/ioutil"
"os"
@ -294,6 +295,13 @@ func moveFile(src, dst string) error {
return nil
}
const InfoColor = "\033[1;32m%s\033[0m"
func printSuccess(text string, args ...interface{}) {
text = fmt.Sprintf(text, args...)
fmt.Printf(InfoColor+"\n", text)
}
// Formats the code using go fmt
func Fmt() {
mg.Deps(initVars)
@ -747,14 +755,56 @@ func init() {
})
}
`
f, err := os.Create(RootPath + "/pkg/migration/" + date + ".go")
filename := "/pkg/migration/" + date + ".go"
f, err := os.Create(RootPath + filename)
defer f.Close()
if err != nil {
return err
}
_, err = f.WriteString(migration)
return err
if _, err := f.WriteString(migration); err != nil {
return err
}
printSuccess("Migration has been created at %s!", filename)
return nil
}
// Create a new event. Takes the name of the event as the first argument and the module where the event should be created as the second argument. Events will be appended to the pkg/<module>/events.go file.
func (Dev) CreateEvent(name, module string) error {
name = strcase.ToCamel(name)
if !strings.HasSuffix(name, "Event") {
name += "Event"
}
eventName := strings.ReplaceAll(strcase.ToDelimited(name, '.'), ".event", "")
newEventCode := `
type ` + name + ` struct {
}
func (t *` + name + `) TopicName() string {
return "` + eventName + `"
}
`
filename := "./pkg/" + module + "/events.go"
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
defer f.Close()
if _, err = f.WriteString(newEventCode); err != nil {
return err
}
printSuccess("The new event has been created successfully! Head over to %s and adjust its content.", filename)
return nil
}
type configOption struct {