Add events #777
56
magefile.go
56
magefile.go
|
@ -24,6 +24,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/iancoleman/strcase"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
@ -294,6 +295,13 @@ func moveFile(src, dst string) error {
|
||||||
return nil
|
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
|
// Formats the code using go fmt
|
||||||
func Fmt() {
|
func Fmt() {
|
||||||
mg.Deps(initVars)
|
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()
|
defer f.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = f.WriteString(migration)
|
if _, err := f.WriteString(migration); err != nil {
|
||||||
return err
|
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 {
|
type configOption struct {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user