/* src/KMSubCommand.cpp
 *
 * Copyright (C) 2024 OpenKylin, Inc
 */

#include "KMSubCommand.h"

#include <gio/gio.h>

KMSubCommand::KMSubCommand()
{
}

KMSubCommand::~KMSubCommand()
{
}

void KMSubCommand::enableVfs()
{
    const char *oldEnv = ::getenv("GIO_USE_VFS");
    ::setenv("GIO_USE_VFS", "local", true);

    g_vfs_get_default();

    if (oldEnv)
    {
        ::setenv("GIO_USE_VFS", oldEnv, true);
    }
    else
    {
        ::unsetenv("GIO_USE_VFS");
    }
}

const std::vector<SubCommandEntry>& KMSubCommandFactory::subCommandEntrys()
{
    /**
     * brief: name为空串时，认为是一个子命令分组的开始
     */
    static std::vector<SubCommandEntry> subCommands = {

        { "new-project", "Create an new project, such as ${appid}.yaml and ${appid}.appdata.xml" },
        { "build", "Run a build command inside the build dir" },
        { "run", "Enter the build environment to execute commands, usually used for debugging" }

    };

    return subCommands;
}

std::unique_ptr<KMSubCommand> KMSubCommandFactory::createKMSubCommand(const std::string &subCommand)
{
    KMSubCommand *pObjetct = dynamic_cast<KMSubCommand *>(KMDynamicCreator::createObjectById("SubCommand_" + subCommand));
    return std::unique_ptr<KMSubCommand>(pObjetct);
}
