Skip to content

Conversation

@yixinshark
Copy link
Contributor

  • Add CommandExecutor class for executing arbitrary commands via DBus
  • Refactor main() into handleList, handleExecuteCommand, handleLaunchApp
  • Add --command/-c option with --type, --run-id, --workdir parameters
  • Add validation to require appId when not using --command
  • Improve help text with Usage section and examples
  • Update documentation with command execution examples

Log: add command execution support to dde-am

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @yixinshark, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: yixinshark

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@yixinshark yixinshark force-pushed the feat-addCommondSupport branch 2 times, most recently from 68ca09d to 4e342db Compare December 22, 2025 06:02
@yixinshark yixinshark force-pushed the feat-addCommondSupport branch from 4e342db to 9fc4161 Compare December 24, 2025 10:20
- Add CommandExecutor class for executing arbitrary commands via DBus
- Refactor main() into handleList, handleExecuteCommand, handleLaunchApp
- Add --command/-c option with --type, --run-id, --workdir parameters
- Add validation to require appId when not using --command
- Improve help text with Usage section and examples
- Update documentation with command execution examples

Log: add command execution support to dde-am
@yixinshark yixinshark force-pushed the feat-addCommondSupport branch from 9fc4161 to 2aca98c Compare December 24, 2025 10:22
@deepin-ci-robot
Copy link

deepin pr auto review

我来对这段代码进行审查,主要从语法逻辑、代码质量、性能和安全这几个方面进行分析:

  1. 语法逻辑:
  • 代码整体结构清晰,语法正确
  • CommandExecutor 类的接口设计合理,职责单一
  • main 函数重构后逻辑更清晰,将不同功能分离到不同处理函数中
  1. 代码质量:
  • 优点:

    • 使用了 DExpected 进行错误处理,提高了代码的健壮性
    • 增加了详细的帮助信息,提高了用户体验
    • 代码结构模块化,便于维护
    • 添加了适当的注释和文档
  • 可改进之处:

    • CommandExecutor 类缺少构造函数初始化成员变量
    • 可以考虑为 CommandExecutor 添加参数验证
    • execute() 方法中的 DBus 类型注册可以移到类初始化时
  1. 性能:
  • DBus 类型注册每次调用 execute() 都会执行,建议移到静态初始化区域
  • 环境变量解析使用循环和字符串操作,对于大量环境变量可能影响性能
  1. 安全性:
  • CommandExecutor 直接执行命令,建议添加:
    • 命令路径验证
    • 参数合法性检查
    • 工作目录访问权限验证
    • 环境变量值的安全性检查

具体改进建议:

// commandexecutor.h 改进
class CommandExecutor {
public:
    CommandExecutor() = default;  // 添加默认构造函数
    
    // 添加参数验证
    bool isValid() const;
    
    // ... 其他方法保持不变 ...

private:
    // 添加验证方法
    bool validateProgram() const;
    bool validateWorkDir() const;
    bool validateEnvironment() const;
};

// commandexecutor.cpp 改进
CommandExecutor::CommandExecutor() {
    // 在构造函数中注册 DBus 类型
    static bool typesRegistered = false;
    if (!typesRegistered) {
        registerComplexDbusType();
        typesRegistered = true;
    }
}

bool CommandExecutor::isValid() const {
    return validateProgram() && 
           validateWorkDir() && 
           validateEnvironment();
}

bool CommandExecutor::validateProgram() const {
    // 验证程序路径是否存在且可执行
    QFileInfo info(m_program);
    return info.exists() && info.isExecutable();
}

bool CommandExecutor::validateWorkDir() const {
    if (m_workdir.isEmpty()) return true;
    QFileInfo info(m_workdir);
    return info.exists() && info.isDir();
}

bool CommandExecutor::validateEnvironment() const {
    // 验证环境变量格式
    for (const auto &env : m_environmentVariables) {
        if (!env.contains('=') || env.startsWith('=')) {
            return false;
        }
    }
    return true;
}

DExpected<void> CommandExecutor::execute() {
    if (!isValid()) {
        return DUnexpected{emplace_tag::USE_EMPLACE, -1, 
                          QString("Invalid command parameters")};
    }
    
    // ... 其余执行逻辑 ...
}

// main.cpp 改进
int handleExecuteCommand(const QCommandLineParser &parser,
                        const QCommandLineOption &executeOption,
                        const QCommandLineOption &typeOption,
                        const QCommandLineOption &runIdOption,
                        const QCommandLineOption &workdirOption,
                        const QCommandLineOption &envOption) {
    CommandExecutor executor;
    
    QString program = parser.value(executeOption);
    if (program.isEmpty()) {
        qCritical() << "Error: No program specified";
        return 1;
    }
    
    // 验证程序路径
    if (!QFileInfo(program).exists()) {
        qCritical() << "Error: Program does not exist:" << program;
        return 1;
    }
    
    executor.setProgram(program);
    
    // ... 其余参数设置 ...
    
    // 执行前验证
    if (!executor.isValid()) {
        qCritical() << "Error: Invalid command parameters";
        return 1;
    }
    
    auto ret = executor.execute();
    if (!ret) {
        qWarning() << ret.error();
        return ret.error().getErrorCode();
    }
    
    return 0;
}

这些改进主要关注:

  1. 增加了参数验证和安全性检查
  2. 优化了 DBus 类型注册的性能
  3. 提高了代码的健壮性和可维护性
  4. 增加了错误处理和用户提示

建议在后续开发中:

  1. 添加单元测试覆盖这些验证逻辑
  2. 考虑添加日志记录功能
  3. 可以考虑添加命令执行超时机制
  4. 增加对特殊字符和注入攻击的防护

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants