|
1
|
|
|
package main |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"flag" |
|
5
|
|
|
"fmt" |
|
6
|
|
|
"os" |
|
7
|
|
|
|
|
8
|
|
|
"github.com/fraenky8/tables-to-go/internal/cli" |
|
9
|
|
|
"github.com/fraenky8/tables-to-go/pkg/database" |
|
10
|
|
|
"github.com/fraenky8/tables-to-go/pkg/output" |
|
11
|
|
|
"github.com/fraenky8/tables-to-go/pkg/settings" |
|
12
|
|
|
) |
|
13
|
|
|
|
|
14
|
|
|
// CmdArgs represents the supported command line args |
|
15
|
|
|
type CmdArgs struct { |
|
16
|
|
|
Help bool |
|
17
|
|
|
*settings.Settings |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
// NewCmdArgs creates and prepares the command line arguments with default values |
|
21
|
|
|
func NewCmdArgs() (args *CmdArgs) { |
|
22
|
|
|
|
|
23
|
|
|
args = &CmdArgs{ |
|
24
|
|
|
Settings: settings.New(), |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
flag.BoolVar(&args.Help, "?", false, "shows help and usage") |
|
28
|
|
|
flag.BoolVar(&args.Help, "help", false, "shows help and usage") |
|
29
|
|
|
flag.BoolVar(&args.Verbose, "v", args.Verbose, "verbose output") |
|
30
|
|
|
flag.BoolVar(&args.VVerbose, "vv", args.VVerbose, "more verbose output") |
|
31
|
|
|
flag.BoolVar(&args.Force, "f", args.Force, "force; skip tables that encounter errors") |
|
32
|
|
|
|
|
33
|
|
|
flag.Var(&args.DbType, "t", fmt.Sprintf("type of database to use, currently supported: %v", settings.SprintfSupportedDbTypes())) |
|
34
|
|
|
flag.StringVar(&args.User, "u", args.User, "user to connect to the database") |
|
35
|
|
|
flag.StringVar(&args.Pswd, "p", args.Pswd, "password of user") |
|
36
|
|
|
flag.StringVar(&args.DbName, "d", args.DbName, "database name") |
|
37
|
|
|
flag.StringVar(&args.Schema, "s", args.Schema, "schema name") |
|
38
|
|
|
flag.StringVar(&args.Host, "h", args.Host, "host of database") |
|
39
|
|
|
flag.StringVar(&args.Port, "port", args.Port, "port of database host, if not specified, it will be the default ports for the supported databases") |
|
40
|
|
|
|
|
41
|
|
|
flag.StringVar(&args.OutputFilePath, "of", args.OutputFilePath, "output file path, default is current working directory") |
|
42
|
|
|
flag.Var(&args.OutputFormat, "format", "format of struct fields (columns): camelCase (c) or original (o)") |
|
43
|
|
|
|
|
44
|
|
|
flag.Var(&args.FileNameFormat, "fn-format", "format of the filename: camelCase (c, default) or snake_case (s)") |
|
45
|
|
|
flag.StringVar(&args.Prefix, "pre", args.Prefix, "prefix for file- and struct names") |
|
46
|
|
|
flag.StringVar(&args.Suffix, "suf", args.Suffix, "suffix for file- and struct names") |
|
47
|
|
|
flag.StringVar(&args.PackageName, "pn", args.PackageName, "package name") |
|
48
|
|
|
flag.Var(&args.Null, "null", "representation of NULL columns: sql.Null* (sql) or primitive pointers (native|primitive)") |
|
49
|
|
|
|
|
50
|
|
|
flag.BoolVar(&args.NoInitialism, "no-initialism", args.NoInitialism, "disable the conversion to upper-case words in column names") |
|
51
|
|
|
|
|
52
|
|
|
flag.BoolVar(&args.TagsNoDb, "tags-no-db", args.TagsNoDb, "do not create db-tags") |
|
53
|
|
|
|
|
54
|
|
|
flag.BoolVar(&args.TagsMastermindStructable, "tags-structable", args.TagsMastermindStructable, "generate struct with tags for use in Masterminds/structable (https://github.com/Masterminds/structable)") |
|
55
|
|
|
flag.BoolVar(&args.TagsMastermindStructableOnly, "tags-structable-only", args.TagsMastermindStructableOnly, "generate struct with tags ONLY for use in Masterminds/structable (https://github.com/Masterminds/structable)") |
|
56
|
|
|
flag.BoolVar(&args.IsMastermindStructableRecorder, "structable-recorder", args.IsMastermindStructableRecorder, "generate a structable.Recorder field") |
|
57
|
|
|
|
|
58
|
|
|
// disable the print of usage when an error occurs |
|
59
|
|
|
flag.CommandLine.Usage = func() {} |
|
60
|
|
|
|
|
61
|
|
|
flag.Parse() |
|
62
|
|
|
|
|
63
|
|
|
return args |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// main function to run the transformations |
|
67
|
|
|
func main() { |
|
68
|
|
|
|
|
69
|
|
|
cmdArgs := NewCmdArgs() |
|
70
|
|
|
|
|
71
|
|
|
if cmdArgs.Help { |
|
72
|
|
|
flag.Usage() |
|
73
|
|
|
os.Exit(0) |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if err := cmdArgs.Verify(); err != nil { |
|
77
|
|
|
fmt.Print(err) |
|
78
|
|
|
os.Exit(1) |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
db := database.New(cmdArgs.Settings) |
|
82
|
|
|
|
|
83
|
|
|
if err := db.Connect(); err != nil { |
|
84
|
|
|
fmt.Println(err) |
|
85
|
|
|
os.Exit(1) |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
writer := output.NewFileWriter(cmdArgs.OutputFilePath) |
|
89
|
|
|
|
|
90
|
|
|
if err := cli.Run(cmdArgs.Settings, db, writer); err != nil { |
|
91
|
|
|
fmt.Printf("run error: %v\n", err) |
|
92
|
|
|
os.Exit(1) |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|