Skip to content

Commit ddb76db

Browse files
authored
feat: split cmd package code (#1932)
1 parent 0b3d927 commit ddb76db

File tree

19 files changed

+291
-157
lines changed

19 files changed

+291
-157
lines changed

.github/workflows/backend-unit-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050
export GO111MOUDULE=on
5151
export APISIX_CONF_PATH=$PWD/conf
5252
sed -i 's/9000/8088/' conf/conf.yaml
53-
go build -o ./manager-api ./cmd/manager
53+
go build -o ./manager-api ./main.go
5454
./manager-api > ./api.log 2>&1 &
5555
sleep 2
5656
cat ./api.log

.github/workflows/frontend-e2e-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555
working-directory: ./api
5656
run: |
5757
sed -i 's@# - dubbo-proxy@- dubbo-proxy@' ./conf/conf.yaml
58-
nohup go run ./cmd/manager &
58+
nohup go run ./main.go &
5959
6060
- name: Cache node_modules
6161
uses: actions/[email protected]

.github/workflows/frontend-plugin-e2e-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757
working-directory: ./api
5858
run: |
5959
sed -i 's@# - dubbo-proxy@- dubbo-proxy@' ./conf/conf.yaml
60-
nohup go run ./cmd/manager &
60+
nohup go run ./main.go &
6161
6262
- name: Cache node_modules
6363
uses: actions/[email protected]

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ api-run: api-default
8080

8181
### api-stop: Stop the manager-api
8282
api-stop:
83-
cd api && go run -ldflags "${GOLDFLAGS}" ./cmd/manager stop
83+
cd api && go run -ldflags "${GOLDFLAGS}" ./main.go stop
8484

8585
### go-lint: Lint Go source codes
8686
.PHONY: go-lint

api/build.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ GOLDFLAGS="-X github.com/apisix/manager-api/internal/utils.version=${VERSION} -X
2424

2525
# Enter dry-run mode
2626
if [ "$1" == "--dry-run" ]; then
27-
cd ./api && go run -ldflags "${GOLDFLAGS}" ./cmd/manager
27+
cd ./api && go run -ldflags "${GOLDFLAGS}" ./main.go
2828
exit 0
2929
fi
3030

@@ -42,7 +42,7 @@ if [[ ! -f "dag-to-lua-1.1/lib/dag-to-lua.lua" ]]; then
4242
fi
4343

4444
# build
45-
cd ./api && go build -o ../output/manager-api -ldflags "${GOLDFLAGS}" ./cmd/manager && cd ..
45+
cd ./api && go build -o ../output/manager-api -ldflags "${GOLDFLAGS}" ./main.go && cd ..
4646

4747
cp ./api/conf/schema.json ./output/conf/schema.json
4848
cp ./api/conf/conf.yaml ./output/conf/conf.yaml

api/cmd/install.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
)
24+
25+
var service *Service
26+
27+
func newInstallCommand() *cobra.Command {
28+
return &cobra.Command{
29+
Use: "install",
30+
Short: "re-install Apache APISIX Dashboard service",
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
serviceState.installService = true
33+
status, err := service.manageService()
34+
fmt.Println(status)
35+
return err
36+
},
37+
}
38+
}

api/cmd/remove.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
)
24+
25+
func newRemoveCommand() *cobra.Command {
26+
return &cobra.Command{
27+
Use: "remove",
28+
Short: "remove Apache APISIX Dashboard service",
29+
RunE: func(cmd *cobra.Command, args []string) error {
30+
serviceState.removeService = true
31+
status, err := service.manageService()
32+
fmt.Println(status)
33+
return err
34+
},
35+
}
36+
}

api/cmd/managerapi.go renamed to api/cmd/root.go

Lines changed: 33 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,19 @@ import (
4242
)
4343

4444
var (
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

9885
func 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
}

api/cmd/service.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import (
2020
"os"
2121
"runtime"
2222

23-
"github.com/apisix/manager-api/internal/conf"
2423
"github.com/takama/daemon"
24+
25+
"github.com/apisix/manager-api/internal/conf"
2526
)
2627

2728
type Service struct {

api/cmd/start.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
)
24+
25+
func newStartCommand() *cobra.Command {
26+
return &cobra.Command{
27+
Use: "start",
28+
Short: "start Apache APISIX Dashboard service",
29+
RunE: func(cmd *cobra.Command, args []string) error {
30+
serviceState.startService = true
31+
status, err := service.manageService()
32+
fmt.Println(status)
33+
return err
34+
},
35+
}
36+
}

0 commit comments

Comments
 (0)