1 Star 0 Fork 0

bughou / go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
cmds.go 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
bughou 提交于 2022-03-21 20:37 . save
package dbs
import (
"gitee.com/go-better/dev/tool/x/misc/dbs/create"
"gitee.com/go-better/dev/tool/x/release"
"github.com/spf13/cobra"
)
func Cmds() []*cobra.Command {
mysqlCmd := makeCmdWithLess(`mysql`, `Enter mysql cli.`, Mysql)
mysqlCmd.AddCommand(
makeCmd(`dump`, `mysqldump to stdout.`, MysqlDump),
)
postgresCmd := makeCmdWithLess(`psql`, `Enter psql cli.`, Psql)
postgresCmd.AddCommand(makeCreateCmd(`postgres`))
return []*cobra.Command{
postgresCmd,
mysqlCmd,
makeCmd(`mongo`, `Enter mongo cli.`, Mongo),
makeCmd(`redis`, `Enter redis cli.`, Redis),
}
}
func makeCmd(name, short string, fun func(env, key string, print bool) error) *cobra.Command {
var print bool
cmd := &cobra.Command{
Use: name + ` [env [key]]`,
Short: `[db] ` + short,
RunE: release.Env1Call(func(env, key string) error {
if key == `` {
key = `default`
}
return fun(env, key, print)
}),
}
cmd.Flags().BoolVarP(&print, `print`, `p`, false, `only print the command.`)
return cmd
}
func makeCmdWithLess(name, short string, fun func(env, key string, less, print bool) error) *cobra.Command {
var less bool
var print bool
cmd := &cobra.Command{
Use: name + ` [env [key]]`,
Short: `[db] ` + short,
RunE: release.Env1Call(func(env, key string) error {
if key == `` {
key = `default`
}
return fun(env, key, less, print)
}),
}
cmd.Flags().BoolVarP(&less, `less`, `l`, false, `use less as the pager.`)
cmd.Flags().BoolVarP(&print, `print`, `p`, false, `only print the command.`)
return cmd
}
func makeCreateCmd(typ string) *cobra.Command {
var dropDB bool
cmd := &cobra.Command{
Use: `create [env [key]]`,
Short: `create databases and tables (execute "*.sql" file in "sqls" dir).`,
RunE: release.Env1Call(func(env, key string) error {
return create.Do(env, typ, key, dropDB)
}),
}
cmd.Flags().BoolVarP(&dropDB, "drop-db", "d", false, "drop existing databases before creating")
return cmd
}
Go
1
https://gitee.com/bughou/go.git
git@gitee.com:bughou/go.git
bughou
go
go
d31700df43a9

搜索帮助