Skip to content

Commit e128fcd

Browse files
mymldengbo11
authored andcommitted
feat: add workdir option to run commands
Add --workdir option to ll-builder and ll-cli run commands to specify working directory inside the application container 1. Add workdir field to RunCommandOptions and RunOptions structs 2. Update Builder::run method to accept workdir parameter and set process.cwd 3. Update Cli::run method to handle workdir option and validate absolute path 4. Add CLI option parsing for --workdir in both ll-builder and ll-cli 5. Validate that workdir path is absolute before setting in container process 6. Maintain backward compatibility with empty workdir (default behavior) Log: Added --workdir option to specify working directory inside container when running applications Influence: 1. Test ll-builder run --workdir=/path/to/dir with absolute paths 2. Test ll-cli run <app> --workdir=/path/to/dir with absolute paths 3. Verify error handling when providing relative paths 4. Test empty workdir to ensure default behavior unchanged 5. Test with various commands and modules to ensure workdir is properly set 6. Verify workdir persists through command execution within container feat: 为运行命令添加工作目录选项 为 ll-builder 和 ll-cli 的 run 命令添加 --workdir 选项,用于指定应用程序 容器内的工作目录 1. 在 RunCommandOptions 和 RunOptions 结构体中添加 workdir 字段 2. 更新 Builder::run 方法以接受 workdir 参数并设置 process.cwd 3. 更新 Cli::run 方法以处理 workdir 选项并验证绝对路径 4. 在 ll-builder 和 ll-cli 中添加 CLI 选项解析支持 --workdir 5. 在容器进程中设置工作目录前验证路径是否为绝对路径 6. 保持向后兼容性,空工作目录使用默认行为 Log: 新增 --workdir 选项用于在运行应用程序时指定容器内的工作目录 Influence: 1. 测试 ll-builder run --workdir=/path/to/dir 使用绝对路径 2. 测试 ll-cli run <app> --workdir=/path/to/dir 使用绝对路径 3. 验证提供相对路径时的错误处理 4. 测试空工作目录以确保默认行为不变 5. 使用各种命令和模块测试工作目录是否正确设置 6. 验证工作目录在容器内的命令执行过程中保持不变
1 parent fd0b5ca commit e128fcd

7 files changed

Lines changed: 30 additions & 4 deletions

File tree

apps/ll-builder/src/command_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct RunCommandOptions
2929
std::vector<std::string> execModules;
3030
std::vector<std::string> commands;
3131
bool debugMode = false;
32+
std::string workdir = "";
3233
std::vector<std::string> extensions;
3334
};
3435

apps/ll-builder/src/main.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,11 @@ int handleRun(linglong::builder::Builder &builder, const RunCommandOptions &opti
249249
}
250250
}
251251

252-
auto result = builder.run(modules, options.commands, options.debugMode, options.extensions);
252+
auto result = builder.run(modules,
253+
options.commands,
254+
options.debugMode,
255+
options.workdir,
256+
options.extensions);
253257
if (!result) {
254258
LogE("Run failed: {}", result.error());
255259
return result.error().code();
@@ -784,6 +788,8 @@ You can report bugs to the linyaps team under this project: https://github.com/O
784788
->delimiter(',')
785789
->allow_extra_args(false)
786790
->type_name("modules");
791+
buildRun->add_option("--workdir", runOpts.workdir, _("Working directory inside the app"))
792+
->type_name("PATH");
787793
buildRun->add_option(
788794
"COMMAND",
789795
runOpts.commands,

apps/ll-cli/src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ ll-cli run org.deepin.demo -- bash -x /path/to/bash/script)"));
179179
_("Specify the runtime used by the application to run"))
180180
->type_name("REF")
181181
->check(validatorString);
182+
cliRun->add_option("--workdir", runOptions.workdir, _("Working directory inside the app"))
183+
->type_name("PATH");
182184
cliRun
183185
->add_option("--extensions",
184186
runOptions.extensions,

libs/linglong/src/linglong/builder/linglong_builder.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,7 @@ utils::error::Result<void> Builder::importLayer(repo::OSTreeRepo &ostree,
17771777
utils::error::Result<void> Builder::run(std::vector<std::string> modules,
17781778
std::vector<std::string> args,
17791779
bool debug,
1780+
const std::string &workdir,
17801781
std::vector<std::string> extensions)
17811782
{
17821783
LINGLONG_TRACE("run application");
@@ -1950,7 +1951,12 @@ utils::error::Result<void> Builder::run(std::vector<std::string> modules,
19501951
}
19511952

19521953
ocppi::runtime::config::types::Process process;
1953-
1954+
if (!workdir.empty()) {
1955+
if (!std::filesystem::path(workdir).is_absolute()) {
1956+
return LINGLONG_ERR(fmt::format("Workdir must be an absolute path: {}", workdir));
1957+
}
1958+
process.cwd = workdir;
1959+
}
19541960
if (!args.empty()) {
19551961
process.args = std::move(args);
19561962
} else {

libs/linglong/src/linglong/builder/linglong_builder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class Builder
9797
auto run(std::vector<std::string> modules,
9898
std::vector<std::string> args,
9999
bool debug = false,
100+
const std::string &workdir = "",
100101
std::vector<std::string> extensions = {}) -> utils::error::Result<void>;
101102
auto runtimeCheck() -> utils::error::Result<void>;
102103
auto runFromRepo(const package::Reference &ref, const std::vector<std::string> &args)

libs/linglong/src/linglong/cli/cli.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,9 +715,18 @@ int Cli::run(const RunOptions &options)
715715
return -1;
716716
}
717717

718+
auto process = ocppi::runtime::config::types::Process{ .args = std::move(commands) };
719+
if (!options.workdir.value_or("").empty()) {
720+
auto workdir = std::filesystem::path(options.workdir.value());
721+
if (!workdir.is_absolute()) {
722+
auto msg = fmt::format("Workdir must be an absolute path: {}", workdir);
723+
this->printer.printErr(LINGLONG_ERRV(msg));
724+
return -1;
725+
}
726+
process.cwd = workdir;
727+
}
718728
ocppi::runtime::RunOption opt{};
719-
auto result =
720-
(*container)->run(ocppi::runtime::config::types::Process{ .args = std::move(commands) }, opt);
729+
auto result = (*container)->run(process, opt);
721730
if (!result) {
722731
this->printer.printErr(result.error());
723732
return -1;

libs/linglong/src/linglong/cli/cli.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct RunOptions
5454
std::vector<std::string> commands;
5555
std::optional<std::string> base;
5656
std::optional<std::string> runtime;
57+
std::optional<std::string> workdir;
5758
std::vector<std::string> extensions;
5859
bool privileged{ false };
5960
std::vector<std::string> capsAdd;

0 commit comments

Comments
 (0)