FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sql2struct/cmd/root.go at main · Siqitango/sql2struct · GitHub
Siqitango
/
sql2struct
Public
forked from
starfishs/sql2struct
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
sql2struct
/
cmd
/
root.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
76 lines (66 loc) · 2.76 KB
Breadcrumbs
sql2struct
/
cmd
/
root.go
Copy path
File metadata and controls
76 lines (66 loc) · 2.76 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
cmd
import
(
"os"
"github.com/spf13/cobra"
"github.com/starfishs/sql2struct/config"
"github.com/starfishs/sql2struct/internal/driver"
"github.com/starfishs/sql2struct/internal/infra"
"github.com/starfishs/sql2struct/utils"
)
// rootCmd represents the base command when called without any subcommands
var
rootCmd
=
&
cobra.
Command
{
Use
:
"sql2struct"
,
Short
:
"sql2struct is a tool for generating golang struct from mysql/postgresql database"
,
Long
:
`sql2struct is a tool for generating golang struct from mysql/postgresql database.
`
,
// Uncomment the following line if your bare application
// has an action associated with it:
Run
:
func
(
cmd
*
cobra.
Command
,
args
[]
string
) {
// Do Stuff Here
if
config
.
Cnf
.
DSN
==
""
{
utils
.
PrintRed
(
"dsn is empty"
)
_
=
cmd
.
Help
()
os
.
Exit
(
1
)
}
driverName
,
dsn
,
err
:=
utils
.
ParseDsn
(
config
.
Cnf
.
DSN
)
if
err
!=
nil
{
utils
.
PrintRed
(
"dsn is invalid"
)
_
=
cmd
.
Help
()
os
.
Exit
(
1
)
}
infra
.
InitDB
(
driverName
,
dsn
)
err
=
driver
.
NewSqlDriverGenerator
(
driverName
).
Run
()
if
err
!=
nil
{
utils
.
PrintRed
(
err
.
Error
())
}
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func
Execute
() {
defer
func
() {
if
err
:=
recover
();
err
!=
nil
{
utils
.
PrintRedf
(
"error occur %v"
,
err
)
}
}()
err
:=
rootCmd
.
Execute
()
if
err
!=
nil
{
os
.
Exit
(
1
)
}
}
func
init
() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
//rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.sql2struct.yaml)")
rootCmd
.
PersistentFlags
().
StringVar
(
&
config
.
Cnf
.
DSN
,
"dsn"
,
""
,
"database dsn string (eg: root:123456@tcp(localhost:3306)/test?charset=utf8)"
)
_
=
rootCmd
.
MarkFlagRequired
(
"dsn"
)
rootCmd
.
PersistentFlags
().
StringVarP
(
&
config
.
Cnf
.
DBTag
,
"dbtag"
,
"g"
,
"gorm"
,
"db tag. default: gorm"
)
rootCmd
.
PersistentFlags
().
StringVarP
(
&
config
.
Cnf
.
TablePrefix
,
"prefix"
,
"p"
,
""
,
"table prefixed with the table name"
)
rootCmd
.
PersistentFlags
().
BoolVarP
(
&
config
.
Cnf
.
WithJsonTag
,
"with_json_tag"
,
"j"
,
true
,
"with json tag. default: true"
)
rootCmd
.
PersistentFlags
().
BoolVarP
(
&
config
.
Cnf
.
WithDefaultValue
,
"with_default_value"
,
""
,
false
,
"with db default value. default: false"
)
rootCmd
.
PersistentFlags
().
StringVarP
(
&
config
.
Cnf
.
OutputDir
,
"output_dir"
,
"o"
,
"./model"
,
"output dir. default: ./model"
)
rootCmd
.
PersistentFlags
().
StringArrayVarP
(
&
config
.
Cnf
.
TableRegexs
,
"table_regexs"
,
"t"
,
nil
,
"Need to generate table names regexs, default is all tables. (eg: -t table1 -t table2)"
)
// Cobra also supports local flags, which will only run
// when this action is called directly.
}
Back
|
FazBrowse Home
|
New Git URL