add CLIParser and Configuration: --relation option v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 09 May 2021 17:20:30 +0200
branchv_0
changeset 25 f71eb7aefd25
parent 24 bbebee4044d9
child 26 b4c4c7f937e9
add CLIParser and Configuration: --relation option
bash-completion.sh
nbproject/configurations.xml
nbproject/project.xml
src/CLIParser.h
src/CMakeLists.txt
src/Configuration.h
src/FstabCommand.cpp
src/FstabCommand.h
src/relpipe-in-fstab.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bash-completion.sh	Sun May 09 17:20:30 2021 +0200
@@ -0,0 +1,34 @@
+# Relational pipes
+# Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, version 3 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+_relpipe_in_fstab_completion() {
+	local w0 w1 w2 w3
+
+	COMPREPLY=()
+	w0=${COMP_WORDS[COMP_CWORD]}
+	w1=${COMP_WORDS[COMP_CWORD-1]}
+	w2=${COMP_WORDS[COMP_CWORD-2]}
+	w3=${COMP_WORDS[COMP_CWORD-3]}
+
+	if   [[ "$w1" == "--relation"                      && "x$w0" == "x" ]];    then COMPREPLY=("''")
+	else
+		OPTIONS=(
+			"--relation"
+		)
+		COMPREPLY=($(compgen -W "${OPTIONS[*]}" -- "$w0"))
+	fi
+}
+
+complete -F _relpipe_in_fstab_completion relpipe-in-fstab
--- a/nbproject/configurations.xml	Sat Oct 24 00:08:17 2020 +0200
+++ b/nbproject/configurations.xml	Sun May 09 17:20:30 2021 +0200
@@ -42,6 +42,7 @@
   <logicalFolder name="root" displayName="root" projectFiles="true" kind="ROOT">
     <df root="." name="0">
       <df name="src">
+        <in>FstabCommand.cpp</in>
         <in>relpipe-in-fstab.cpp</in>
       </df>
     </df>
@@ -91,7 +92,11 @@
           <preBuildFirst>true</preBuildFirst>
         </preBuild>
       </makefileType>
-      <item path="src/relpipe-in-fstab.cpp" ex="false" tool="1" flavor2="0">
+      <item path="src/FstabCommand.cpp" ex="false" tool="1" flavor2="0">
+        <ccTool flags="0">
+        </ccTool>
+      </item>
+      <item path="src/relpipe-in-fstab.cpp" ex="false" tool="1" flavor2="11">
         <ccTool flags="0">
         </ccTool>
       </item>
--- a/nbproject/project.xml	Sat Oct 24 00:08:17 2020 +0200
+++ b/nbproject/project.xml	Sun May 09 17:20:30 2021 +0200
@@ -6,7 +6,7 @@
             <name>relpipe-in-fstab.cpp</name>
             <c-extensions/>
             <cpp-extensions>cpp</cpp-extensions>
-            <header-extensions/>
+            <header-extensions>h</header-extensions>
             <sourceEncoding>UTF-8</sourceEncoding>
             <make-dep-projects/>
             <sourceRootList>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/CLIParser.h	Sun May 09 17:20:30 2021 +0200
