-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (50 loc) · 1.23 KB
/
main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"log"
"os"
"github.com/happenslol/mog/codegen"
"github.com/happenslol/mog/templates"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
)
func main() {
log.SetFlags(0)
app := &cli.App{
Name: "mog",
Action: generate,
Usage: "Generate mongodb collections and query helpers",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
DefaultText: "mog.yml",
Value: "mog.yml",
Usage: "Config file location",
Aliases: []string{"c"}}},
Commands: []*cli.Command{
{
Name: "init",
Usage: "Generate a file containing the default configuration",
Action: initConfig}}}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func generate(c *cli.Context) error {
configPath := c.String("config")
rawConfig, err := os.ReadFile(configPath)
if err != nil {
return fmt.Errorf("Failed to read config: %s", err.Error())
}
config := codegen.DefaultConfig()
if err := yaml.Unmarshal(rawConfig, &config); err != nil {
return fmt.Errorf("Failed to parse config: %s", err.Error())
}
config.Generate()
return nil
}
func initConfig(c *cli.Context) error {
configPath := c.String("config")
templates.WriteDefaultConfig(configPath)
return nil
}