Closed trajan0x closed 3 months ago
To respect ctrl+c in the core/commandline
module, update the signal handling logic in shell.go
to cancel the context and allow the user to input a new command. Modify the Action
function in GenerateShellCommand
as follows:
Action: func(c *cli.Context) (err error) {
SetLogLevel(c);
console := cli.NewApp();
console.Commands = capturedCommands;
console.Action = func(c *cli.Context) error {
fmt.Printf("Command not found. Type 'help' for a list of commands or \"%s\", \"%s\" or \"%s\" to exit.\n", quitCommand, exitCommand, quitCommandShort);
return nil;
};
if c.Args().Len() == 0 {
err := console.RunContext(c.Context, strings.Fields("cmd help"));
if err != nil {
return errors.Wrap(err, "could not show help");
}
}
sigs := make(chan os.Signal);
ctx, cancel := context.WithCancel(c.Context);
go func() {
for sig := range sigs {
if sig == syscall.SIGINT || sig == syscall.SIGTERM {
cancel();
fmt.Printf("\n(type \"%s\", \"%s\" or \"%s\" to exit)\n\n >", quitCommand, exitCommand, quitCommandShort);
}
}
}();
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM);
defer func() {
signal.Stop(sigs);
close(sigs);
}();
if !shellAvailable {
fmt.Println("Shell is not available in this environment because /dev/tty is not available. This is expected in containerized images");
return nil;
}
interactive := newInteractiveClient(ctx, capturedCommands, console);
for {
p := prompt.New(
interactive.executor,
interactive.completor,
prompt.OptionCompletionWordSeparator(completer.FilePathCompletionSeparator),
prompt.OptionMaxSuggestion(3),
prompt.OptionLivePrefix(livePrefix),
);
p.Run();
}
}
the
core/commandline
module should respect ctrl+c and exit the current command by cancelling the context and allow the user to put in a new command