Luna/shell/builtin/cd.cpp
apio 4439ef8ec6
All checks were successful
continuous-integration/drone/push Build is passing
sh: Add a system to easily add flexible shell builtins
2023-07-21 20:44:01 +02:00

25 lines
761 B
C++

#include "sh.h"
#include <luna/String.h>
#include <os/ArgumentParser.h>
#include <os/FileSystem.h>
shell_builtin_t builtin_cd = [](int argc, char** argv) -> Result<void> {
StringView directory;
os::ArgumentParser parser;
parser.set_should_exit_on_bad_usage(false);
parser.add_description("Change working directory (shell builtin)"_sv);
parser.add_system_program_info("cd"_sv);
parser.add_positional_argument(directory, "directory"_sv, false);
if (!TRY(parser.parse(argc, argv))) return {};
if (!directory.is_empty()) TRY(os::FileSystem::change_directory(directory));
else
{
auto home = TRY(os::FileSystem::home_directory());
TRY(os::FileSystem::change_directory(home.view()));
}
return {};
};