forked from gnormal/gnorm
/
preview.go
171 lines (161 loc) · 5.03 KB
/
preview.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package run
import (
"bytes"
"encoding/csv"
"encoding/json"
"fmt"
"sort"
"strings"
"text/template"
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
"gnorm.org/gnorm/environ"
"gnorm.org/gnorm/run/data"
yaml "gopkg.in/yaml.v2"
)
var previewTpl = template.Must(
template.New("preview").
Funcs(map[string]interface{}{"makeTable": makeTable}).
Parse(`
{{- range .Schemas }}{{$schema := .DBName -}}
Schema: {{.Name}}({{.DBName}})
{{range .Enums}}
Enum: {{.Name}}({{$schema}}.{{.DBName}})
{{makeTable .Values "{{.Name}}|{{.DBName}}|{{.Value}}" "Name" "DBName" "Value" }}
{{end -}}
{{range .Tables}}
Table: {{.Name}}({{$schema}}.{{.DBName}}){{if ne .Comment ""}}; {{.Comment}}{{end}}
{{makeTable .Columns "{{.Name}}|{{.DBName}}|{{.Type}}|{{.DBType}}|{{.IsArray}}|{{.IsPrimaryKey}}|{{.Ordinal}}|{{.IsFK}}|{{.HasFKRef}}|{{.Length}}|{{.UserDefined}}|{{.Nullable}}|{{.HasDefault}}|{{.Comment}}" "Name" "DBName" "Type" "DBType" "IsArray" "IsPrimaryKey" "Ordinal" "IsFK" "HasFKRef" "Length" "UserDefined" "Nullable" "HasDefault" "Comment" -}}
Indexes:
{{makeTable .Indexes "{{.Name}}|{{.DBName}}|{{.IsUnique}}|{{join .Columns.Names \", \"}}" "Name" "DBName" "IsUnique" "Columns"}}
{{end -}}
{{end -}}
`))
// PreviewFormat defines the types of output that Preview can return.
type PreviewFormat int
const (
// PreviewTabular shows the data in textual tables.
PreviewTabular PreviewFormat = iota
// PreviewYAML shows the data in YAML.
PreviewYAML
// PreviewJSON shows the data in JSON.
PreviewJSON
// PreviewTypes just prints out the column types used by the DB.
PreviewTypes
)
// Preview displays the database info that would be passed to your template
// based on your configuration.
func Preview(env environ.Values, cfg *Config, format PreviewFormat) error {
info, err := cfg.Driver.Parse(env.Log, cfg.ConnStr, cfg.Schemas, makeFilter(cfg.IncludeTables, cfg.ExcludeTables))
if err != nil {
return err
}
data, err := makeData(env.Log, info, cfg)
if err != nil {
return err
}
switch format {
case PreviewTypes:
displayTypes(env, data)
return nil
case PreviewYAML:
b, err := yaml.Marshal(data)
if err != nil {
return errors.WithMessage(err, "couldn't convert data to yaml")
}
_, err = env.Stdout.Write(b)
return err
case PreviewJSON:
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
return errors.WithMessage(err, "couldn't convert data to json")
}
_, err = env.Stdout.Write(b)
return err
case PreviewTabular:
return previewTpl.Execute(env.Stdout, data)
default:
return errors.Errorf("Unsupported format: %v", format)
}
}
// makeTable makes a nice-looking textual table from the given data using the
// given template as the rendering for each line. Columns in the template
// should be separated by a pipe '|'. Column titles are prepended to the table
// if they exist.
//
// For example where here people is a slice of structs with a Name and Age fields:
// ```
// makeTable(people, "{{.Name}}|{{.Age}}", "Name", "Age")
//
// +----------+-----+
// | Name | Age |
// +----------+-----+
// | Bob | 30 |
// | Samantha | 3 |
// +----------+-----+
// ```
func makeTable(data interface{}, templateStr string, columnTitles ...string) (string, error) {
t, err := template.New("table").
Funcs(map[string]interface{}{"join": strings.Join}).
Parse("{{range .}}" + templateStr + "\n{{end}}")
if err != nil {
return "", errors.WithMessage(err, "failed to parse table template")
}
buf := &bytes.Buffer{}
hasHeader := len(columnTitles) > 0
if hasHeader {
// this can't fail so we drop the error
_, _ = buf.WriteString(strings.Join(columnTitles, "|") + "\n")
}
if err := t.Execute(buf, data); err != nil {
return "", errors.WithMessage(err, "failed to run table template")
}
r := csv.NewReader(buf)
r.Comma = '|'
output := &bytes.Buffer{}
table, err := tablewriter.NewCSVReader(output, r, hasHeader)
if err != nil {
return "", errors.WithMessage(err, "failed to render from pipe delimited bytes")
}
table.SetAutoFormatHeaders(false)
table.Render()
return output.String(), nil
}
func displayTypes(env environ.Values, info *data.DBData) {
var nullCols []*data.Column
var cols []*data.Column
lookUp := make(map[string]bool)
nullLookUp := make(map[string]bool)
for _, v := range info.Schemas {
for _, t := range v.Tables {
for _, c := range t.Columns {
if c.Nullable {
if !nullLookUp[c.DBType] {
nullCols = append(nullCols, c)
nullLookUp[c.DBType] = true
}
} else {
if !lookUp[c.DBType] {
cols = append(cols, c)
lookUp[c.DBType] = true
}
}
}
}
}
sort.SliceStable(cols, func(i, j int) bool {
return cols[i].DBType < cols[j].DBType
})
sort.SliceStable(nullCols, func(i, j int) bool {
return nullCols[i].DBType < nullCols[j].DBType
})
fmt.Fprintln(env.Stdout, "[TypeMap]")
for _, c := range cols {
fmt.Fprintf(env.Stdout, "%q = %q\n", c.DBType, c.Type)
}
fmt.Fprintln(env.Stdout)
fmt.Fprintln(env.Stdout, "[NullableTypeMap]")
for _, c := range nullCols {
fmt.Fprintf(env.Stdout, "%q = %q\n", c.DBType, c.Type)
}
}