Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store chip-tool interactive history in the general chip-tool storage directory. #30803

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions examples/chip-tool/commands/interactive/InteractiveCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <editline.h>

constexpr const char * kInteractiveModePrompt = ">>> ";
constexpr const char * kInteractiveModeHistoryFilePath = "/tmp/chip_tool_history";
constexpr const char * kInteractiveModeHistoryFileName = "chip_tool_history";
constexpr const char * kInteractiveModeStopCommand = "quit()";
constexpr const char * kCategoryError = "Error";
constexpr const char * kCategoryProgress = "Info";
Expand Down Expand Up @@ -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)
{
Expand All @@ -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()
{
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions examples/chip-tool/commands/interactive/InteractiveCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading