fix_: ability to turn on/off interactive mode in serve (#5318)

This commit is contained in:
Pablo Lopez 2024-06-10 13:21:09 +03:00 committed by GitHub
parent cd8f8aaf62
commit b434d50ec5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 14 deletions

View File

@ -30,7 +30,7 @@ You can also run `make status-cli` in the root directory to build the binary.
./status-cli serve -n charlie -p 8565 -a <alice-pubkey> ./status-cli serve -n charlie -p 8565 -a <alice-pubkey>
``` ```
You can send direct messages through terminal or JSON RPC. You can send direct messages through JSON RPC. If you also want to send messages through terminal enable `interactive` mode (with the `-i` flag)
JSON RPC examples: JSON RPC examples:

View File

@ -76,6 +76,11 @@ var ServeFlags = append([]cli.Flag{
Value: 8545, Value: 8545,
Usage: "HTTP Server port to listen on", Usage: "HTTP Server port to listen on",
}, },
&cli.BoolFlag{
Name: InteractiveFlag,
Aliases: []string{"i"},
Usage: "Use interactive mode to input the messages",
},
}, CommonFlags...) }, CommonFlags...)
var logger *zap.SugaredLogger var logger *zap.SugaredLogger

View File

@ -32,6 +32,8 @@ func serve(cCtx *cli.Context) error {
port := cCtx.Int(PortFlag) port := cCtx.Int(PortFlag)
apiModules := cCtx.String(APIModulesFlag) apiModules := cCtx.String(APIModulesFlag)
telemetryUrl := cCtx.String(TelemetryServerURLFlag) telemetryUrl := cCtx.String(TelemetryServerURLFlag)
interactive := cCtx.Bool(InteractiveFlag)
dest := cCtx.String(AddFlag)
cli, err := start(name, port, apiModules, telemetryUrl) cli, err := start(name, port, apiModules, telemetryUrl)
if err != nil { if err != nil {
@ -55,7 +57,7 @@ func serve(cCtx *cli.Context) error {
logger.Infof("message received: %v (ID=%v)", message.Text, message.ID) logger.Infof("message received: %v (ID=%v)", message.Text, message.ID)
// if request contact, accept it // if request contact, accept it
if message.ContentType == protobuf.ChatMessage_SYSTEM_MESSAGE_MUTUAL_EVENT_SENT { if message.ContentType == protobuf.ChatMessage_SYSTEM_MESSAGE_MUTUAL_EVENT_SENT {
if err = cli.sendContactRequestAcceptance(cCtx.Context, message.ID); err != nil { if err := cli.sendContactRequestAcceptance(cCtx.Context, message.ID); err != nil {
logger.Errorf("accepting contact request: %v", err) logger.Errorf("accepting contact request: %v", err)
return return
} }
@ -65,7 +67,6 @@ func serve(cCtx *cli.Context) error {
})) }))
// Send contact request // Send contact request
dest := cCtx.String(AddFlag)
if dest != "" { if dest != "" {
err := cli.sendContactRequest(cCtx.Context, dest) err := cli.sendContactRequest(cCtx.Context, dest)
if err != nil { if err != nil {
@ -76,17 +77,16 @@ func serve(cCtx *cli.Context) error {
// nightly testrunner looks for this log to consider node as started // nightly testrunner looks for this log to consider node as started
logger.Info("retrieve messages...") logger.Info("retrieve messages...")
ctx, cancel := context.WithCancel(cCtx.Context) if interactive {
go func() { ctx, cancel := context.WithCancel(cCtx.Context)
// Wait for signal to exit go func() {
sig := make(chan os.Signal, 1) waitForSigExit()
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) cancel()
<-sig }()
cancel() interactiveSendMessageLoop(ctx, cli)
}() } else {
waitForSigExit()
// Send message if mutual contact exists }
interactiveSendMessageLoop(ctx, cli)
logger.Info("Exiting") logger.Info("Exiting")
@ -99,3 +99,9 @@ type MobileSignalEvent struct {
Messages []*common.Message `json:"messages"` Messages []*common.Message `json:"messages"`
} `json:"event"` } `json:"event"`
} }
func waitForSigExit() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
<-sig
}