Skip to content

Commit b387613

Browse files
committed
Merge pull request #1 from suntong/reconstruct
Reconstruct
2 parents 9763302 + 7fb3cac commit b387613

File tree

11 files changed

+289
-286
lines changed

11 files changed

+289
-286
lines changed

cmd/easygen/main.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
////////////////////////////////////////////////////////////////////////////
2+
// Porgram: EasyGen
3+
// Purpose: Easy to use universal code/text generator
4+
// Authors: Tong Sun (c) 2015, All rights reserved
5+
////////////////////////////////////////////////////////////////////////////
6+
7+
////////////////////////////////////////////////////////////////////////////
8+
// Program start
9+
10+
package main
11+
12+
import (
13+
"flag"
14+
"fmt"
15+
"os"
16+
)
17+
18+
import (
19+
"github.com/suntong/easygen"
20+
)
21+
22+
////////////////////////////////////////////////////////////////////////////
23+
// Constant and data type/structure definitions
24+
25+
////////////////////////////////////////////////////////////////////////////
26+
// Global variables definitions
27+
28+
////////////////////////////////////////////////////////////////////////////
29+
// Commandline definitions
30+
31+
func init() {
32+
33+
// set default values for command line parameters
34+
flag.BoolVar(&easygen.Opts.HTML, "html", false,
35+
"treat the template file as html instead of text")
36+
flag.StringVar(&easygen.Opts.TemplateStr, "ts", "",
37+
"template string (in text)")
38+
flag.StringVar(&easygen.Opts.TemplateFile, "tf", "",
39+
".tmpl template file `name` (default: same as .yaml file)")
40+
flag.StringVar(&easygen.Opts.ExtYaml, "ey", ".yaml",
41+
"`extension` of yaml file")
42+
flag.StringVar(&easygen.Opts.ExtTmpl, "et", ".tmpl",
43+
"`extension` of template file")
44+
flag.StringVar(&easygen.Opts.StrFrom, "rf", "",
45+
"replace from, the from string used for the replace function")
46+
flag.StringVar(&easygen.Opts.StrTo, "rt", "",
47+
"replace to, the to string used for the replace function")
48+
flag.IntVar(&easygen.Opts.Debug, "debug", 0,
49+
"debugging `level`")
50+
51+
// Now override those default values from environment variables
52+
if len(easygen.Opts.TemplateStr) == 0 ||
53+
len(os.Getenv("EASYGEN_TS")) != 0 {
54+
easygen.Opts.TemplateStr = os.Getenv("EASYGEN_TS")
55+
}
56+
if len(easygen.Opts.TemplateFile) == 0 ||
57+
len(os.Getenv("EASYGEN_TF")) != 0 {
58+
easygen.Opts.TemplateFile = os.Getenv("EASYGEN_TF")
59+
}
60+
if len(easygen.Opts.ExtYaml) == 0 ||
61+
len(os.Getenv("EASYGEN_EY")) != 0 {
62+
easygen.Opts.ExtYaml = os.Getenv("EASYGEN_EY")
63+
}
64+
if len(easygen.Opts.ExtTmpl) == 0 ||
65+
len(os.Getenv("EASYGEN_ET")) != 0 {
66+
easygen.Opts.ExtTmpl = os.Getenv("EASYGEN_ET")
67+
}
68+
if len(easygen.Opts.StrFrom) == 0 ||
69+
len(os.Getenv("EASYGEN_RF")) != 0 {
70+
easygen.Opts.StrFrom = os.Getenv("EASYGEN_RF")
71+
}
72+
if len(easygen.Opts.StrTo) == 0 ||
73+
len(os.Getenv("EASYGEN_RT")) != 0 {
74+
easygen.Opts.StrTo = os.Getenv("EASYGEN_RT")
75+
}
76+
77+
}
78+
79+
////////////////////////////////////////////////////////////////////////////
80+
// Main
81+
82+
func main() {
83+
flag.Usage = easygen.Usage
84+
flag.Parse()
85+
86+
// One mandatory non-flag arguments
87+
if len(flag.Args()) < 1 {
88+
easygen.Usage()
89+
}
90+
fileName := flag.Args()[0]
91+
easygen.TFStringsInit()
92+
93+
fmt.Print(easygen.Generate(easygen.Opts.HTML, fileName))
94+
}

config.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// !!! !!!
2+
// WARNING: Code automatically generated. Editing discouraged.
3+
// !!! !!!
4+
5+
package easygen
6+
7+
import (
8+
"flag"
9+
"fmt"
10+
"os"
11+
)
12+
13+
////////////////////////////////////////////////////////////////////////////
14+
// Constant and data type/structure definitions
15+
16+
const progname = "easygen" // os.Args[0]
17+
18+
// The Options struct defines the structure to hold the commandline values
19+
type Options struct {
20+
HTML bool // treat the template file as html instead of text
21+
TemplateStr string // template string (in text)
22+
TemplateFile string // .tmpl template file `name` (default: same as .yaml file)
23+
ExtYaml string // `extension` of yaml file
24+
ExtTmpl string // `extension` of template file
25+
StrFrom string // replace from, the from string used for the replace function
26+
StrTo string // replace to, the to string used for the replace function
27+
Debug int // debugging `level`
28+
}
29+
30+
////////////////////////////////////////////////////////////////////////////
31+
// Global variables definitions
32+
33+
// Opts holds the actual values from the command line parameters
34+
var Opts = Options{ExtYaml: ".yaml", ExtTmpl: ".tmpl"}
35+
36+
////////////////////////////////////////////////////////////////////////////
37+
// Commandline definitions
38+
39+
// Usage function shows help on commandline usage
40+
func Usage() {
41+
fmt.Fprintf(os.Stderr,
42+
"\nUsage:\n %s [flags] YamlFileName\n\nFlags:\n\n",
43+
progname)
44+
flag.PrintDefaults()
45+
fmt.Fprintf(os.Stderr,
46+
"\nYamlFileName: The name for the .yaml data and .tmpl template file\n\tOnly the name part, without extension. Can include the path as well.\n")
47+
os.Exit(0)
48+
}

