-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcmd_generate.go
62 lines (58 loc) · 1.46 KB
/
cmd_generate.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
61
62
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/machinebox/remoto/generator"
"github.com/spf13/cobra"
)
func init() {
var outputFile string
var generateCmd = &cobra.Command{
Use: "generate definition template",
Short: "Generate source code from a template and remoto definition.",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
definition := args[0]
template := args[1]
f, err := os.Open(definition)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
defer f.Close()
def, err := generator.Parse(f)
if err != nil {
fmt.Fprintf(os.Stderr, "parse: %v\n", err)
os.Exit(1)
}
var o io.Writer = os.Stdout
if outputFile != "" {
if err := os.MkdirAll(filepath.Dir(outputFile), 0777); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
outFile, err := os.Create(outputFile)
if err != nil {
fmt.Fprintf(os.Stderr, "parse: %v\n", err)
os.Exit(1)
}
defer outFile.Close()
o = outFile
}
b, err := ioutil.ReadFile(template)
if err != nil {
fmt.Fprintf(os.Stderr, "template: %v\n", err)
os.Exit(1)
}
if err := generator.Render(o, template, string(b), def); err != nil {
fmt.Fprintf(os.Stderr, "render template: %v\n", err)
os.Exit(1)
}
},
}
generateCmd.Flags().StringVarP(&outputFile, "output", "o", "", "output file (default stdout)")
rootCmd.AddCommand(generateCmd)
}