Skip to content

Instantly share code, notes, and snippets.

@apainintheneck
Last active April 20, 2022 16:40
Show Gist options
  • Save apainintheneck/e6bb012226fda49abd4e8474bf2ee68d to your computer and use it in GitHub Desktop.
Save apainintheneck/e6bb012226fda49abd4e8474bf2ee68d to your computer and use it in GitHub Desktop.
Shell-lib: A small wrapper for standard C++ library functions that makes them behave like shell commands.
/*
Shell-lib
----------
Mimic familiar shell commands inside of C++.
--------------------------------------------
MIT License
Copyright (c) 2022 Kevin Robell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <cstdlib> // for std::getenv
#include <filesystem>
#include <fstream> // for std::ofstream
#include <string>
namespace sh {
using path = std::filesystem::path;
const bool recursive = true;
bool cd() noexcept {
std::error_code err;
std::filesystem::current_path(std::getenv("HOME"), err);
return !err;
}
bool cd(const path& dest) noexcept {
std::error_code err;
std::filesystem::current_path(dest, err);
return !err;
}
bool cp(const path& src, const path& dest, const bool recursive = false) noexcept {
std::error_code err;
if(recursive) {
std::filesystem::copy(src, dest, std::filesystem::copy_options::recursive, err);
return !err;
} else {
return std::filesystem::copy_file(src, dest, err);
}
}
std::string env(const std::string& input) noexcept {
if(const char* env_str = std::getenv(input.c_str())) {
return env_str;
}
return "";
}
bool mv(const path& src, const path& dest) noexcept {
std::error_code err;
std::filesystem::rename(src, dest, err);
return !err;
}
bool mkdir(const path& dir) noexcept {
std::error_code err;
return std::filesystem::create_directory(dir, err);
}
path pwd() noexcept {
std::error_code err;
return std::filesystem::current_path(err);
}
bool rm(const path& src, const bool recursive = false) noexcept {
std::error_code err;
if(recursive) {
std::filesystem::remove_all(src, err);
return !err;
} else {
return std::filesystem::remove(src, err);
}
}
bool rmdir(const path& dir) noexcept {
if(not (std::filesystem::is_directory(dir) and std::filesystem::is_empty(dir)))
return false;
std::error_code err;
return std::filesystem::remove(dir, err);
}
bool touch(const std::string& filepath) noexcept {
std::ofstream file(filepath, std::ios::out | std::ios::app);
return file.is_open();
}
} //namespace sh
/*
Functional tests
Functional tests for Shell-lib
*/
#include "shell-lib.hpp"
#include <iostream>
#include <cassert>
#include <filesystem>
template <typename Cmd, typename ...Output>
void prompt(Cmd cmd, Output ...output) {
std::cout << "$ " << cmd << '\n';
if(sizeof...(output)) {
(std::cout << ... << output) << '\n';
}
}
void assert_exists_directory(const std::string& path) {
std::error_code err;
assert(std::filesystem::is_directory(path, err));
}
void assert_exists_file(const std::string& path) {
std::error_code err;
assert(std::filesystem::is_regular_file(path, err));
}
void refute_exists_directory(const std::string& path) {
std::error_code err;
assert(not std::filesystem::is_directory(path, err));
}
void refute_exists_file(const std::string& path) {
std::error_code err;
assert(not std::filesystem::is_regular_file(path, err));
}
int main() {
prompt("pwd", sh::pwd());
sh::cd();
prompt("cd");
prompt("pwd", sh::pwd());
prompt("echo $PATH", sh::env("PATH"));
sh::mkdir("__test__");
prompt("mkdir __test__");
assert_exists_directory("__test__");
sh::cd("__test__");
prompt("cd __test__");
prompt("pwd", sh::pwd());
sh::touch("touched_file");
prompt("touch touched_file");
assert_exists_file("touched_file");
sh::cp("touched_file", "copied_file");
prompt("cp touched_file copied_file");
assert_exists_file("copied_file");
sh::mkdir("__dir__");
prompt("mkdir __dir__");
assert_exists_directory("__dir__");
sh::mv("touched_file", "__dir__/touched_file");
prompt("mv touched_file __dir__/touched_file");
assert_exists_file("__dir__/touched_file");
sh::mv("copied_file", "__dir__/copied_file");
prompt("mv copied_file __dir__/copied_file");
assert_exists_file("__dir__/copied_file");
sh::cp("__dir__", "__copied_dir__", sh::recursive);
prompt("cp -R __dir__ __copied_dir__");
assert_exists_directory("__copied_dir__");
assert_exists_file("__copied_dir__/touched_file");
assert_exists_file("__copied_dir__/copied_file");
sh::cd("__dir__");
prompt("cd __dir__");
prompt("pwd", sh::pwd());
sh::rm("touched_file");
prompt("rm touched_file");
refute_exists_file("touched_file");
sh::rm("copied_file");
prompt("rm copied_file");
refute_exists_file("copied_file");
sh::cd("..");
prompt("cd ..");
prompt("pwd", sh::pwd());
sh::rmdir("__dir__");
prompt("rmdir __dir__");
refute_exists_directory("__dir__");
sh::rm("__copied_dir__", sh::recursive);
prompt("rm -R __copied_dir__");
refute_exists_directory("__copied_dir__");
sh::cd("..");
prompt("cd ..");
prompt("pwd", sh::pwd());
sh::rmdir("__test__");
prompt("rmdir __test__");
refute_exists_directory("__test__");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment