go corba命令行工具封装优化

# go 语言工具包 go install github.com/hhatto/gocloc/cmd/gocloc@latest

# WEBCLI120 CMD目录说明
### internal/consts 常量
### internal/menucmd 命令
### internal/service 实现类

### 下载GO代码工具
#### go get -u git.ichub.com/general/gocode
#### go install git.ichub.com/general/gocode/cmd/gocode

### 下载基础命令工具--一键生成神器

#### go get -u git.ichub.com/general/webcli120
#### go install git.ichub.com/general/webcli120/cmd/webcli120

# 基于技术
###  "github.com/spf13/cobra"

# bat 公共常用命令
###    goinstall.bat 安装
###    goget.bat 下包
###    commit.bat 提交
###    gorun.bat  运行
###    .\gtag.bat  打版本标签

# bat_git git命令
###   .\gtag.bat l|a|d 列表展示|增加|删除 
    
# webcli120 
###     命令行工具 webcli120 -h 
###     webcli120 godi all

# 检查全版本冲突
## webcli120 cv :
## webcli120 cv all|docker|go

### all       check all files for git merge conflict!
### docker    check docker dir's files for git merge conflict!
### go        check go all-files for git merge conflict!

# 一键生成工程
###     cd 项目路径
###     go mod init XXX.XXX.XXX.PRJ
        生成 4个工程 api grpc web admin

###     第一步 webcli120 inst config | cmd 
###     第二步 webcli120 inst api | rpc | admin | web | mobile 
####    webcli120 inst admin
#####        修改websample目录为server
#####  go.mod增加:  replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
## replace google.golang.org/protobuf => google.golang.org/protobuf v1.25.0
## replace golang.org/x/tools =>    golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect

####    webcli120 inst rpc
####    webcli120 inst web
#####      修改websample目录为server
####    webcli120 inst mobile
#####      暂时不使用   
####    webcli120 inst api
###        go mod init XXX.XXX.XXX
###        api工程的git路径=go包名
### 一体化本地引用包技术 go work init admin grpc web api  
        生成go.work文件

### 加密 webcli120 enc xxxx
### 解密 webcli120 dec xxxx
###  webcli120 rpc
###  webcli120 rpc server
###  webcli120 rpc cli


##   go mod tidy
# CC 圈复杂度检查
###    webcli120 cc all 产生一个汇总报告
###    webcli120 cc filename 指定文件检查报告
###    webcli120 cc cc 指定目录二级分目录的报告

# godi 
###    依赖注入工具
###    webcli120 godi all
###    依据结构名生成测试套
###    webcli120 godi suite IchubConfig 
  
# gtag
###    工程包打版本标签:webcli120 gtag list|add|del
###     Available Commands:
###    list    gtag list
###    add     gtag add tag
###    del     gtag del tag

# go workspace操作命令

##   go work init rpc,web,admin,api
##   这样本地可相互调用了。不需要打TAG,前提是一个GIT工程下的模块go.mod。
##   目前发版工具暂不支持 git.ichub.com之发版
##   已经验证gitlab.ichub.com一体化工程,go work简化跨工程引用包

# webcli120 list Usage:
### webcli120 list [flags]
#### webcli120 list [command]

### Available Commands:
#### config      list config 查看配置信息
#### path        list path显示路径
#### rpc         list rpc service显示本服务,注册的RPC服务信息
#### s           list s [flag] 显示指定名称的WEB服务/RPC服务信息
#### web         list web 显示注册的WEB服务信息list web service
package main

import (
    "git.ichub.com/general/webcli120/cmd/webcli120/internal/menu"
    "git.ichub.com/general/webcli120/cmd/webcli120/internal/service/webfile"
)

func main() {

    webfile.FindBeanWebFileService().IniConfigIfNotExist()
    menu.Execute()
}
package menu

import (
    "git.ichub.com/general/webcli120/cmd/webcli120/internal/menu/allcmd"
    "git.ichub.com/general/webcli120/cmd/webcli120/internal/menu/cccmd"
    "git.ichub.com/general/webcli120/cmd/webcli120/internal/menu/checkgvcmd"
    "git.ichub.com/general/webcli120/cmd/webcli120/internal/menu/gitcmd"
    "git.ichub.com/general/webcli120/cmd/webcli120/internal/menu/godicmd"
    "git.ichub.com/general/webcli120/cmd/webcli120/internal/menu/gorpccmd"
    "git.ichub.com/general/webcli120/cmd/webcli120/internal/menu/instcmd"
    "git.ichub.com/general/webcli120/cmd/webcli120/internal/menu/listcmd"
    "git.ichub.com/general/webcli120/cmd/webcli120/internal/menu/natscmd"
    "git.ichub.com/general/webcli120/cmd/webcli120/internal/menu/statcmd"
    "git.ichub.com/general/webcli120/cmd/webcli120/internal/service/menudto"
    "git.ichub.com/general/webcli120/cmd/webcli120/internal/service/menufactroy"
)
var ListCmdsMenu = menudto.MenuCmds{
    Title: "list",
    Short: "显示配置信息、服务信息、文件路径",
    Cmds:  list.ListCmds,
}

