Skip to content
Snippets Groups Projects
Commit ed63be57 authored by Josh Brooks's avatar Josh Brooks
Browse files

Constant flags and clean exit code

parent 16c3db3f
No related branches found
No related tags found
2 merge requests!510Release,!275Implement connection CLI
...@@ -17,6 +17,9 @@ import ( ...@@ -17,6 +17,9 @@ import (
"gitlab.com/elixxir/client/e2e" "gitlab.com/elixxir/client/e2e"
"gitlab.com/elixxir/client/e2e/receive" "gitlab.com/elixxir/client/e2e/receive"
"gitlab.com/elixxir/client/xxdk" "gitlab.com/elixxir/client/xxdk"
"os"
"os/signal"
"syscall"
"time" "time"
) )
...@@ -36,8 +39,8 @@ var connectionCmd = &cobra.Command{ ...@@ -36,8 +39,8 @@ var connectionCmd = &cobra.Command{
regCode := viper.GetString("regcode") regCode := viper.GetString("regcode")
cmixParams, e2eParams := initParams() cmixParams, e2eParams := initParams()
forceLegacy := viper.GetBool("force-legacy") forceLegacy := viper.GetBool("force-legacy")
if viper.GetBool("startServer") { if viper.GetBool(connectionStartServerFlag) {
if viper.GetBool("authenticated") { if viper.GetBool(connectionAuthenticatedFlag) {
secureConnServer(forceLegacy, statePass, statePath, regCode, secureConnServer(forceLegacy, statePass, statePath, regCode,
cmixParams, e2eParams) cmixParams, e2eParams)
} else { } else {
...@@ -45,7 +48,7 @@ var connectionCmd = &cobra.Command{ ...@@ -45,7 +48,7 @@ var connectionCmd = &cobra.Command{
cmixParams, e2eParams) cmixParams, e2eParams)
} }
} else { } else {
if viper.GetBool("authenticated") { if viper.GetBool(connectionAuthenticatedFlag) {
secureConnClient(forceLegacy, statePass, statePath, regCode, secureConnClient(forceLegacy, statePass, statePath, regCode,
cmixParams, e2eParams) cmixParams, e2eParams)
} else { } else {
...@@ -152,7 +155,7 @@ func secureConnServer(forceLegacy bool, statePass []byte, statePath, regCode str ...@@ -152,7 +155,7 @@ func secureConnServer(forceLegacy bool, statePass []byte, statePath, regCode str
} }
// Keep server running to receive messages------------------------------------ // Keep server running to receive messages------------------------------------
serverTimeout := viper.GetDuration("serverTimeout") serverTimeout := viper.GetDuration(connectionServerTimeoutFlag)
if serverTimeout != 0 { if serverTimeout != 0 {
timer := time.NewTimer(serverTimeout) timer := time.NewTimer(serverTimeout)
select { select {
...@@ -163,8 +166,21 @@ func secureConnServer(forceLegacy bool, statePass []byte, statePath, regCode str ...@@ -163,8 +166,21 @@ func secureConnServer(forceLegacy bool, statePass []byte, statePath, regCode str
} }
} }
// If timeout is not specified, leave as long-running thread // Keep app running to receive messages------------------------------------
select {}
// Wait until the user terminates the program
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
err = connectServer.Messenger.StopNetworkFollower()
if err != nil {
jww.ERROR.Printf("Failed to stop network follower: %+v", err)
} else {
jww.INFO.Printf("Stopped network follower.")
}
os.Exit(0)
} }
...@@ -257,8 +273,8 @@ func insecureConnServer(forceLegacy bool, statePass []byte, statePath, regCode s ...@@ -257,8 +273,8 @@ func insecureConnServer(forceLegacy bool, statePass []byte, statePath, regCode s
} }
// Keep server running to receive messages------------------------------------ // Keep server running to receive messages------------------------------------
if viper.GetDuration("serverTimeout") != 0 { if viper.GetDuration(connectionServerTimeoutFlag) != 0 {
timer := time.NewTimer(viper.GetDuration("serverTimeout")) timer := time.NewTimer(viper.GetDuration(connectionServerTimeoutFlag))
select { select {
case <-timer.C: case <-timer.C:
fmt.Println("Shutting down connection server") fmt.Println("Shutting down connection server")
...@@ -266,9 +282,21 @@ func insecureConnServer(forceLegacy bool, statePass []byte, statePath, regCode s ...@@ -266,9 +282,21 @@ func insecureConnServer(forceLegacy bool, statePass []byte, statePath, regCode s
return return
} }
} }
// Keep app running to receive messages------------------------------------
// If timeout is not specified, leave as long-running thread // Wait until the user terminates the program
select {} c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
err = connectServer.Messenger.StopNetworkFollower()
if err != nil {
jww.ERROR.Printf("Failed to stop network follower: %+v", err)
} else {
jww.INFO.Printf("Stopped network follower.")
}
os.Exit(0)
} }
...@@ -321,7 +349,7 @@ func secureConnClient(forceLegacy bool, statePass []byte, statePath, regCode str ...@@ -321,7 +349,7 @@ func secureConnClient(forceLegacy bool, statePass []byte, statePath, regCode str
waitUntilConnected(connected) waitUntilConnected(connected)
// Connect with the server------------------------------------------------- // Connect with the server-------------------------------------------------
contactPath := viper.GetString("connect") contactPath := viper.GetString(connectionFlag)
serverContact := getContactFromFile(contactPath) serverContact := getContactFromFile(contactPath)
fmt.Println("Sending connection request") fmt.Println("Sending connection request")
...@@ -387,7 +415,7 @@ func insecureConnClient(forceLegacy bool, statePass []byte, statePath, regCode s ...@@ -387,7 +415,7 @@ func insecureConnClient(forceLegacy bool, statePass []byte, statePath, regCode s
waitUntilConnected(connected) waitUntilConnected(connected)
// Connect with the server------------------------------------------------- // Connect with the server-------------------------------------------------
contactPath := viper.GetString("connect") contactPath := viper.GetString(connectionFlag)
serverContact := getContactFromFile(contactPath) serverContact := getContactFromFile(contactPath)
fmt.Println("Sending connection request") fmt.Println("Sending connection request")
jww.INFO.Printf("[CONN] Sending connection request to %s", jww.INFO.Printf("[CONN] Sending connection request to %s",
...@@ -445,7 +473,7 @@ func miscConnectionFunctions(client *xxdk.E2e, conn connect.Connection) { ...@@ -445,7 +473,7 @@ func miscConnectionFunctions(client *xxdk.E2e, conn connect.Connection) {
} }
// Disconnect from connection partner-------------------------------------------- // Disconnect from connection partner--------------------------------------------
if viper.GetBool("disconnect") { if viper.GetBool(connectionDisconnectFlag) {
// Close the connection // Close the connection
if err := conn.Close(); err != nil { if err := conn.Close(); err != nil {
jww.FATAL.Panicf("Failed to disconnect with %s: %v", jww.FATAL.Panicf("Failed to disconnect with %s: %v",
...@@ -486,7 +514,7 @@ func (l listener) Name() string { ...@@ -486,7 +514,7 @@ func (l listener) Name() string {
// init initializes commands and flags for Cobra. // init initializes commands and flags for Cobra.
func init() { func init() {
connectionCmd.Flags().String("connect", "", connectionCmd.Flags().String(connectionFlag, "",
"This flag is a client side operation. "+ "This flag is a client side operation. "+
"This flag expects a path to a contact file (similar "+ "This flag expects a path to a contact file (similar "+
"to destfile). It will parse this into an contact object,"+ "to destfile). It will parse this into an contact object,"+
...@@ -495,51 +523,36 @@ func init() { ...@@ -495,51 +523,36 @@ func init() {
"If a connection already exists between "+ "If a connection already exists between "+
"the client and the server, this will be used instead of "+ "the client and the server, this will be used instead of "+
"resending a connection request to the server.") "resending a connection request to the server.")
err := viper.BindPFlag("connect", connectionCmd.Flags().Lookup("connect")) bindFlagHelper(connectionFlag, connectionCmd)
if err != nil {
jww.ERROR.Printf("viper.BindPFlag failed for %q: %+v", "connect", err)
}
connectionCmd.Flags().Bool("startServer", false, connectionCmd.Flags().Bool(connectionStartServerFlag, false,
"This flag is a server-side operation and takes no arguments. "+ "This flag is a server-side operation and takes no arguments. "+
"This initiates a connection server. "+ "This initiates a connection server. "+
"Calling this flag will have this process call "+ "Calling this flag will have this process call "+
"connection.StartServer().") "connection.StartServer().")
err = viper.BindPFlag("startServer", connectionCmd.Flags().Lookup("startServer")) bindFlagHelper(connectionStartServerFlag, connectionCmd)
if err != nil {
jww.ERROR.Printf("viper.BindPFlag failed for %q: %+v", "startServer", err)
}
connectionCmd.Flags().Duration("serverTimeout", time.Duration(0), connectionCmd.Flags().Duration(connectionServerTimeoutFlag, time.Duration(0),
"This flag is a connection parameter. "+ "This flag is a connection parameter. "+
"This takes as an argument a time.Duration. "+ "This takes as an argument a time.Duration. "+
"This duration specifies how long a server will run before "+ "This duration specifies how long a server will run before "+
"closing. Without this flag present, a server will be "+ "closing. Without this flag present, a server will be "+
"long-running.") "long-running.")
err = viper.BindPFlag("serverTimeout", connectionCmd.Flags().Lookup("serverTimeout")) bindFlagHelper(connectionServerTimeoutFlag, connectionCmd)
if err != nil {
jww.ERROR.Printf("viper.BindPFlag failed for %q: %+v", "serverTimeout", err)
}
connectionCmd.Flags().Bool("disconnect", false, connectionCmd.Flags().Bool(connectionDisconnectFlag, false,
"This flag is available to both server and client. "+ "This flag is available to both server and client. "+
"This uses a contact object from a file specified by --destfile."+ "This uses a contact object from a file specified by --destfile."+
"This will close the connection with the given contact "+ "This will close the connection with the given contact "+
"if it exists.") "if it exists.")
err = viper.BindPFlag("disconnect", connectionCmd.Flags().Lookup("disconnect")) bindFlagHelper(connectionDisconnectFlag, connectionCmd)
if err != nil {
jww.ERROR.Printf("viper.BindPFlag failed for %q: %+v", "disconnect", err)
}
connectionCmd.Flags().Bool("authenticated", false, connectionCmd.Flags().Bool(connectionAuthenticatedFlag, false,
"This flag is available to both server and client. "+ "This flag is available to both server and client. "+
"This flag operates as a switch for the authenticated code-path. "+ "This flag operates as a switch for the authenticated code-path. "+
"With this flag present, any additional connection related flags"+ "With this flag present, any additional connection related flags"+
" will call the applicable authenticated counterpart") " will call the applicable authenticated counterpart")
err = viper.BindPFlag("authenticated", connectionCmd.Flags().Lookup("authenticated")) bindFlagHelper(connectionAuthenticatedFlag, connectionCmd)
if err != nil {
jww.ERROR.Printf("viper.BindPFlag failed for %q: %+v", "authenticated", err)
}
rootCmd.AddCommand(connectionCmd) rootCmd.AddCommand(connectionCmd)
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment