diff --git a/cmd/dfdaemon/app/gen_doc.go b/cmd/dfdaemon/app/gen_doc.go deleted file mode 100644 index 0f01616eb..000000000 --- a/cmd/dfdaemon/app/gen_doc.go +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright The Dragonfly Authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package app - -import ( - "fmt" - "os" - - "github.com/spf13/cobra" - "github.com/spf13/cobra/doc" -) - -// GenDocCommand is used to implement 'gen-doc' command. -type GenDocCommand struct { - cmd *cobra.Command - - // path is the destination path of generated markdown documents. - path string -} - -func init() { - genDocCommand := &GenDocCommand{} - genDocCommand.cmd = &cobra.Command{ - Use: "gen-doc", - Short: "Generate Document for dfdaemon command line tool with MarkDown format", - Args: cobra.NoArgs, - SilenceErrors: true, - SilenceUsage: true, - RunE: func(cmd *cobra.Command, args []string) error { - return genDocCommand.runGenDoc(args) - }, - } - genDocCommand.addFlags() - rootCmd.AddCommand(genDocCommand.cmd) -} - -// addFlags adds flags for specific command. -func (g *GenDocCommand) addFlags() { - flagSet := g.cmd.Flags() - - flagSet.StringVarP(&g.path, "path", "p", "/tmp", "destination path of generated markdown documents") -} - -func (g *GenDocCommand) runGenDoc(args []string) error { - if _, err := os.Stat(g.path); err != nil { - if os.IsNotExist(err) { - return fmt.Errorf("path %s does not exist, please check your gen-doc input flag --path", g.path) - } - return err - } - return doc.GenMarkdownTree(g.cmd.Parent(), g.path) -} diff --git a/cmd/dfdaemon/app/root.go b/cmd/dfdaemon/app/root.go index 068a41bd2..9955b92cf 100644 --- a/cmd/dfdaemon/app/root.go +++ b/cmd/dfdaemon/app/root.go @@ -27,6 +27,7 @@ import ( "github.com/dragonflyoss/Dragonfly/dfdaemon" "github.com/dragonflyoss/Dragonfly/dfdaemon/config" "github.com/dragonflyoss/Dragonfly/dfdaemon/constant" + "github.com/dragonflyoss/Dragonfly/pkg/cmd" dferr "github.com/dragonflyoss/Dragonfly/pkg/errortypes" "github.com/dragonflyoss/Dragonfly/pkg/netutils" "github.com/dragonflyoss/Dragonfly/pkg/rate" @@ -105,6 +106,7 @@ func init() { rf.StringSlice("node", nil, "specify the addresses(host:port) of supernodes that will be passed to dfget.") exitOnError(bindRootFlags(viper.GetViper()), "bind root command flags") + rootCmd.AddCommand(cmd.NewGenDocCommand("dfdaemon")) } // bindRootFlags binds flags on rootCmd to the given viper instance. diff --git a/cmd/dfget/app/root.go b/cmd/dfget/app/root.go index 9f87ce355..8064f3990 100644 --- a/cmd/dfget/app/root.go +++ b/cmd/dfget/app/root.go @@ -26,6 +26,7 @@ import ( "github.com/dragonflyoss/Dragonfly/dfget/config" "github.com/dragonflyoss/Dragonfly/dfget/core" + "github.com/dragonflyoss/Dragonfly/pkg/cmd" "github.com/dragonflyoss/Dragonfly/pkg/dflog" "github.com/dragonflyoss/Dragonfly/pkg/errortypes" "github.com/dragonflyoss/Dragonfly/pkg/printer" @@ -67,6 +68,7 @@ var rootCmd = &cobra.Command{ func init() { initFlags() + rootCmd.AddCommand(cmd.NewGenDocCommand("dfget")) } // runDfget does some init operations and starts to download. diff --git a/cmd/supernode/app/root.go b/cmd/supernode/app/root.go index 6ec50dc06..817b3c878 100644 --- a/cmd/supernode/app/root.go +++ b/cmd/supernode/app/root.go @@ -23,6 +23,7 @@ import ( "reflect" "time" + "github.com/dragonflyoss/Dragonfly/pkg/cmd" "github.com/dragonflyoss/Dragonfly/pkg/dflog" "github.com/dragonflyoss/Dragonfly/pkg/errortypes" "github.com/dragonflyoss/Dragonfly/pkg/fileutils" @@ -50,9 +51,15 @@ var ( supernodeViper = viper.GetViper() ) +// supernodeDescription is used to describe supernode command in details. +var supernodeDescription = `SuperNode is a long-running process with two primary responsibilities: +It's the tracker and scheduler in the P2P network that choose appropriate downloading net-path for each peer. +It's also a CDN server that caches downloaded data from source to avoid downloading the same files from source repeatedly.` + var rootCmd = &cobra.Command{ - Use: "Dragonfly Supernode", - Long: "", + Use: "supernode", + Short: "the central control server of Dragonfly used for scheduling and cdn cache", + Long: supernodeDescription, Args: cobra.NoArgs, SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { @@ -115,6 +122,7 @@ var rootCmd = &cobra.Command{ func init() { setupFlags(rootCmd) + rootCmd.AddCommand(cmd.NewGenDocCommand("supernode")) } // setupFlags setups flags for command line. diff --git a/hack/generate-docs.sh b/hack/generate-docs.sh index 3953a6bfc..265c92a2a 100755 --- a/hack/generate-docs.sh +++ b/hack/generate-docs.sh @@ -14,9 +14,11 @@ generate-cli-docs(){ CLI_DOCS_DIR=$(cd "../docs/cli_reference" && pwd) DFGET_BIN_PATH=../"${BUILD_PATH}"/dfget DFDAEMON_BIN_PATH=../"${BUILD_PATH}"/dfdaemon + SUPERNODE_BIN_PATH=../"${BUILD_PATH}"/supernode ${DFGET_BIN_PATH} gen-doc -p "${CLI_DOCS_DIR}" || return ${DFDAEMON_BIN_PATH} gen-doc -p "${CLI_DOCS_DIR}" || return + ${SUPERNODE_BIN_PATH} gen-doc -p "${CLI_DOCS_DIR}" || return echo "Generate: CLI docs in ${CLI_DOCS_DIR}" } diff --git a/cmd/dfget/app/gen_doc.go b/pkg/cmd/gen_doc.go similarity index 90% rename from cmd/dfget/app/gen_doc.go rename to pkg/cmd/gen_doc.go index 11e27345c..c56ec5496 100644 --- a/cmd/dfget/app/gen_doc.go +++ b/pkg/cmd/gen_doc.go @@ -14,7 +14,7 @@ * limitations under the License. */ -package app +package cmd import ( "fmt" @@ -32,11 +32,11 @@ type GenDocCommand struct { path string } -func init() { +func NewGenDocCommand(name string) *cobra.Command { genDocCommand := &GenDocCommand{} genDocCommand.cmd = &cobra.Command{ Use: "gen-doc", - Short: "Generate Document for dfget command line tool with MarkDown format", + Short: fmt.Sprintf("Generate Document for %s command line tool in MarkDown format", name), Args: cobra.NoArgs, SilenceErrors: true, SilenceUsage: true, @@ -45,7 +45,7 @@ func init() { }, } genDocCommand.addFlags() - rootCmd.AddCommand(genDocCommand.cmd) + return genDocCommand.cmd } // addFlags adds flags for specific command.