diff --git a/cmd/root.go b/cmd/root.go index c5e5fa178d..51555da248 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -19,6 +19,7 @@ import ( "os" "github.com/spf13/cobra" + "github.com/spf13/cobra/doc" "github.com/ucloud/ucloud-sdk-go/ucloud/log" @@ -58,6 +59,7 @@ func NewCmdRoot() *cobra.Command { cmd.Flags().BoolVar(&global.Signup, "signup", false, "Launch UCloud sign up page in browser") cmd.AddCommand(NewCmdInit()) + cmd.AddCommand(NewCmdDoc()) cmd.AddCommand(NewCmdConfig()) cmd.AddCommand(NewCmdRegion()) cmd.AddCommand(NewCmdProject()) @@ -76,6 +78,38 @@ func NewCmdRoot() *cobra.Command { cmd.AddCommand(NewCmdRedis()) cmd.AddCommand(NewCmdMemcache()) + cmd.SetHelpTemplate(helpTmpl) + cmd.SetUsageTemplate(usageTmpl) + resetHelpFunc(cmd) + + return cmd +} + +//NewCmdDoc ucloud doc +func NewCmdDoc() *cobra.Command { + var dir string + cmd := &cobra.Command{ + Use: "doc", + Short: "Generate documents for all commands", + Long: "Generate documents for all commands", + Run: func(c *cobra.Command, args []string) { + rootCmd := NewCmdRoot() + emptyStr := func(s string) string { return "" } + linkHandler := func(name, ref string) string { + return fmt.Sprintf(":ref:`%s <%s>`", name, ref) + } + err := doc.GenReSTTreeCustom(rootCmd, dir, emptyStr, linkHandler) + if err != nil { + log.Fatal(err) + } + }, + } + cmd.Flags().StringVar(&dir, "dir", "", "Required. the directory where documents of commands are stored") + cmd.Flags().SetFlagValuesFunc("dir", func() []string { + return base.GetFileList("") + }) + cmd.MarkFlagRequired("dir") + return cmd } @@ -121,12 +155,8 @@ Use "{{.CommandPath}} --help" for details.{{end}} // 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() { - rootCmd := NewCmdRoot() - rootCmd.SetHelpTemplate(helpTmpl) - rootCmd.SetUsageTemplate(usageTmpl) - resetHelpFunc(rootCmd) - - if err := rootCmd.Execute(); err != nil { + cmd := NewCmdRoot() + if err := cmd.Execute(); err != nil { os.Exit(1) } } diff --git a/go.mod b/go.mod index 9f5ea2aa9b..5bb7ad9455 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,10 @@ go 1.12 require ( github.com/spf13/cobra v0.0.3 github.com/spf13/pflag v1.0.3 - github.com/ucloud/ucloud-sdk-go v0.8.1-alpha + github.com/ucloud/ucloud-sdk-go v0.8.1-beta1 ) replace ( - github.com/spf13/cobra v0.0.3 => github.com/lixiaojun629/cobra v0.0.5 + github.com/spf13/cobra v0.0.3 => github.com/lixiaojun629/cobra v0.0.6 github.com/spf13/pflag v1.0.3 => github.com/lixiaojun629/pflag v1.0.5 ) diff --git a/go.sum b/go.sum index 290863d2ac..46b62d7a9a 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,8 @@ -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= +github.com/cpuguy83/go-md2man v1.0.8 h1:DwoNytLphI8hzS2Af4D0dfaEaiSq2bN05mEm4R6vf8M= github.com/cpuguy83/go-md2man v1.0.8/go.mod h1:N6JayAiVKtlHSnuTCeuLSQVs75hb8q+dYQLjr7cDsKY= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -14,8 +14,8 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/lixiaojun629/cobra v0.0.5 h1:zGL+RRJEFBUrwy3Ehni/1uVe1K7KtbbCEtz1vD5o7zs= -github.com/lixiaojun629/cobra v0.0.5/go.mod h1:/UrNgAugHQZNoelw+TjY7C7OUQZT6O+B5EzpqMiSUkA= +github.com/lixiaojun629/cobra v0.0.6 h1:gq9sixoETQq8RiXcbQZJx1VggxCc0VgRiYg5xR/tQEk= +github.com/lixiaojun629/cobra v0.0.6/go.mod h1:S99rAzrIKP/LgwJBq8fP1b3CgQIa9xMVEI8/TmholyE= github.com/lixiaojun629/pflag v1.0.5 h1:plFJ2SBJd2S2Fc7ZwwFZ3682IvxBiUkhRuJS40OvEMs= github.com/lixiaojun629/pflag v1.0.5/go.mod h1:uchrjsiFxJj1XOBpO4YJCZwpqXHsCHovxY91tyFoUrg= github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY= @@ -30,7 +30,9 @@ github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/russross/blackfriday v2.0.0+incompatible/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= +github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo= +github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME= github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= @@ -45,8 +47,8 @@ github.com/spf13/viper v1.3.1/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DM github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/ucloud/ucloud-sdk-go v0.8.1-alpha h1:nnrkUqp1BWBd2wS8zP+HgMbj52KJtuEcWeEcHLDXIps= -github.com/ucloud/ucloud-sdk-go v0.8.1-alpha/go.mod h1:lM6fpI8y6iwACtlbHUav823/uKPdXsNBlnBpRF2fj3c= +github.com/ucloud/ucloud-sdk-go v0.8.1-beta1 h1:JEYMj+nm4vGUVIqpg0kc/iXMntzzxqN+sLo2W5+QduU= +github.com/ucloud/ucloud-sdk-go v0.8.1-beta1/go.mod h1:lM6fpI8y6iwACtlbHUav823/uKPdXsNBlnBpRF2fj3c= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I= @@ -59,6 +61,7 @@ golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a h1:1n5lsVfiQW3yfsRGu98756EH1 golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=