-
Notifications
You must be signed in to change notification settings - Fork 40
/
gen.go
60 lines (51 loc) · 1.43 KB
/
gen.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 cmd
import (
"fmt"
"os"
"github.com/MakeNowJust/heredoc"
"github.com/raystack/meteor/recipe"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
// GenCmd creates a command object for the "gen" action
func GenCmd() *cobra.Command {
var (
outputDirPath string
dataFilePath string
)
cmd := &cobra.Command{
Use: "gen",
Args: cobra.MatchAll(cobra.ExactArgs(1)),
Short: "Generate recipes",
Long: heredoc.Doc(`
Generate multiple recipes using a template and list of data.
The generated recipes will be created on output directory.`,
),
Example: heredoc.Doc(`
# generate multiple recipes with a template
$ meteor gen my-template.yaml -o ./output-dir -d ./data.yaml
`),
Annotations: map[string]string{
"group": "core",
},
RunE: func(cmd *cobra.Command, args []string) error {
templatePath := args[0]
bytes, err := os.ReadFile(dataFilePath)
if err != nil {
return fmt.Errorf("error reading data: %w", err)
}
var data []recipe.TemplateData
if err := yaml.Unmarshal(bytes, &data); err != nil {
return fmt.Errorf("error parsing data: %w", err)
}
return recipe.FromTemplate(recipe.TemplateConfig{
TemplateFilePath: templatePath,
OutputDirPath: outputDirPath,
Data: data,
})
},
}
cmd.Flags().StringVarP(&outputDirPath, "output", "o", "", "Output directory")
cmd.Flags().StringVarP(&dataFilePath, "data", "d", "", "Template's data")
return cmd
}