func init() {

    var rootCmd = menufactroy.FindBeanMenuFactroy().Init()

    //all enc-cmd
    rootCmd.AddCommands(allcmd.AllCmds...)

    // listcmd
    rootCmd.AddSubCmd(ListCmdsMenu)
    // stat
    rootCmd.AddSub("stat", "性能统计", statcmd.StatCmds...)
    // godi
    rootCmd.AddSub("godi", "依赖注入", godicmd.GodiCmds...)
    //check ver conflict
    rootCmd.AddSub("cv", "检查版本冲突check version", checkgvcmd.CvCmds...)
    // cccmd
    rootCmd.AddSub("cc", "检查代码圈复杂度Check CycloComplex", cccmd.CvCmds...)
    // nats
    rootCmd.AddSub("nats", "nats服务", natscmd.NatsCmds...)
    // rpc
    rootCmd.AddSub("rpc", "gorpc服务", gorpccmd.RpcCmds...)
    //gittag
    rootCmd.AddSub("gtag", "git tag标签版本", gitcmd.GitCmds...)
    // inst
    rootCmd.AddSub("inst", "一键生成工程", instcmd.InstCmds...)
    // install
    rootCmd.AddSub("i", "一键安装子项", instcmd.InstallCmds...)
}

func Execute() {
    menufactroy.FindBeanMenuFactroy().Execute()
}
var ListCmds = []*cobra.Command{
    ListRpcServiceCmd, ListServiceCmd, ListCfgCmd, ListPathCmd, ListServiceDefaultCmd,
}
var ListRpcServiceCmd = &cobra.Command{
    Use:   "rpc",
    Short: "list rpc service显示本服务注册的RPC服务信息",
    Run:   ListRpcService,
}
var ListServiceCmd = &cobra.Command{
    Use:   "s",
    Short: "list s [flag] 显示注册的WEB服务信息",
    Run:   ListServiceAs,
}

func ListServiceAs(cmd *cobra.Command, args []string) {
    fmt.Println("list service of project at etcd " + ichubconfig.FindBeanIchubConfig().Etcd.Server.Hosturl)
    var webcli = webclient.Default()
    if len(args) == 0 {

       fmt.Println("list service XXX.XXX.XXX , now is error!")
       return
    }
    var node, err = webcli.FindServicesAs(args[0])
    if err != nil {
       fmt.Println("err", err)
       return
    }
    if node == nil {
       fmt.Println(ichubconfig.FindBeanIchubConfig().ReadIchubWebServer())
       fmt.Println("no node found web service!")
       return
    }
    fmt.Println(jsonutils.ToJsonPretty(node))

}
func ListRpcService(cmd *cobra.Command, args []string) {
    fmt.Println("list service of project at etcd " + ichubconfig.FindBeanIchubConfig().Etcd.Server.Hosturl)
    var webcli = webclient.Default()

    var services, err = webcli.FindServicesAs(webcli.Config.ReadIchubRpc().ServerName)
    if err != nil {
       fmt.Println("err", err)
       return
    }
    if services == nil {
       fmt.Println(ichubconfig.FindBeanIchubConfig().ReadIchubWebServer())
       fmt.Println("no services found web service!" + webcli.Config.ReadIchubRpc().ToString())
       return
    }
    fmt.Println(jsonutils.ToJsonPretty(services))

}

var ListServiceDefaultCmd = &cobra.Command{
    Use:   "web",
    Short: "显示注册的WEB服务list web service",
    Run:   ListServiceDefault,
}

func ListServiceDefault(cmd *cobra.Command, args []string) {
    fmt.Println("list web service of goweb by etcd" + ichubconfig.FindBeanIchubConfig().Etcd.Server.Hosturl)
    var webcli = webclient.Default()

    var node = webcli.FindServiceNode()
    if node == nil {
       fmt.Println(ichubconfig.FindBeanIchubConfig().ReadIchubWebServer())
       fmt.Println("no node found web service!")
       return
    }
    fmt.Println(jsonutils.ToJsonPretty(node))

}

var ListPathCmd = &cobra.Command{
    Use:   "path",
    Short: "list path显示路径",
    Run:   ListPath,
}

func ListPath(cmd *cobra.Command, args []string) {
    fmt.Println("list path of goweb")
    webfile.FindBeanWebFileService().ListPath()

}

var ListCfgCmd = &cobra.Command{
    Use:   "config",
    Short: "查看配置信息",
    Run:   ListCfg,
}

func ListCfg(cmd *cobra.Command, args []string) {
    fmt.Println("view config ")
    var cfg = ichubconfig.FindBeanIchubConfig()
    cfg.Read()

    logrus.Info(cfg.ToPrettyString())
}
package menufactroy

import (
    "fmt"
    "git.ichub.com/general/webcli120/cmd/webcli120/internal/service/menudto"
    "git.ichub.com/general/webcli120/goconfig/base/basedto"
    "git.ichub.com/general/webcli120/goconfig/base/fileutils/goconfig"
    "github.com/spf13/cobra"
)

var version = "2.0"

const BASE_PKG = "git.ichub.com/general/webcli120/goconfig/base/basedto"

