2021-01-16 16:34:50 +00:00
|
|
|
package main
|
2020-05-03 08:18:28 +00:00
|
|
|
|
|
|
|
import (
|
2021-01-13 20:38:52 +00:00
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"os/user"
|
|
|
|
"syscall"
|
2020-05-03 08:18:28 +00:00
|
|
|
"time"
|
2020-12-26 13:11:49 +00:00
|
|
|
|
2021-01-13 20:38:52 +00:00
|
|
|
"github.com/google/uuid"
|
2020-12-26 13:11:49 +00:00
|
|
|
"github.com/spf13/cobra"
|
2021-01-16 16:34:50 +00:00
|
|
|
"google.golang.org/grpc"
|
2020-05-03 08:18:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cliCmd = &cobra.Command{
|
|
|
|
Use: "",
|
|
|
|
Short: "IMCTL is the CLI app to interact with an INetMock server",
|
2021-01-16 16:34:50 +00:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return initGRPCConnection()
|
|
|
|
},
|
2020-05-03 08:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inetMockSocketPath string
|
|
|
|
outputFormat string
|
|
|
|
grpcTimeout time.Duration
|
2021-01-13 20:38:52 +00:00
|
|
|
appCtx context.Context
|
|
|
|
appCancel context.CancelFunc
|
2021-01-16 16:34:50 +00:00
|
|
|
conn *grpc.ClientConn
|
2020-05-03 08:18:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-01-13 20:38:52 +00:00
|
|
|
cliCmd.PersistentFlags().StringVar(&inetMockSocketPath, "socket-path", "unix:///var/run/inetmock.sock", "Path to the INetMock socket file")
|
2020-05-03 08:18:28 +00:00
|
|
|
cliCmd.PersistentFlags().StringVarP(&outputFormat, "format", "f", "table", "Output format to use. Possible values: table, json, yaml")
|
|
|
|
cliCmd.PersistentFlags().DurationVar(&grpcTimeout, "grpc-timeout", 5*time.Second, "Timeout to connect to the gRPC API")
|
|
|
|
|
2021-01-13 20:38:52 +00:00
|
|
|
cliCmd.AddCommand(endpointsCmd, handlerCmd, healthCmd, auditCmd)
|
2020-05-03 08:18:28 +00:00
|
|
|
endpointsCmd.AddCommand(getEndpoints)
|
|
|
|
handlerCmd.AddCommand(getHandlersCmd)
|
2021-01-16 16:34:50 +00:00
|
|
|
healthCmd.AddCommand(generalHealthCmd, containerHealthCmd)
|
2021-01-13 20:38:52 +00:00
|
|
|
|
|
|
|
currentUser := ""
|
|
|
|
if usr, err := user.Current(); err == nil {
|
|
|
|
currentUser = usr.Username
|
|
|
|
} else {
|
|
|
|
currentUser = uuid.New().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
watchEventsCmd.PersistentFlags().StringVar(
|
|
|
|
&listenerName,
|
|
|
|
"listener-name",
|
|
|
|
currentUser,
|
|
|
|
"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)
|
2020-06-23 12:54:08 +00:00
|
|
|
}
|
2021-01-13 20:38:52 +00:00
|
|
|
|
2021-01-16 16:34:50 +00:00
|
|
|
func initGRPCConnection() (err error) {
|
|
|
|
appCtx, appCancel = context.WithCancel(context.Background())
|
2021-01-13 20:38:52 +00:00
|
|
|
|
|
|
|
signals := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-signals
|
2021-01-16 16:34:50 +00:00
|
|
|
appCancel()
|
2021-01-13 20:38:52 +00:00
|
|
|
}()
|
|
|
|
|
2021-01-16 16:34:50 +00:00
|
|
|
dialCtx, cancel := context.WithTimeout(appCtx, grpcTimeout)
|
|
|
|
conn, err = grpc.DialContext(dialCtx, inetMockSocketPath, grpc.WithInsecure())
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
return
|
2021-01-13 20:38:52 +00:00
|
|
|
}
|