@@ -17,94 +17,94 @@ limitations under the License.
1717package cmd
1818
1919import (
20- "fmt"
21- "net"
22- "os"
23- "os/signal"
24- "syscall"
25-
26- "github.com/linuxsuren/api-testing/pkg/mock"
27- "github.com/spf13/cobra"
20+ "fmt"
21+ "net"
22+ "os"
23+ "os/signal"
24+ "syscall"
25+
26+ "github.com/linuxsuren/api-testing/pkg/mock"
27+ "github.com/spf13/cobra"
2828)
2929
3030type mockOption struct {
31- port int
32- prefix string
33- metrics bool
34- tls bool
35- tlsCert string
36- tlsKey string
31+ port int
32+ prefix string
33+ metrics bool
34+ tls bool
35+ tlsCert string
36+ tlsKey string
3737}
3838
3939func createMockCmd () (c * cobra.Command ) {
40- opt := & mockOption {}
41-
42- c = & cobra.Command {
43- Use : "mock" ,
44- Short : "Start a mock server" ,
45- Args : cobra .ExactArgs (1 ),
46- RunE : opt .runE ,
47- }
48-
49- flags := c .Flags ()
50- flags .IntVarP (& opt .port , "port" , "" , 6060 , "The mock server port" )
51- flags .StringVarP (& opt .prefix , "prefix" , "" , "/mock" , "The mock server API prefix" )
52- flags .BoolVarP (& opt .metrics , "metrics" , "m" , true , "Enable request metrics collection" )
53- flags .BoolVarP (& opt .tls , "tls" , "" , false , "Enable TLS mode. Set to true to enable TLS. Alow SAN certificates" )
54- flags .StringVarP (& opt .tlsCert , "cert-file" , "" , "" , "The path to the certificate file, Alow SAN certificates" )
55- flags .StringVarP (& opt .tlsKey , "key-file" , "" , "" , "The path to the key file, Alow SAN certificates" )
56- return
40+ opt := & mockOption {}
41+
42+ c = & cobra.Command {
43+ Use : "mock" ,
44+ Short : "Start a mock server" ,
45+ Args : cobra .ExactArgs (1 ),
46+ RunE : opt .runE ,
47+ }
48+
49+ flags := c .Flags ()
50+ flags .IntVarP (& opt .port , "port" , "" , 6060 , "The mock server port" )
51+ flags .StringVarP (& opt .prefix , "prefix" , "" , "/mock" , "The mock server API prefix" )
52+ flags .BoolVarP (& opt .metrics , "metrics" , "m" , true , "Enable request metrics collection" )
53+ flags .BoolVarP (& opt .tls , "tls" , "" , false , "Enable TLS mode. Set to true to enable TLS. Alow SAN certificates" )
54+ flags .StringVarP (& opt .tlsCert , "cert-file" , "" , "" , "The path to the certificate file, Alow SAN certificates" )
55+ flags .StringVarP (& opt .tlsKey , "key-file" , "" , "" , "The path to the key file, Alow SAN certificates" )
56+ return
5757}
5858
5959func (o * mockOption ) runE (c * cobra.Command , args []string ) (err error ) {
60- reader := mock .NewLocalFileReader (args [0 ])
61- server := mock .NewInMemoryServer (c .Context (), o .port )
62- if o .tls {
63- server .WithTLS (o .tlsCert , o .tlsKey )
64- }
65- if o .metrics {
66- server .EnableMetrics ()
67- }
68- if err = server .Start (reader , o .prefix ); err != nil {
69- return
70- }
71-
72- clean := make (chan os.Signal , 1 )
73- signal .Notify (clean , syscall .SIGINT , syscall .SIGTERM , syscall .SIGHUP , syscall .SIGQUIT )
74- printLocalIPs (c , o .port )
75- if o .metrics {
76- c .Printf ("Metrics available at http://localhost:%d%s/metrics\n " , o .port , o .prefix )
77- }
78-
79- select {
80- case <- c .Context ().Done ():
81- case <- clean :
82- }
83- err = server .Stop ()
84- return
60+ reader := mock .NewLocalFileReader (args [0 ])
61+ server := mock .NewInMemoryServer (c .Context (), o .port )
62+ if o .tls {
63+ server .WithTLS (o .tlsCert , o .tlsKey )
64+ }
65+ if o .metrics {
66+ server .EnableMetrics ()
67+ }
68+ if err = server .Start (reader , o .prefix ); err != nil {
69+ return
70+ }
71+
72+ clean := make (chan os.Signal , 1 )
73+ signal .Notify (clean , syscall .SIGINT , syscall .SIGTERM , syscall .SIGHUP , syscall .SIGQUIT )
74+ printLocalIPs (c , o .port )
75+ if o .metrics {
76+ c .Printf ("Metrics available at http://localhost:%d%s/metrics\n " , o .port , o .prefix )
77+ }
78+
79+ select {
80+ case <- c .Context ().Done ():
81+ case <- clean :
82+ }
83+ err = server .Stop ()
84+ return
8585}
8686
8787func printLocalIPs (c * cobra.Command , port int ) {
88- if ips , err := getLocalIPs (); err == nil {
89- for _ , ip := range ips {
90- c .Printf ("server is available at http://%s:%d\n " , ip , port )
91- }
92- }
88+ if ips , err := getLocalIPs (); err == nil {
89+ for _ , ip := range ips {
90+ c .Printf ("server is available at http://%s:%d\n " , ip , port )
91+ }
92+ }
9393}
9494
9595func getLocalIPs () ([]string , error ) {
96- var ips []string
97- addrs , err := net .InterfaceAddrs ()
98- if err != nil {
99- return nil , fmt .Errorf ("failed to get interface addresses: %v" , err )
100- }
101-
102- for _ , addr := range addrs {
103- if ipNet , ok := addr .(* net.IPNet ); ok {
104- if ipNet .IP .To4 () != nil && ! ipNet .IP .IsLoopback () {
105- ips = append (ips , ipNet .IP .String ())
106- }
107- }
108- }
109- return ips , nil
96+ var ips []string
97+ addrs , err := net .InterfaceAddrs ()
98+ if err != nil {
99+ return nil , fmt .Errorf ("failed to get interface addresses: %v" , err )
100+ }
101+
102+ for _ , addr := range addrs {
103+ if ipNet , ok := addr .(* net.IPNet ); ok {
104+ if ipNet .IP .To4 () != nil && ! ipNet .IP .IsLoopback () {
105+ ips = append (ips , ipNet .IP .String ())
106+ }
107+ }
108+ }
109+ return ips , nil
110110}
0 commit comments