/
new_function.go
47 lines (41 loc) · 1.19 KB
/
new_function.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package commands
import (
"fmt"
"github.com/discless/discless/types/config"
"github.com/discless/discless/types/kinds"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"io/ioutil"
)
var NewFuncCmd = &cobra.Command{
Use: "function [function name]",
Short: "Create a new function with given name",
Args: cobra.MinimumNArgs(1),
RunE: FNewFunc,
}
func FNewFunc(cmd *cobra.Command, args []string) error {
yamlTemplate := &config.Config{
kinds.Function,
map[string]config.Function{
args[0]:{
File: args[0]+".go",
Function: "Handler",
Category: "",
},
},
}
mrs, _ := yaml.Marshal(yamlTemplate)
ioutil.WriteFile("function.yaml",mrs,0644)
goTemplate := `package main
import (
"github.com/bwmarrin/discordgo"
"github.com/discless/discless/types"
)
func ` + yamlTemplate.Functions[args[0]].Function + `(self *types.Self, s *discordgo.Session, m *discordgo.MessageCreate, args []string) error {
s.ChannelMessageSend(m.ChannelID, "Pong!")
return nil
}`
ioutil.WriteFile(yamlTemplate.Functions[args[0]].File,[]byte(goTemplate),0644)
fmt.Println("✅ Created the function",args[0],"\n ⤷ Edit its configuration in function.yaml or edit the function in",args[0]+".go")
return nil
}