/
genEndpoint.go
71 lines (61 loc) · 1.29 KB
/
genEndpoint.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
63
64
65
66
67
68
69
70
71
package gen
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"text/template"
)
func endpointGen(s string) error {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
abs, err := filepath.Abs(before(dir, "src/"))
var b []byte
if abs == dir {
b, err = ioutil.ReadFile(abs + "/github.com/AliasYermukanov/gkgen/templates/endpoint.gotxt") // just pass the file name
} else {
b, err = ioutil.ReadFile(abs + "/src/github.com/AliasYermukanov/gkgen/templates/endpoint.gotxt") // just pass the file name
}
if err != nil {
fmt.Print(err)
}
f, _ := os.Create(s + "-api/src/" + s + "/endpoint.go")
_, _ = f.Write([]byte(string(b)))
_ = f.Close()
err = parseEndpoint(s+"-api/src/"+s+"/endpoint.go", s, dir)
if err != nil {
return err
}
f, _ = os.Open(s + "-api/src/" + s + "/endpoint.go")
_, _ = io.Copy(os.Stdout, f)
_ = f.Close()
return nil
}
func parseEndpoint(path, s, src string) error {
t, err := template.ParseFiles(path)
if err != nil {
log.Print(err)
return err
}
f, err := os.Create(path)
if err != nil {
log.Println("create file: ", err)
return err
}
// A sample config
config := map[string]string{
"Sname": s,
"Dir": src,
}
err = t.Execute(f, config)
if err != nil {
log.Print("execute: ", err)
return err
}
_ = f.Close()
return nil
}