easygen.go

Lines changed: 116 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,138 @@
77
////////////////////////////////////////////////////////////////////////////
88
// Program start
99

10-
package main
10+
package easygen
1111

1212
import (
13-
"flag"
13+
"bytes"
1414
"fmt"
15+
ht "html/template"
16+
"io"
17+
"io/ioutil"
18+
"os"
19+
"path/filepath"
20+
"strings"
21+
tt "text/template"
1522

16-
"github.com/suntong/easygen/easygenapi"
23+
"gopkg.in/yaml.v2"
1724
)
1825

1926
////////////////////////////////////////////////////////////////////////////
2027
// Constant and data type/structure definitions
2128

29+
// common type for a *(text|html).Template value
30+
type template interface {
31+
Execute(wr io.Writer, data interface{}) error
32+
ExecuteTemplate(wr io.Writer, name string, data interface{}) error
33+
Name() string
34+
}
35+
2236
////////////////////////////////////////////////////////////////////////////
2337
// Global variables definitions
2438

2539
////////////////////////////////////////////////////////////////////////////
26-
// Commandline definitions
40+
// Function definitions
2741

28-
////////////////////////////////////////////////////////////////////////////
29-
// Main
42+
// Generate2 will produce output according to the given template and driving data files
43+
func Generate2(HTML bool, fileNameTempl string, fileName string) string {
44+
Opts.TemplateFile = fileNameTempl
45+
ret := Generate(HTML, fileName)
46+
Opts.TemplateFile = ""
47+
return ret
48+
}
49+
50+
// Generate0 will produce output according from driving data without a template file
51+
func Generate0(HTML bool, strTempl string, fileName string) string {
52+
Opts.TemplateStr = strTempl
53+
ret := Generate(HTML, fileName)
54+
Opts.TemplateStr = ""
55+
return ret
56+
}
57+
58+
// Generate will produce output from the template according to driving data
59+
func Generate(HTML bool, fileName string) string {
60+
source, err := ioutil.ReadFile(fileName + Opts.ExtYaml)
61+
checkError(err)
3062

31-
func main() {
32-
flag.Usage = easygenapi.Usage
33-
flag.Parse()
63+
m := make(map[interface{}]interface{})
3464

35-
// One mandatory non-flag arguments
36-
if len(flag.Args()) < 1 {
37-
easygenapi.Usage()
65+
err = yaml.Unmarshal(source, &m)
66+
checkError(err)
67+
68+
// template file name
69+
fileNameT := fileName
70+
if len(Opts.TemplateFile) > 0 {
71+
fileNameT = Opts.TemplateFile
3872
}
39-
fileName := flag.Args()[0]
40-
easygenapi.TFStringsInit()
4173

42-
fmt.Print(easygenapi.Generate(easygenapi.Opts.HTML, fileName))
74+
t, err := parseFiles(HTML, fileNameT+Opts.ExtTmpl)
75+
checkError(err)
76+
77+
buf := new(bytes.Buffer)
78+
err = t.Execute(buf, m)
79+
checkError(err)
80+
81+
return buf.String()
82+
}
83+
84+
// parseFiles, initialization. By Matt Harden @gmail.com
85+
func parseFiles(HTML bool, filenames ...string) (template, error) {
86+
tname := filepath.Base(filenames[0])
87+
88+
// use text template
89+
funcMapHT := ht.FuncMap{
90+
"minus1": minus1,
91+
"cls2lc": cls2lc.String,
92+
"cls2uc": cls2uc.String,
93+
"cls2ss": cls2ss.String,
94+
"ck2lc": ck2lc.String,
95+
"ck2uc": ck2uc.String,
96+
"ck2ls": ck2ls.String,
97+
"ck2ss": ck2ss.String,
98+
"clc2ss": clc2ss.String,
99+
"cuc2ss": cuc2ss.String,
100+
}
101+
102+
_ = funcMapHT
103+
104+
if HTML {
105+
// use html template
106+
t, err := ht.ParseFiles(filenames...)
107+
//t, err := ht.New("HT").Funcs(funcMapHT).ParseFiles(filenames...)
108+
return t, err
109+
}
110+
111+
// use text template
112+
funcMap := tt.FuncMap{
113+
"eqf": strings.EqualFold,
114+
"split": strings.Fields,
115+
"minus1": minus1,
116+
"replace": replace,
117+
"replacec": replacec,
118+
"cls2lc": cls2lc.String,
119+
"cls2uc": cls2uc.String,
120+
"cls2ss": cls2ss.String,
121+
"ck2lc": ck2lc.String,
122+
"ck2uc": ck2uc.String,
123+
"ck2ls": ck2ls.String,
124+
"ck2ss": ck2ss.String,
125+
"clc2ss": clc2ss.String,
126+
"cuc2ss": cuc2ss.String,
127+
}
128+
129+
if len(Opts.TemplateStr) > 0 {
130+
t, err := tt.New("TT").Funcs(funcMap).Parse(Opts.TemplateStr)
131+
return t, err
132+
}
133+
134+
t, err := tt.New(tname).Funcs(funcMap).ParseFiles(filenames...)
135+
return t, err
136+
}
137+
138+
// Exit if error occurs
139+
func checkError(err error) {
140+
if err != nil {
141+
fmt.Printf("[%s] Fatal error - %v", progname, err.Error())
142+
os.Exit(1)
143+
}
43144
}

0 commit comments

Comments
 (0)