From e3c757dcaea22fd75cdd065fca116f7ddbf4d9fc Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Tue, 5 Dec 2023 11:29:03 -0500 Subject: [PATCH] Store chip-tool interactive history in the general chip-tool storage directory. (#30803) Addresses part of https://github.com/project-chip/connectedhomeip/issues/30790 Co-authored-by: Andrei Litvin --- .../interactive/InteractiveCommands.cpp | 32 ++++++++++++++++--- .../interactive/InteractiveCommands.h | 4 +++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/examples/chip-tool/commands/interactive/InteractiveCommands.cpp b/examples/chip-tool/commands/interactive/InteractiveCommands.cpp index 9856b9af1bbd1a..d0f27acad60d34 100644 --- a/examples/chip-tool/commands/interactive/InteractiveCommands.cpp +++ b/examples/chip-tool/commands/interactive/InteractiveCommands.cpp @@ -23,7 +23,7 @@ #include constexpr char kInteractiveModePrompt[] = ">>> "; -constexpr char kInteractiveModeHistoryFilePath[] = "/tmp/chip_tool_history"; +constexpr char kInteractiveModeHistoryFileName[] = "chip_tool_history"; constexpr char kInteractiveModeStopCommand[] = "quit()"; constexpr char kCategoryError[] = "Error"; constexpr char kCategoryProgress[] = "Info"; @@ -257,7 +257,9 @@ void ENFORCE_FORMAT(3, 0) InteractiveServerLoggingCallback(const char * module, gInteractiveServerResult.MaybeAddLog(module, category, base64Message); } -char * GetCommand(char * command) +} // namespace + +char * InteractiveStartCommand::GetCommand(char * command) { if (command != nullptr) { @@ -271,12 +273,32 @@ char * GetCommand(char * command) if (command != nullptr && *command) { add_history(command); - write_history(kInteractiveModeHistoryFilePath); + write_history(GetHistoryFilePath().c_str()); } return command; } -} // namespace + +std::string InteractiveStartCommand::GetHistoryFilePath() const +{ + std::string storageDir; + if (GetStorageDirectory().HasValue()) + { + storageDir = GetStorageDirectory().Value(); + } + else + { + // Match what GetFilename in ExamplePersistentStorage.cpp does. + const char * dir = getenv("TMPDIR"); + if (dir == nullptr) + { + dir = "/tmp"; + } + storageDir = dir; + } + + return storageDir + "/" + kInteractiveModeHistoryFileName; +} CHIP_ERROR InteractiveServerCommand::RunCommand() { @@ -329,7 +351,7 @@ CHIP_ERROR InteractiveServerCommand::LogJSON(const char * json) CHIP_ERROR InteractiveStartCommand::RunCommand() { - read_history(kInteractiveModeHistoryFilePath); + read_history(GetHistoryFilePath().c_str()); // Logs needs to be redirected in order to refresh the screen appropriately when something // is dumped to stdout while the user is typing a command. diff --git a/examples/chip-tool/commands/interactive/InteractiveCommands.h b/examples/chip-tool/commands/interactive/InteractiveCommands.h index 85cc53c92b487a..88a33dcb26c96f 100644 --- a/examples/chip-tool/commands/interactive/InteractiveCommands.h +++ b/examples/chip-tool/commands/interactive/InteractiveCommands.h @@ -59,6 +59,10 @@ class InteractiveStartCommand : public InteractiveCommand /////////// CHIPCommand Interface ///////// CHIP_ERROR RunCommand() override; + +private: + char * GetCommand(char * command); + std::string GetHistoryFilePath() const; }; class InteractiveServerCommand : public InteractiveCommand, public WebSocketServerDelegate, public RemoteDataModelLoggerDelegate