type MenuFactroy struct {
    basedto.BaseEntitySingle

    version    string
    cmdName    string
    basePkg    string
    RootCmd    *cobra.Command
    versionCmd *cobra.Command
}

func (self *MenuFactroy) BasePkg() string {
    return self.basePkg
}

func (self *MenuFactroy) SetBasePkg(basePkg string) {
    self.basePkg = basePkg
}

func NewMenuFactroy() *MenuFactroy {
    var f = &MenuFactroy{
       cmdName: "webcli120",
       version: version,
       basePkg: BASE_PKG,
    }

    return f
}

// goconfig.FindBeanGominiFilecfg().InitPkg(BASE_PKG)
func (self *MenuFactroy) Init() *MenuFactroy {
    _ = goconfig.FindBeanGominiFilecfg().InitPkgPath() //self.basePkg)

    self.rootCmds()
    self.versionCmds()
    self.AddCommand(self.versionCmd)
    return self
}
func (self *MenuFactroy) AddCommand(cmd *cobra.Command) *MenuFactroy {

    self.RootCmd.AddCommand(cmd)
    return self
}
func (self *MenuFactroy) AddCommands(cmds ...*cobra.Command) *MenuFactroy {
    for _, cmd := range cmds {
       self.RootCmd.AddCommand(cmd)
    }
    return self
}
func (self *MenuFactroy) AddSubCmds(parentTitle string, cmds ...*cobra.Command) *MenuFactroy {
    var pcmd = &cobra.Command{
       Use:   parentTitle,
       Short: parentTitle,
       Run: func(cmd *cobra.Command, args []string) {
          fmt.Println(parentTitle)
       },
    }
    for _, cmd := range cmds {
       pcmd.AddCommand(cmd)
    }
    self.RootCmd.AddCommand(pcmd)
    return self
}
func (self *MenuFactroy) AddSubCmd(cmd menudto.MenuCmds) *MenuFactroy {
    return self.AddSub(cmd.Title, cmd.Short, cmd.Cmds...)

}
func (self *MenuFactroy) AddSub(parentTitle, short string, cmds ...*cobra.Command) *MenuFactroy {
    var pcmd = &cobra.Command{
       Use:   parentTitle,
       Short: short,
       Run: func(cmd *cobra.Command, args []string) {
          fmt.Println(parentTitle)
       },
    }
    for _, cmd := range cmds {
       pcmd.AddCommand(cmd)
    }
    self.RootCmd.AddCommand(pcmd)
    return self
}

func (self *MenuFactroy) Execute() error {

    return self.RootCmd.Execute()
}

func (self *MenuFactroy) rootCmds() {

    self.RootCmd = &cobra.Command{
       Use:   self.cmdName,
       Short: "A cmd tool  : a tool using Cobra!",
       Long:  `This is a cmd application using Cobra`,
       Run: func(cmd *cobra.Command, args []string) {
          fmt.Println("Hello,welcome to  webcli120!")
       },
    }
}
func (self *MenuFactroy) versionCmds() {
    self.versionCmd = &cobra.Command{
       Use:   "version",
       Short: "Print the version number of webcli120",
       Run: func(cmd *cobra.Command, args []string) {
          fmt.Printf("webcli120 version %s\n", self.version)
       },
    }
}

func (self *MenuFactroy) CmdName() string {
    return self.cmdName
}

func (self *MenuFactroy) SetCmdName(cmdName string) {
    self.cmdName = cmdName
}

func (self *MenuFactroy) Version() string {
    return self.version
}

func (self *MenuFactroy) SetVersion(version string) {
    self.version = version
}
package menufactroy

import (
    "git.ichub.com/general/webcli120/goconfig/base/baseiface"
    "git.ichub.com/general/webcli120/goconfig/basedi"

    "github.com/sirupsen/logrus"
)

/*
    @Title   文件名称: menu_factroy_init.go
    @Desp    描述:    自动注册注入
    @Company 公司:    kunlong@sz.com

    @Author  作者:    LEIJMDAS@163.COM  时间: 2024-08-07 21:30:11
    @Update  作者:    LEIJMDAS@163.COM  时间: 2024-08-07 21:30:11
*/

var singleNameMenuFactroy = "*cmdfactroy.MenuFactroy-2f2c1dbf-02ca-48b9-9d3e-c92720cc37d5"

// init register load
func init() {
    registerBeanMenuFactroy()
}

// register MenuFactroy
func registerBeanMenuFactroy() {
    _ = basedi.RegisterLoadBean(singleNameMenuFactroy, LoadMenuFactroy)
}

// FindBeanMenuFactroy
func FindBeanMenuFactroy() *MenuFactroy {

    if bean, ok := basedi.FindBeanOk(singleNameMenuFactroy); ok {
       return bean.(*MenuFactroy)
    }

    logrus.Error("not find bean!")
    return nil
}

func LoadMenuFactroy() baseiface.ISingleton {
    var inst = NewMenuFactroy()
    InjectMenuFactroy(inst)
    return inst

}

func InjectMenuFactroy(s *MenuFactroy) {

    //logrus.Info("inject")
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leijmdas

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值