This is an automated email from the ASF dual-hosted git repository.

bzp2010 pushed a commit to branch next
in repository https://gitbox.apache.org/repos/asf/apisix-dashboard.git


The following commit(s) were added to refs/heads/next by this push:
     new 4b2ca919 chore: simplify CLI entry (#2498)
4b2ca919 is described below

commit 4b2ca919646326d2854d5177d3172c27c1f73891
Author: Zeping Bai <[email protected]>
AuthorDate: Fri Jul 8 20:16:19 2022 +0800

    chore: simplify CLI entry (#2498)
---
 api/cmd/root.go | 78 +++++++++++++++++++++++++--------------------------------
 api/main.go     |  9 ++++++-
 2 files changed, 42 insertions(+), 45 deletions(-)

diff --git a/api/cmd/root.go b/api/cmd/root.go
index 9e41c6f0..b9870333 100644
--- a/api/cmd/root.go
+++ b/api/cmd/root.go
@@ -17,7 +17,6 @@
 package cmd
 
 import (
-       "fmt"
        "os"
        "os/signal"
        "syscall"
@@ -29,55 +28,46 @@ import (
        "github.com/apache/apisix-dashboard/api/internal/log"
 )
 
-var rootCmd = &cobra.Command{
-       Use:   "manager-api",
-       Short: "Apache APISIX Manager API",
-       RunE: func(cmd *cobra.Command, args []string) error {
-               err := manageAPI()
-               return err
-       },
-}
-
-func init() {
-       rootCmd.PersistentFlags().StringVarP(&conf.ConfigFile, "config", "c", 
"", "config file")
-       rootCmd.PersistentFlags().StringVarP(&conf.WorkDir, "work-dir", "p", 
".", "current work directory")
+func NewRootCommand() *cobra.Command {
+       cmd := &cobra.Command{
+               Use:   "apisix-dashboard",
+               Short: "Apache APISIX Dashboard",
+               RunE: func(cmd *cobra.Command, args []string) error {
+                       conf.InitConf()
+                       log.InitLogger()
 
-       rootCmd.AddCommand(
-               newVersionCommand(),
-       )
-}
+                       s, err := server.NewServer(&server.Options{})
+                       if err != nil {
+                               return err
+                       }
 
-func Execute() {
-       if err := rootCmd.Execute(); err != nil {
-               _, _ = fmt.Fprintln(os.Stderr, err)
-       }
-}
+                       // start Manager API server
+                       errSig := make(chan error, 5)
+                       s.Start(errSig)
 
-func manageAPI() error {
-       conf.InitConf()
-       log.InitLogger()
+                       // Signal received to the process externally.
+                       quit := make(chan os.Signal, 1)
+                       signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
 
-       s, err := server.NewServer(&server.Options{})
-       if err != nil {
-               return err
+                       select {
+                       case sig := <-quit:
+                               log.Infof("The Manager API server receive %s 
and start shutting down", sig.String())
+                               s.Stop()
+                               log.Infof("See you next time!")
+                       case err := <-errSig:
+                               log.Errorf("The Manager API server start 
failed: %s", err.Error())
+                               return err
+                       }
+                       return nil
+               },
        }
 
-       // start Manager API server
-       errSig := make(chan error, 5)
-       s.Start(errSig)
+       cmd.PersistentFlags().StringVarP(&conf.ConfigFile, "config", "c", "", 
"config file")
+       cmd.PersistentFlags().StringVarP(&conf.WorkDir, "work-dir", "p", ".", 
"current work directory")
 
-       // Signal received to the process externally.
-       quit := make(chan os.Signal, 1)
-       signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
+       cmd.AddCommand(
+               newVersionCommand(),
+       )
 
-       select {
-       case sig := <-quit:
-               log.Infof("The Manager API server receive %s and start shutting 
down", sig.String())
-               s.Stop()
-               log.Infof("See you next time!")
-       case err := <-errSig:
-               log.Errorf("The Manager API server start failed: %s", 
err.Error())
-               return err
-       }
-       return nil
+       return cmd
 }
diff --git a/api/main.go b/api/main.go
index 04affdfc..1b405b9c 100644
--- a/api/main.go
+++ b/api/main.go
@@ -17,9 +17,16 @@
 package main
 
 import (
+       "fmt"
+       "os"
+
        "github.com/apache/apisix-dashboard/api/cmd"
 )
 
 func main() {
-       cmd.Execute()
+       rootCmd := cmd.NewRootCommand()
+       if err := rootCmd.Execute(); err != nil {
+               _, _ = fmt.Fprintln(os.Stderr, err)
+               os.Exit(1)
+       }
 }

Reply via email to