summaryrefslogtreecommitdiff
path: root/kel_cpp_boilerplate/c++/kel_cpp_boilerplate.cpp
blob: 2399f61b47cd797cdde8b1f16b1c26f308316be0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <forstio/error.hpp>

#include "blueprints.hpp"

#include <iostream>
#include <filesystem>
#include <fstream>

namespace kel {
saw::error_or<void> write_to_file(
	std::string_view file_path,
	std::string_view file_content
){
	namespace fs = std::filesystem;
	try {
		auto fpath = fs::path(file_path);
		if(fpath.has_parent_path()){
			fs::create_directories(fs::path(fpath).parent_path());
		}

		std::ofstream out(fpath, std::ios::binary);
		if(not out){
			return saw::make_error<saw::err::critical>("Couldn't create file");
		}
		out.write(file_content.data(), static_cast<std::streamsize>(file_content.size()));
		if(not out){
			return saw::make_error<saw::err::critical>("Couldn't write to file");
		}
	}catch(const std::exception& e){
		(void) e;
		return saw::make_error<saw::err::critical>("Couldn't create file somehow");
	}

	return saw::make_void();
}

saw::error_or<void> kel_main(int argc, char** argv){
	(void) argc;
	(void) argv;

	namespace fs = std::filesystem;

	try {
		fs::path cwd = fs::current_path();
		std::cout << "\nCurrent working is directory: " << cwd << "\n\n";

	} catch (const std::exception& e) {
		return saw::make_error<saw::err::critical>("Couldn't get the working directory");
	}
	{
		std::string confirm;
		std::cout << "Are you sure to proceed? Type 'yes' to continue: ";
		std::getline(std::cin, confirm);

		if (confirm != "yes") {
			std::cout<<"Didn't type \"yes\". Aborting..."<<std::endl;
			return saw::make_void();
		}
	}

	{
		auto eov = write_to_file(readme_txt_file_path,readme_txt_file_content);
		if(eov.is_error()){
			return eov;
		}
	}
	{
		auto eov = write_to_file(default_nix_file_path,default_nix_file_content);
		if(eov.is_error()){
			return eov;
		}
	}
	{
		auto eov = write_to_file(sconstruct_file_path,sconstruct_file_content);
		if(eov.is_error()){
			return eov;
		}
	}
	{
		auto eov = write_to_file(sconscript_file_path,sconscript_file_content);
		if(eov.is_error()){
			return eov;
		}
	}
	{
		auto eov = write_to_file(main_cpp_file_path,main_cpp_file_content);
		if(eov.is_error()){
			return eov;
		}
	}
	{
		auto eov = write_to_file(gitignore_file_path,gitignore_file_content);
		if(eov.is_error()){
			return eov;
		}
	}
	{
		auto eov = write_to_file(derivation_nix_file_path,derivation_nix_file_content);
		if(eov.is_error()){
			return eov;
		}
	}

	return saw::make_void();
}
}

int main(int argc, char** argv){
	auto eov = kel::kel_main(argc, argv);
	if(eov.is_error()){
		auto& err = eov.get_error();
		auto err_msg = err.get_message();
		std::cerr<<"[Error]: "<<err.get_category();

		if(not err_msg.empty()){
			std::cerr<<" - "<<err_msg;
		}
		std::cerr<<std::endl;

		return err.get_id();
	}

	return 0;
}