2018-10-07 03:38:44 +00:00
|
|
|
#pragma once
|
2015-06-10 02:40:03 +00:00
|
|
|
|
|
|
|
#include "ToolBase.hpp"
|
2017-12-29 07:56:31 +00:00
|
|
|
#include <cstdio>
|
2015-06-10 02:40:03 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
class ToolInit final : public ToolBase {
|
2021-06-30 18:20:45 +00:00
|
|
|
const std::string* m_dir = nullptr;
|
2015-06-10 02:40:03 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
public:
|
2019-08-20 02:49:24 +00:00
|
|
|
explicit ToolInit(const ToolPassInfo& info) : ToolBase(info) {
|
2018-12-08 05:18:42 +00:00
|
|
|
hecl::Sstat theStat;
|
2021-06-30 18:20:45 +00:00
|
|
|
const std::string* dir;
|
2018-12-08 05:18:42 +00:00
|
|
|
if (info.args.size())
|
|
|
|
dir = &info.args.front();
|
|
|
|
else
|
|
|
|
dir = &info.cwd;
|
2015-06-10 02:40:03 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
if (hecl::Stat(dir->c_str(), &theStat)) {
|
|
|
|
hecl::MakeDir(dir->c_str());
|
|
|
|
if (hecl::Stat(dir->c_str(), &theStat)) {
|
2021-06-30 18:20:45 +00:00
|
|
|
LogModule.report(logvisor::Fatal, FMT_STRING("unable to stat '{}'"), *dir);
|
2018-12-08 05:18:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!S_ISDIR(theStat.st_mode)) {
|
2021-06-30 18:20:45 +00:00
|
|
|
LogModule.report(logvisor::Fatal, FMT_STRING("'{}' is not a directory"), *dir);
|
2018-12-08 05:18:42 +00:00
|
|
|
return;
|
2015-06-10 02:40:03 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
std::string testPath = *dir + "/.hecl/beacon";
|
2018-12-08 05:18:42 +00:00
|
|
|
if (!hecl::Stat(testPath.c_str(), &theStat)) {
|
2021-06-30 18:20:45 +00:00
|
|
|
LogModule.report(logvisor::Fatal, FMT_STRING("project already exists at '{}'"), *dir);
|
2018-12-08 05:18:42 +00:00
|
|
|
return;
|
2015-06-10 02:40:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
m_dir = dir;
|
|
|
|
}
|
2015-06-10 02:40:03 +00:00
|
|
|
|
2019-08-20 02:34:12 +00:00
|
|
|
int run() override {
|
2018-12-08 05:18:42 +00:00
|
|
|
if (!m_dir)
|
|
|
|
return 1;
|
|
|
|
size_t ErrorRef = logvisor::ErrorCount;
|
|
|
|
hecl::Database::Project proj((hecl::ProjectRootPath(*m_dir)));
|
|
|
|
if (logvisor::ErrorCount > ErrorRef)
|
|
|
|
return 1;
|
2021-06-30 18:20:45 +00:00
|
|
|
LogModule.report(logvisor::Info, FMT_STRING("initialized project at '{}/.hecl'"), *m_dir);
|
2018-12-08 05:18:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-06-10 02:40:03 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
static void Help(HelpOutput& help) {
|
2021-06-30 18:20:45 +00:00
|
|
|
help.secHead("NAME");
|
2018-12-08 05:18:42 +00:00
|
|
|
help.beginWrap();
|
2021-06-30 18:20:45 +00:00
|
|
|
help.wrap("hecl-init - Initialize a brand-new project database\n");
|
2018-12-08 05:18:42 +00:00
|
|
|
help.endWrap();
|
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
help.secHead("SYNOPSIS");
|
2018-12-08 05:18:42 +00:00
|
|
|
help.beginWrap();
|
2021-06-30 18:20:45 +00:00
|
|
|
help.wrap("hecl init [<dir>]\n");
|
2018-12-08 05:18:42 +00:00
|
|
|
help.endWrap();
|
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
help.secHead("DESCRIPTION");
|
2018-12-08 05:18:42 +00:00
|
|
|
help.beginWrap();
|
2021-06-30 18:20:45 +00:00
|
|
|
help.wrap("Creates a ");
|
|
|
|
help.wrapBold(".hecl");
|
|
|
|
help.wrap(" directory within the selected directory with an initialized database index. "
|
|
|
|
"This constitutes an empty HECL project, ready for making stuff!!\n");
|
2018-12-08 05:18:42 +00:00
|
|
|
help.endWrap();
|
2015-06-10 02:40:03 +00:00
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
help.secHead("OPTIONS");
|
|
|
|
help.optionHead("<dir>", "group directory path");
|
2018-12-08 05:18:42 +00:00
|
|
|
help.beginWrap();
|
2021-06-30 18:20:45 +00:00
|
|
|
help.wrap("Directory to create new project database in. If not specified, current directory is used.\n");
|
2018-12-08 05:18:42 +00:00
|
|
|
help.endWrap();
|
|
|
|
}
|
2015-06-10 02:40:03 +00:00
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
std::string_view toolName() const override { return "init"sv; }
|
2015-06-10 02:40:03 +00:00
|
|
|
};
|