2020-05-03 08:18:28 +00:00
package main
2021-01-18 17:35:13 +00:00
import (
"context"
2021-01-20 17:39:57 +00:00
"fmt"
"os"
2021-01-18 17:35:13 +00:00
"os/user"
"time"
"github.com/google/uuid"
"github.com/spf13/cobra"
"gitlab.com/inetmock/inetmock/internal/app"
"google.golang.org/grpc"
)
var (
inetMockSocketPath string
outputFormat string
grpcTimeout time . Duration
cliApp app . App
conn * grpc . ClientConn
)
2020-05-03 08:18:28 +00:00
func main ( ) {
2021-01-18 17:35:13 +00:00
endpointsCmd . AddCommand ( getEndpoints )
handlerCmd . AddCommand ( getHandlersCmd )
healthCmd . AddCommand ( generalHealthCmd , containerHealthCmd )
cliApp = app . NewApp ( "imctl" , "IMCTL is the CLI app to interact with an INetMock server" ) .
WithCommands ( endpointsCmd , handlerCmd , healthCmd , auditCmd ) .
WithInitTasks ( func ( _ * cobra . Command , _ [ ] string ) ( err error ) {
return initGRPCConnection ( )
2021-01-20 17:39:57 +00:00
} ) .
WithLogger ( )
2021-01-18 17:35:13 +00:00
cliApp . RootCommand ( ) . PersistentFlags ( ) . StringVar ( & inetMockSocketPath , "socket-path" , "unix:///var/run/inetmock.sock" , "Path to the INetMock socket file" )
cliApp . RootCommand ( ) . PersistentFlags ( ) . StringVarP ( & outputFormat , "format" , "f" , "table" , "Output format to use. Possible values: table, json, yaml" )
cliApp . RootCommand ( ) . PersistentFlags ( ) . DurationVar ( & grpcTimeout , "grpc-timeout" , 5 * time . Second , "Timeout to connect to the gRPC API" )
currentUser := ""
if usr , err := user . Current ( ) ; err == nil {
currentUser = usr . Username
} else {
currentUser = uuid . New ( ) . String ( )
2020-05-03 08:18:28 +00:00
}
2021-01-18 17:35:13 +00:00
2021-01-20 17:39:57 +00:00
hostname := "."
if hn , err := os . Hostname ( ) ; err == nil {
hostname = hn
}
watchEventsCmd . PersistentFlags ( ) . StringVar ( & listenerName , "listener-name" , fmt . Sprintf ( "%s\\%s is watching" , hostname , currentUser ) , "set listener name - defaults to the current username, if the user cannot be determined a random UUID will be used" )
auditCmd . AddCommand ( listSinksCmd , watchEventsCmd , addFileCmd , removeFileCmd , readFileCmd )
2021-01-18 17:35:13 +00:00
cliApp . MustRun ( )
}
func initGRPCConnection ( ) ( err error ) {
dialCtx , cancel := context . WithTimeout ( cliApp . Context ( ) , grpcTimeout )
conn , err = grpc . DialContext ( dialCtx , inetMockSocketPath , grpc . WithInsecure ( ) )
cancel ( )
return
2020-05-03 08:18:28 +00:00
}