2020-05-03 08:18:28 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
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"
|
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",
|
|
|
|
}
|
|
|
|
|
|
|
|
inetMockSocketPath string
|
|
|
|
outputFormat string
|
|
|
|
grpcTimeout time.Duration
|
2021-01-13 20:38:52 +00:00
|
|
|
appCtx context.Context
|
|
|
|
appCancel context.CancelFunc
|
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)
|
2020-06-23 12:54:08 +00:00
|
|
|
healthCmd.AddCommand(generalHealthCmd)
|
2020-06-15 10:04:08 +00:00
|
|
|
healthCmd.AddCommand(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)
|
|
|
|
|
|
|
|
appCtx, appCancel = initAppContext()
|
2020-05-03 08:18:28 +00:00
|
|
|
}
|
2020-06-23 12:54:08 +00:00
|
|
|
|
|
|
|
func ExecuteClientCommand() error {
|
2021-01-13 20:38:52 +00:00
|
|
|
defer appCancel()
|
2020-06-23 12:54:08 +00:00
|
|
|
return cliCmd.Execute()
|
|
|
|
}
|
2021-01-13 20:38:52 +00:00
|
|
|
|
|
|
|
func initAppContext() (context.Context, context.CancelFunc) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
signals := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-signals
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ctx, cancel
|
|
|
|
}
|