|
5 | 5 | "io"
|
6 | 6 | "os"
|
7 | 7 | "os/user"
|
| 8 | + "sort" |
8 | 9 | "strings"
|
| 10 | + "time" |
9 | 11 |
|
10 | 12 | "github.com/urfave/cli"
|
11 | 13 | "github.com/voidint/tsdump/build"
|
@@ -67,11 +69,12 @@ COPYRIGHT:
|
67 | 69 | }
|
68 | 70 |
|
69 | 71 | func main() {
|
| 72 | + now := time.Now() |
70 | 73 | app := cli.NewApp()
|
71 | 74 | app.Name = "tsdump"
|
72 | 75 | app.Usage = "Database table structure dump tool."
|
73 | 76 | app.Version = build.Version()
|
74 |
| - app.Copyright = "Copyright (c) 2017-2020, voidint. All rights reserved." |
| 77 | + app.Copyright = fmt.Sprintf("Copyright (c) 2017-%d, voidint. All rights reserved.", now.Year()) |
75 | 78 | app.Authors = []cli.Author{
|
76 | 79 | cli.Author{
|
77 | 80 | Name: "voidint",
|
@@ -127,6 +130,11 @@ func main() {
|
127 | 130 | Usage: "write to a file, instead of STDOUT",
|
128 | 131 | Destination: &c.Output,
|
129 | 132 | },
|
| 133 | + cli.BoolFlag{ |
| 134 | + Name: "s, sorted", |
| 135 | + Usage: "sort table columns", |
| 136 | + Destination: &c.Sorted, |
| 137 | + }, |
130 | 138 | }
|
131 | 139 |
|
132 | 140 | app.Before = func(ctx *cli.Context) (err error) {
|
@@ -155,6 +163,10 @@ func main() {
|
155 | 163 | return cli.NewExitError(fmt.Sprintf("[tsdump] %s", err.Error()), 1)
|
156 | 164 | }
|
157 | 165 |
|
| 166 | + if c.Sorted { |
| 167 | + sortedDBs(dbs) |
| 168 | + } |
| 169 | + |
158 | 170 | if c.Output != "" {
|
159 | 171 | var f *os.File
|
160 | 172 | if f, err = os.Create(c.Output); err != nil {
|
@@ -231,3 +243,28 @@ func getMetadata(repo model.IRepo, db string, tables ...string) (dbs []model.DB,
|
231 | 243 | }
|
232 | 244 | return dbs, nil
|
233 | 245 | }
|
| 246 | + |
| 247 | +func sortedDBs(dbs []model.DB) { |
| 248 | + for i := range dbs { |
| 249 | + sortedTables(dbs[i].Tables) |
| 250 | + } |
| 251 | + |
| 252 | + sort.Slice(dbs, func(i, j int) bool { |
| 253 | + return dbs[i].Name < dbs[j].Name |
| 254 | + }) |
| 255 | +} |
| 256 | + |
| 257 | +func sortedTables(tables []model.Table) { |
| 258 | + for i := range tables { |
| 259 | + sortedColumns(tables[i].Columns) |
| 260 | + } |
| 261 | + sort.Slice(tables, func(i, j int) bool { |
| 262 | + return tables[i].Name < tables[j].Name |
| 263 | + }) |
| 264 | +} |
| 265 | + |
| 266 | +func sortedColumns(columns []model.Column) { |
| 267 | + sort.Slice(columns, func(i, j int) bool { |
| 268 | + return columns[i].Name < columns[j].Name |
| 269 | + }) |
| 270 | +} |
0 commit comments