Skip to content

Commit c181f37

Browse files
committed
feat: add a service commnd
The following command will install atest as a Linux service. `atest service --action install`. Set the default service port to be 7070 to avoid confliction
1 parent 9ee38ff commit c181f37

File tree

5 files changed

+73
-4
lines changed

5 files changed

+73
-4
lines changed

cmd/root.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func NewRootCmd() (c *cobra.Command) {
1717
c.Version = version.GetVersion()
1818
c.AddCommand(createInitCommand(),
1919
createRunCommand(), createSampleCmd(),
20-
createServerCmd(), createJSONSchemaCmd())
20+
createServerCmd(), createJSONSchemaCmd(),
21+
createServiceCommand())
2122
return
2223
}

cmd/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func createServerCmd() (c *cobra.Command) {
1919
RunE: opt.runE,
2020
}
2121
flags := c.Flags()
22-
flags.IntVarP(&opt.port, "port", "p", 9090, "The RPC server port")
22+
flags.IntVarP(&opt.port, "port", "p", 7070, "The RPC server port")
2323
flags.BoolVarP(&opt.printProto, "print-proto", "", false, "Print the proto content and exit")
2424
return
2525
}

cmd/service.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
func createServiceCommand() (c *cobra.Command) {
12+
opt := &serviceOption{}
13+
c = &cobra.Command{
14+
Use: "service",
15+
Aliases: []string{"s"},
16+
Short: "Install atest as a Linux service",
17+
PreRunE: opt.preRunE,
18+
RunE: opt.runE,
19+
}
20+
flags := c.Flags()
21+
flags.StringVarP(&opt.action, "action", "a", "", "The action of service, support actions: install")
22+
return
23+
}
24+
25+
type serviceOption struct {
26+
action string
27+
}
28+
29+
func (o *serviceOption) preRunE(c *cobra.Command, args []string) (err error) {
30+
if runtime.GOOS != "linux" {
31+
err = fmt.Errorf("only support on Linux")
32+
}
33+
return
34+
}
35+
36+
func (o *serviceOption) runE(c *cobra.Command, args []string) (err error) {
37+
switch o.action {
38+
case "install", "i":
39+
err = os.WriteFile("/lib/systemd/system/atest.service", []byte(script), os.ModeAppend)
40+
default:
41+
err = fmt.Errorf("not support action: '%s'", o.action)
42+
}
43+
return
44+
}
45+
46+
var script = `[Unit]
47+
Description=API Testing
48+
49+
[Service]
50+
ExecStart=atest server
51+
52+
[Install]
53+
WantedBy=multi-user.target
54+
`

cmd/service_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestService(t *testing.T) {
10+
root := NewRootCmd()
11+
root.SetArgs([]string{"service", "--action", "fake"})
12+
err := root.Execute()
13+
assert.NotNil(t, err)
14+
}

sample/manifest.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ metadata:
3737
spec:
3838
ports:
3939
- name: server
40-
port: 9090
40+
port: 7070
4141
protocol: TCP
42-
targetPort: 9090
42+
targetPort: 7070
4343
selector:
4444
app: api-testing
4545
sessionAffinity: None

0 commit comments

Comments
 (0)