@@ -0,0 +1,66 @@
+/**
+ * Relational pipes
+ * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <vector>
+#include <iostream>
+
+#include <relpipe/writer/typedefs.h>
+#include <relpipe/cli/CLI.h>
+#include <relpipe/cli/RelpipeCLIException.h>
+
+#include "Configuration.h"
+
+namespace relpipe {
+namespace in {
+namespace fstab {
+
+class CLIParser {
+private:
+
+	relpipe::writer::string_t readNext(const std::vector<relpipe::writer::string_t>& arguments, int& i) {
+		if (i < arguments.size()) return arguments[i++];
+		else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+	}
+
+public:
+
+	static const relpipe::writer::string_t OPTION_RELATION;
+
+	Configuration parse(const std::vector<relpipe::writer::string_t>& arguments) {
+		Configuration c;
+
+		for (int i = 0; i < arguments.size();) {
+			relpipe::writer::string_t option = readNext(arguments, i);
+
+			if (option == OPTION_RELATION) {
+				c.relation = readNext(arguments, i);
+			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+		}
+
+		return c;
+	}
+
+	virtual ~CLIParser() {
+	}
+};
+
+const relpipe::writer::string_t CLIParser::OPTION_RELATION = L"--relation";
+
+}
+}
+}
--- a/src/CMakeLists.txt	Sat Oct 24 00:08:17 2020 +0200
+++ b/src/CMakeLists.txt	Sun May 09 17:20:30 2021 +0200
@@ -29,6 +29,7 @@
 # Executable output:
 add_executable(
 	${EXECUTABLE_FILE}
+	FstabCommand.cpp
 	relpipe-in-fstab.cpp
 )
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Configuration.h	Sun May 09 17:20:30 2021 +0200
@@ -0,0 +1,39 @@
+/**
+ * Relational pipes
+ * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <vector>
+#include <iostream>
+
+#include <relpipe/writer/typedefs.h>
+
+
+namespace relpipe {
+namespace in {
+namespace fstab {
+
+class Configuration {
+public:
+	relpipe::writer::string_t relation = L"fstab";
+
+	virtual ~Configuration() {
+	}
+};
+
+}
+}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/FstabCommand.cpp	Sun May 09 17:20:30 2021 +0200
@@ -0,0 +1,100 @@
+/**
+ * Relational pipes
+ * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <cstdlib>
+#include <vector>
+#include <memory>
+#include <locale>
+#include <regex>
+#include <algorithm>
+#include <unistd.h>
+
+#include <relpipe/writer/RelationalWriter.h>
+#include <relpipe/writer/RelpipeWriterException.h>
+#include <relpipe/writer/AttributeMetadata.h>
+#include <relpipe/writer/Factory.h>
+#include <relpipe/writer/TypeId.h>
+
+#include <relpipe/cli/CLI.h>
+
+#include "FstabCommand.h"
+
+using namespace std;
+using namespace relpipe::cli;
+using namespace relpipe::writer;
+
+namespace relpipe {
+namespace in {
+namespace fstab {
+
+using namespace relpipe::cli;
+using namespace relpipe::writer;
+
+/**
+ * see https://hg.frantovo.cz/sql-api/file/tip/prototyp/prototyp.sql#l49
+ */
+void FstabCommand::process(std::istream& input, std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) {
+	wregex devicePattern = wregex(L"(LABEL|UUID)=(.*)");
+	wregex linePattern = wregex(L"^([^\\s#]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+(\\d+)\\s+(\\d+)\\s*$");
+	wstring_convert < codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
+
+	writer->startRelation(configuration.relation,{
+		{L"scheme", TypeId::STRING},
+		{L"device", TypeId::STRING},
+		{L"mount_point", TypeId::STRING},
+		{L"type", TypeId::STRING},
+		// {L"types", TypeId::STRING}, // TODO: array
+		{L"options", TypeId::STRING}, // TODO: array
+		{L"dump", TypeId::INTEGER},
+		{L"pass", TypeId::INTEGER}
+	}, true);
+
+	string lineBytes;
+	while (getline(input, lineBytes)) {
+
+		wstring line = convertor.from_bytes(lineBytes);
+		wsmatch lineMatch;
+		if (regex_search(line, lineMatch, linePattern) && lineMatch.size() > 0) {
+			int g = 1;
+			wstring device = lineMatch[g++];
+			wstring mountPoint = lineMatch[g++];
+			wstring type = lineMatch[g++];
+			wstring options = lineMatch[g++];
+			wstring dump = lineMatch[g++];
+			wstring pass = lineMatch[g++];
+
+			wsmatch deviceMatch;
+			if (regex_search(device, deviceMatch, devicePattern)) {
+				writer->writeAttribute(deviceMatch[1]);
+				writer->writeAttribute(deviceMatch[2]);
+			} else {
+				writer->writeAttribute(L""); // TODO: null (requires bitmap)
+				writer->writeAttribute(device);
+			}
+
+			if (mountPoint == L"none") mountPoint = L""; // TODO: null (requires bitmap)
+			writer->writeAttribute(mountPoint);
+			writer->writeAttribute(type);
+			writer->writeAttribute(options);
+			writer->writeAttribute(dump);
+			writer->writeAttribute(pass);
+		}
+	}
+}
+
+}
+}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/FstabCommand.h	Sun May 09 17:20:30 2021 +0200
@@ -0,0 +1,41 @@
+/**
+ * Relational pipes
+ * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <iostream>
+#include <sstream>
+#include <vector>
+#include <memory>
+
+#include <relpipe/writer/TypeId.h>
+
+#include "Configuration.h"
+
+namespace relpipe {
+namespace in {
+namespace fstab {
+
+class FstabCommand {
+private:
+public:
+	void process(std::istream& input, std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration);
+
+};
+
+}
+}
+}
--- a/src/relpipe-in-fstab.cpp	Sat Oct 24 00:08:17 2020 +0200
+++ b/src/relpipe-in-fstab.cpp	Sun May 09 17:20:30 2021 +0200
@@ -28,72 +28,27 @@
 
 #include <relpipe/cli/CLI.h>
 
+#include "CLIParser.h"
+#include "Configuration.h"
+#include "FstabCommand.h"
+
 using namespace relpipe::cli;
 using namespace relpipe::writer;
-
-/**
- * see https://hg.frantovo.cz/sql-api/file/tip/prototyp/prototyp.sql#l49
- */
-void processDataStream(ostream &output, istream* input) {
-	wregex devicePattern = wregex(L"(LABEL|UUID)=(.*)");
-	wregex linePattern = wregex(L"^([^\\s#]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+(\\d+)\\s+(\\d+)\\s*$");
-	wstring_convert < codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
-
-	std::shared_ptr<RelationalWriter> writer(Factory::create(output));
-
-	writer->startRelation(L"fstab",{
-		{L"scheme", TypeId::STRING},
-		{L"device", TypeId::STRING},
-		{L"mount_point", TypeId::STRING},
-		{L"type", TypeId::STRING},
-		// {L"types", TypeId::STRING}, // TODO: array
-		{L"options", TypeId::STRING}, // TODO: array
-		{L"dump", TypeId::INTEGER},
-		{L"pass", TypeId::INTEGER}
-	}, true);
-
-	string lineBytes;
-	while (getline(*input, lineBytes)) {
-
-		wstring line = convertor.from_bytes(lineBytes);
-		wsmatch lineMatch;
-		if (regex_search(line, lineMatch, linePattern) && lineMatch.size() > 0) {
-			int g = 1;
-			wstring device = lineMatch[g++];
-			wstring mountPoint = lineMatch[g++];
-			wstring type = lineMatch[g++];
-			wstring options = lineMatch[g++];
-			wstring dump = lineMatch[g++];
-			wstring pass = lineMatch[g++];
-
-			wsmatch deviceMatch;
-			if (regex_search(device, deviceMatch, devicePattern)) {
-				writer->writeAttribute(deviceMatch[1]);
-				writer->writeAttribute(deviceMatch[2]);
-			} else {
-				writer->writeAttribute(L""); // TODO: null (requires bitmap)
-				writer->writeAttribute(device);
-			}
-
-			if (mountPoint == L"none") mountPoint = L""; // TODO: null (requires bitmap)
-			writer->writeAttribute(mountPoint);
-			writer->writeAttribute(type);
-			writer->writeAttribute(options);
-			writer->writeAttribute(dump);
-			writer->writeAttribute(pass);
-		}
-	}
-
-}
+using namespace relpipe::in::fstab;
 
 int main(int argc, char** argv) {
 	setlocale(LC_ALL, "");
 	CLI::untieStdIO();
-	//CLI cli(argc, argv);
+	CLI cli(argc, argv);
 
 	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
 
 	try {
+		CLIParser cliParser;
+		Configuration configuration = cliParser.parse(cli.arguments());
+		FstabCommand command;
+		std::shared_ptr<RelationalWriter> writer(Factory::create(std::cout));
+
 		if (isatty(fileno(stdin))) {
 			/**
 			 * Our program is executed on TTY without input stream redirection → read from default file location.
@@ -101,7 +56,7 @@
 			 * (we don't expect that user is writing the content of fstab by hand on the terminal)
 			 */
 			ifstream s("/etc/fstab");
-			processDataStream(cout, &s);
+			command.process(s, writer, configuration);
 		} else {
 			/**
 			 * Input stream comes from a file or is piped from other command → read it instead of default file.
@@ -109,11 +64,16 @@
 			 * or $ relpipe-in-fstab < /etc/fstab | … # without UUoC
 			 * or $ ssh example.com cat /etc/fstab | relpipe-in-fstab | …
 			 * or $ cat | relpipe-in-fstab | … # user writes the fstab content by hand
+			 * or $ cat /etc/mtab | relpipe-in-fstab | relpipe-out-tabular | less -RSi # currently mounted filesystems
 			 */
-			processDataStream(cout, &cin);
+			command.process(std::cin, writer, configuration);
 		}
+
 		resultCode = CLI::EXIT_CODE_SUCCESS;
-
+	} catch (RelpipeCLIException e) {
+		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str());
+		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
+		resultCode = e.getExitCode();
 	} catch (RelpipeWriterException e) {
 		fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessge().c_str());
 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());