@@ -42,30 +42,19 @@ import (
4242)
4343
4444var (
45- showVersion bool
46- Version string
47- GitHash string
48- service * Service
45+ configFile string
4946)
5047
51- func printInfo () {
52- fmt .Fprint (os .Stdout , "The manager-api is running successfully!\n \n " )
53- printVersion ()
54- fmt .Fprintf (os .Stdout , "%-8s: %s:%d\n " , "Listen" , conf .ServerHost , conf .ServerPort )
55- if conf .SSLCert != "" && conf .SSLKey != "" {
56- fmt .Fprintf (os .Stdout , "%-8s: %s:%d\n " , "HTTPS Listen" , conf .SSLHost , conf .SSLPort )
57- }
58- fmt .Fprintf (os .Stdout , "%-8s: %s\n " , "Loglevel" , conf .ErrorLogLevel )
59- fmt .Fprintf (os .Stdout , "%-8s: %s\n \n " , "Logfile" , conf .ErrorLogPath )
60- }
61-
62- func printVersion () {
63- fmt .Fprintf (os .Stdout , "%-8s: %s\n " , "Version" , Version )
64- fmt .Fprintf (os .Stdout , "%-8s: %s\n " , "GitHash" , GitHash )
48+ var rootCmd = & cobra.Command {
49+ Use : "manager-api [flags]" ,
50+ Short : "Apache APISIX Manager API" ,
51+ RunE : func (cmd * cobra.Command , args []string ) error {
52+ err := manageAPI ()
53+ return err
54+ },
6555}
6656
67- // NewManagerAPICommand creates the manager-api command.
68- func NewManagerAPICommand () * cobra.Command {
57+ func init () {
6958 cobra .OnInitialize (func () {
7059 var err error
7160 service , err = createService ()
@@ -74,25 +63,23 @@ func NewManagerAPICommand() *cobra.Command {
7463 }
7564 })
7665
77- cmd := & cobra.Command {
78- Use : "manager-api [flags]" ,
79- Short : "APISIX Manager API" ,
80- RunE : func (cmd * cobra.Command , args []string ) error {
81- GitHash , Version = utils .GetHashAndVersion ()
82- if showVersion {
83- printVersion ()
84- os .Exit (0 )
85- }
86- err := manageAPI ()
87- return err
88- },
89- }
90-
91- cmd .PersistentFlags ().StringVarP (& conf .WorkDir , "work-dir" , "p" , "." , "current work directory" )
92- cmd .PersistentFlags ().BoolVarP (& showVersion , "version" , "v" , false , "show manager-api version" )
66+ rootCmd .PersistentFlags ().StringVarP (& configFile , "config" , "c" , "./conf/conf.yml" , "config file" )
67+ rootCmd .PersistentFlags ().StringVarP (& conf .WorkDir , "work-dir" , "p" , "." , "current work directory" )
68+
69+ rootCmd .AddCommand (
70+ newVersionCommand (),
71+ newInstallCommand (),
72+ newRemoveCommand (),
73+ newStartCommand (),
74+ newStopCommand (),
75+ newStatusCommand (),
76+ )
77+ }
9378
94- cmd .AddCommand (newStartCommand (), newInstallCommand (), newStatusCommand (), newStopCommand (), newRemoveCommand ())
95- return cmd
79+ func Execute () {
80+ if err := rootCmd .Execute (); err != nil {
81+ _ , _ = fmt .Fprintln (os .Stderr , err .Error ())
82+ }
9683}
9784
9885func manageAPI () error {
@@ -211,80 +198,13 @@ func shutdownServer(server *http.Server) {
211198 }
212199}
213200
214- func newStartCommand () * cobra.Command {
215- cmd := & cobra.Command {
216- Use : "start" ,
217- Short : "start Apache APISIX Dashboard service" ,
218- RunE : func (cmd * cobra.Command , args []string ) error {
219- serviceState .startService = true
220- status , err := service .manageService ()
221- fmt .Println (status )
222- return err
223- },
224- }
225- return cmd
226- }
227-
228- func newInstallCommand () * cobra.Command {
229- cmd := & cobra.Command {
230- Use : "install" ,
231- Short : "re-install Apache APISIX Dashboard service" ,
232- RunE : func (cmd * cobra.Command , args []string ) error {
233- serviceState .installService = true
234- status , err := service .manageService ()
235- fmt .Println (status )
236- return err
237- },
238- }
239- return cmd
240- }
241-
242- func newStatusCommand () * cobra.Command {
243- cmd := & cobra.Command {
244- Use : "status" ,
245- Short : "inspect the status of Apache APISIX Dashboard service" ,
246- RunE : func (cmd * cobra.Command , args []string ) error {
247- serviceState .status = true
248- status , err := service .manageService ()
249- fmt .Println (status )
250- return err
251- },
252- }
253- return cmd
254- }
255-
256- func newStopCommand () * cobra.Command {
257- cmd := & cobra.Command {
258- Use : "stop" ,
259- Short : "stop Apache APISIX Dashboard service/program" ,
260- Run : func (cmd * cobra.Command , args []string ) {
261- pid , err := utils .ReadPID (conf .PIDPath )
262- if err != nil {
263- if syscall .ENOENT .Error () != err .Error () {
264- fmt .Fprintf (os .Stderr , "failed to get manager-api pid: %s\n " , err )
265- } else {
266- fmt .Fprintf (os .Stderr , "pid path %s not found, is manager-api running?\n " , conf .PIDPath )
267- }
268- return
269- }
270- if err := syscall .Kill (pid , syscall .SIGINT ); err != nil {
271- fmt .Fprintf (os .Stderr , "failed to kill manager-api: %s" , err )
272- }
273- },
274- }
275- return cmd
276- }
277-
278- func newRemoveCommand () * cobra.Command {
279- cmd := & cobra.Command {
280- Use : "remove" ,
281- Short : "remove Apache APISIX Dashboard service" ,
282- RunE : func (cmd * cobra.Command , args []string ) error {
283- serviceState .removeService = true
284- status , err := service .manageService ()
285- fmt .Println (status )
286- return err
287- },
201+ func printInfo () {
202+ fmt .Fprint (os .Stdout , "The manager-api is running successfully!\n \n " )
203+ printVersion ()
204+ fmt .Fprintf (os .Stdout , "%-8s: %s:%d\n " , "Listen" , conf .ServerHost , conf .ServerPort )
205+ if conf .SSLCert != "" && conf .SSLKey != "" {
206+ fmt .Fprintf (os .Stdout , "%-8s: %s:%d\n " , "HTTPS Listen" , conf .SSLHost , conf .SSLPort )
288207 }
289- return cmd
208+ fmt .Fprintf (os .Stdout , "%-8s: %s\n " , "Loglevel" , conf .ErrorLogLevel )
209+ fmt .Fprintf (os .Stdout , "%-8s: %s\n \n " , "Logfile" , conf .ErrorLogPath )
290210}
0 commit comments