Moved code of commands to their main package
- add init code to reduce code duplication of connection setup
This commit is contained in:
parent
af31b1166a
commit
2f0f3edfdf
13 changed files with 75 additions and 103 deletions
|
@ -7,7 +7,7 @@ before:
|
|||
builds:
|
||||
- id: "inetmock"
|
||||
binary: inetmock
|
||||
main: ./cmd/inetmock/main.go
|
||||
main: ./cmd/inetmock/
|
||||
ldflags:
|
||||
- -w -s
|
||||
env:
|
||||
|
@ -19,7 +19,7 @@ builds:
|
|||
- amd64
|
||||
- id: "imctl"
|
||||
binary: imctl
|
||||
main: ./cmd/imctl/main.go
|
||||
main: ./cmd/imctl/
|
||||
ldflags:
|
||||
- -w -s
|
||||
goos:
|
||||
|
@ -65,7 +65,7 @@ release:
|
|||
name: inetmock
|
||||
|
||||
dockers:
|
||||
- binaries:
|
||||
- ids:
|
||||
- inetmock
|
||||
- imctl
|
||||
image_templates:
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package cmd
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
@ -43,13 +43,6 @@ func runAddFile(_ *cobra.Command, args []string) (err error) {
|
|||
}
|
||||
|
||||
func runRemoveFile(_ *cobra.Command, args []string) (err error) {
|
||||
var conn *grpc.ClientConn
|
||||
|
||||
if conn, err = grpc.Dial(inetMockSocketPath, grpc.WithInsecure()); err != nil {
|
||||
fmt.Printf("Failed to connecto INetMock socket: %v\n", err)
|
||||
os.Exit(10)
|
||||
}
|
||||
|
||||
auditClient := rpc.NewAuditClient(conn)
|
||||
ctx, cancel := context.WithTimeout(appCtx, grpcTimeout)
|
||||
defer cancel()
|
|
@ -1,4 +1,4 @@
|
|||
package cmd
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
@ -8,7 +8,6 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
"gitlab.com/inetmock/inetmock/internal/rpc"
|
||||
"gitlab.com/inetmock/inetmock/pkg/audit"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -27,13 +26,6 @@ var (
|
|||
)
|
||||
|
||||
func watchAuditEvents(_ *cobra.Command, _ []string) (err error) {
|
||||
var conn *grpc.ClientConn
|
||||
|
||||
if conn, err = grpc.Dial(inetMockSocketPath, grpc.WithInsecure()); err != nil {
|
||||
fmt.Printf("Failed to connecto INetMock socket: %v\n", err)
|
||||
os.Exit(10)
|
||||
}
|
||||
|
||||
auditClient := rpc.NewAuditClient(conn)
|
||||
|
||||
var watchClient rpc.Audit_WatchEventsClient
|
|
@ -1,4 +1,4 @@
|
|||
package cmd
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
@ -10,12 +10,16 @@ import (
|
|||
|
||||
"github.com/google/uuid"
|
||||
"github.com/spf13/cobra"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var (
|
||||
cliCmd = &cobra.Command{
|
||||
Use: "",
|
||||
Short: "IMCTL is the CLI app to interact with an INetMock server",
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
return initGRPCConnection()
|
||||
},
|
||||
}
|
||||
|
||||
inetMockSocketPath string
|
||||
|
@ -23,6 +27,7 @@ var (
|
|||
grpcTimeout time.Duration
|
||||
appCtx context.Context
|
||||
appCancel context.CancelFunc
|
||||
conn *grpc.ClientConn
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
@ -33,8 +38,7 @@ func init() {
|
|||
cliCmd.AddCommand(endpointsCmd, handlerCmd, healthCmd, auditCmd)
|
||||
endpointsCmd.AddCommand(getEndpoints)
|
||||
handlerCmd.AddCommand(getHandlersCmd)
|
||||
healthCmd.AddCommand(generalHealthCmd)
|
||||
healthCmd.AddCommand(containerHealthCmd)
|
||||
healthCmd.AddCommand(generalHealthCmd, containerHealthCmd)
|
||||
|
||||
currentUser := ""
|
||||
if usr, err := user.Current(); err == nil {
|
||||
|
@ -50,25 +54,22 @@ func init() {
|
|||
"set listener name - defaults to the current username, if the user cannot be determined a random UUID will be used",
|
||||
)
|
||||
auditCmd.AddCommand(watchEventsCmd, addFileCmd, removeFileCmd)
|
||||
|
||||
appCtx, appCancel = initAppContext()
|
||||
}
|
||||
|
||||
func ExecuteClientCommand() error {
|
||||
defer appCancel()
|
||||
return cliCmd.Execute()
|
||||
}
|
||||
|
||||
func initAppContext() (context.Context, context.CancelFunc) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
func initGRPCConnection() (err error) {
|
||||
appCtx, appCancel = context.WithCancel(context.Background())
|
||||
|
||||
signals := make(chan os.Signal, 1)
|
||||
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
||||
|
||||
go func() {
|
||||
<-signals
|
||||
cancel()
|
||||
appCancel()
|
||||
}()
|
||||
|
||||
return ctx, cancel
|
||||
dialCtx, cancel := context.WithTimeout(appCtx, grpcTimeout)
|
||||
conn, err = grpc.DialContext(dialCtx, inetMockSocketPath, grpc.WithInsecure())
|
||||
cancel()
|
||||
|
||||
return
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package cmd
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
@ -8,7 +8,6 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
"gitlab.com/inetmock/inetmock/internal/format"
|
||||
"gitlab.com/inetmock/inetmock/internal/rpc"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -51,12 +50,6 @@ func fromEndpoints(eps []*rpc.Endpoint) (out []*printableEndpoint) {
|
|||
}
|
||||
|
||||
func runGetEndpoints(_ *cobra.Command, _ []string) (err error) {
|
||||
var conn *grpc.ClientConn
|
||||
|
||||
if conn, err = grpc.Dial(inetMockSocketPath, grpc.WithInsecure()); err != nil {
|
||||
fmt.Printf("Failed to connecto INetMock socket: %v\n", err)
|
||||
os.Exit(10)
|
||||
}
|
||||
endpointsClient := rpc.NewEndpointsClient(conn)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
|
||||
defer cancel()
|
|
@ -1,4 +1,4 @@
|
|||
package cmd
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
@ -8,7 +8,6 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
"gitlab.com/inetmock/inetmock/internal/format"
|
||||
"gitlab.com/inetmock/inetmock/internal/rpc"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -39,15 +38,11 @@ func fromHandlers(hs []string) (handlers []*printableHandler) {
|
|||
}
|
||||
|
||||
func runGetHandlers(_ *cobra.Command, _ []string) {
|
||||
var err error
|
||||
var conn *grpc.ClientConn
|
||||
|
||||
if conn, err = grpc.Dial(inetMockSocketPath, grpc.WithInsecure()); err != nil {
|
||||
fmt.Printf("Failed to connecto INetMock socket: %v\n", err)
|
||||
os.Exit(10)
|
||||
}
|
||||
handlersClient := rpc.NewHandlersClient(conn)
|
||||
ctx, _ := context.WithTimeout(context.Background(), grpcTimeout)
|
||||
|
||||
ctx, cancel := context.WithTimeout(appCtx, grpcTimeout)
|
||||
defer cancel()
|
||||
var err error
|
||||
var handlersResp *rpc.GetHandlersResponse
|
||||
|
||||
if handlersResp, err = handlersClient.GetHandlers(ctx, &rpc.GetHandlersRequest{}); err != nil {
|
|
@ -1,4 +1,4 @@
|
|||
package cmd
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
@ -8,7 +8,6 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
"gitlab.com/inetmock/inetmock/internal/format"
|
||||
"gitlab.com/inetmock/inetmock/internal/rpc"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -55,15 +54,10 @@ func fromComponentsHealth(componentsHealth map[string]*rpc.ComponentHealth) (com
|
|||
}
|
||||
|
||||
func getHealthResult() (healthResp *rpc.HealthResponse, err error) {
|
||||
var conn *grpc.ClientConn
|
||||
|
||||
if conn, err = grpc.Dial(inetMockSocketPath, grpc.WithInsecure()); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var healthClient = rpc.NewHealthClient(conn)
|
||||
ctx, _ := context.WithTimeout(context.Background(), grpcTimeout)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
|
||||
healthResp, err = healthClient.GetHealth(ctx, &rpc.HealthRequest{})
|
||||
cancel()
|
||||
return
|
||||
}
|
||||
|
|
@ -1,9 +1,8 @@
|
|||
package main
|
||||
|
||||
import "gitlab.com/inetmock/inetmock/internal/cmd"
|
||||
|
||||
func main() {
|
||||
if err := cmd.ExecuteClientCommand(); err != nil {
|
||||
defer appCancel()
|
||||
if err := cliCmd.Execute(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package cmd
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
|
@ -1,14 +1,47 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"gitlab.com/inetmock/inetmock/internal/cmd"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gitlab.com/inetmock/inetmock/internal/app"
|
||||
"gitlab.com/inetmock/inetmock/internal/endpoint/handler/dns/mock"
|
||||
_ "gitlab.com/inetmock/inetmock/internal/endpoint/handler/dns/mock"
|
||||
_ "gitlab.com/inetmock/inetmock/internal/endpoint/handler/http/mock"
|
||||
mock2 "gitlab.com/inetmock/inetmock/internal/endpoint/handler/http/mock"
|
||||
"gitlab.com/inetmock/inetmock/internal/endpoint/handler/http/proxy"
|
||||
_ "gitlab.com/inetmock/inetmock/internal/endpoint/handler/http/proxy"
|
||||
"gitlab.com/inetmock/inetmock/internal/endpoint/handler/metrics"
|
||||
_ "gitlab.com/inetmock/inetmock/internal/endpoint/handler/metrics"
|
||||
"gitlab.com/inetmock/inetmock/internal/endpoint/handler/tls/interceptor"
|
||||
_ "gitlab.com/inetmock/inetmock/internal/endpoint/handler/tls/interceptor"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
server app.App
|
||||
)
|
||||
|
||||
func main() {
|
||||
cmd.ExecuteServerCommand()
|
||||
logger, _ := zap.NewProduction()
|
||||
defer func() {
|
||||
if err := logger.Sync(); err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
}()
|
||||
|
||||
var err error
|
||||
if server, err = app.NewApp(
|
||||
mock2.AddHTTPMock,
|
||||
mock.AddDNSMock,
|
||||
interceptor.AddTLSInterceptor,
|
||||
proxy.AddHTTPProxy,
|
||||
metrics.AddMetricsExporter,
|
||||
); err != nil {
|
||||
fmt.Println(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
server.
|
||||
WithCommands(serveCmd, generateCaCmd).
|
||||
MustRun()
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package cmd
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
|
@ -1,34 +0,0 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gitlab.com/inetmock/inetmock/internal/app"
|
||||
"gitlab.com/inetmock/inetmock/internal/endpoint/handler/dns/mock"
|
||||
mock2 "gitlab.com/inetmock/inetmock/internal/endpoint/handler/http/mock"
|
||||
"gitlab.com/inetmock/inetmock/internal/endpoint/handler/http/proxy"
|
||||
"gitlab.com/inetmock/inetmock/internal/endpoint/handler/metrics"
|
||||
"gitlab.com/inetmock/inetmock/internal/endpoint/handler/tls/interceptor"
|
||||
)
|
||||
|
||||
var (
|
||||
server app.App
|
||||
)
|
||||
|
||||
func ExecuteServerCommand() {
|
||||
var err error
|
||||
if server, err = app.NewApp(
|
||||
mock2.AddHTTPMock,
|
||||
mock.AddDNSMock,
|
||||
interceptor.AddTLSInterceptor,
|
||||
proxy.AddHTTPProxy,
|
||||
metrics.AddMetricsExporter,
|
||||
); err != nil {
|
||||
fmt.Println(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
server.
|
||||
WithCommands(serveCmd, generateCaCmd).
|
||||
MustRun()
|
||||
}
|
|
@ -12,8 +12,8 @@ const (
|
|||
)
|
||||
|
||||
var (
|
||||
defaultDistributeParallelization int
|
||||
generatorIdx int64 = 1
|
||||
defaultDistributeParallelization = runtime.NumCPU() / 2
|
||||
WithBufferSize = func(bufferSize int) EventStreamOption {
|
||||
return func(cfg *eventStreamCfg) {
|
||||
cfg.bufferSize = bufferSize
|
||||
|
@ -44,6 +44,12 @@ var (
|
|||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
if defaultDistributeParallelization = runtime.NumCPU() / 2; defaultDistributeParallelization < 2 {
|
||||
defaultDistributeParallelization = 2
|
||||
}
|
||||
}
|
||||
|
||||
type EventStreamOption func(cfg *eventStreamCfg)
|
||||
|
||||
type eventStreamCfg struct {
|
||||
|
|
Loading…
Reference in a new issue