diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fc3e982 --- /dev/null +++ b/Makefile @@ -0,0 +1,29 @@ + +mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) +#CURRENT_DIR := $(notdir $(patsubst %/,%,$(dir $(mkfile_path)))) +mkfile_dir := $(dir $(mkfile_path)) + +INSTALL_DIR=/usr/local +INSTALL_BIN_DIR=$(INSTALL_DIR)/bin +BUILD_BIN_DIR=$(mkfile_dir)bin/cpp/ +BIN_OUTPUT=saferm + +srm: + haxe $(mkfile_dir)build.hxml -D HAXE_OUTPUT_FILE=$(BIN_OUTPUT) + + +install: #uninstall + mkdir -p "$(DESTDIR)$(INSTALL_BIN_DIR)" + cp "$(BUILD_BIN_DIR)$(BIN_OUTPUT)" "$(DESTDIR)$(INSTALL_BIN_DIR)/$(BIN_OUTPUT)" +# mkdir -p "$(DESTDIR)$(INSTALL_STD_DIR)" +# cp -r std/* "$(DESTDIR)$(INSTALL_STD_DIR)" + +uninstall: + #rm -rf $(DESTDIR)$(INSTALL_BIN_DIR)/$(BIN_OUTPUT) + + +all: srm + + + +.PHONY: all install uninstall \ No newline at end of file diff --git a/build.hxml b/build.hxml new file mode 100644 index 0000000..cf14432 --- /dev/null +++ b/build.hxml @@ -0,0 +1,5 @@ +-cpp bin/cpp +-cp src +-D HAXE_OUTPUT_FILE=saferm +-D version=1.0.0 +-main Main \ No newline at end of file diff --git a/src/Main.hx b/src/Main.hx new file mode 100644 index 0000000..139a51d --- /dev/null +++ b/src/Main.hx @@ -0,0 +1,138 @@ +package; + +/*********************************** + * Author: Andreas Schaafsma + * Email: and.schaafsma@gmail.com + * Purpose: For deleting files and you don't accidentally want to press enter too early + ************************************/ + +import sys.FileSystem; +import Sys; + +class Main{ + + + static var switches:Map = [ + "-v"=>"Prints the version number", + "-t"=>"Turbo mode: Skips recursively listing all content in directories" + ]; + static var ansiColors:Map = []; + static function setupColors():Void + { + ansiColors['black'] = '\033[0;30m'; + ansiColors['red'] = '\033[31m'; + ansiColors['green'] = '\033[32m'; + ansiColors['yellow'] = '\033[33m'; + ansiColors['blue'] = '\033[1;34m'; + ansiColors['magenta'] = '\033[1;35m'; + ansiColors['cyan'] = '\033[0;36m'; + ansiColors['grey'] = '\033[0;37m'; + ansiColors['white'] = '\033[1;37m'; + } + + static var SWITCHMODE_TURBO:Bool = false; + + static function main(){ + // Set up ansiColors + setupColors(); + // Store args + var args = Sys.args(); + // Check if we need to throw usage strings back at the user + if(args.length == 0){ + usage(); + return; + } + // Create arrays to hold switches and files + var _switches:Array = []; + var files:Array = []; + // Loop through args to sort them into switches and files + for(arg in args){ + if(arg.charAt(0) == "-" && switches.exists(arg)){ + _switches.push(arg); + //Sys.println("switch found"); + } + else{ + files.push(arg); + } + } + + // Look at switch contents + if(_switches.contains("-v")){ + Sys.println('version: ${haxe.macro.Compiler.getDefine("version");}'); + return; + } + if(_switches.contains("-t")){ + SWITCHMODE_TURBO = true; + } + + // Good to go! + run(files, _switches); + // Reset color just in case + Sys.print(ansiColors['grey']); + + Sys.exit(0); + + } + + static function run(files:Array, _switches:Array):Void + { + // Warn user about the operation they're about to perform + Sys.println(ansiColors['red'] + 'Are you ABSOLUTELY sure you want to delete the following files/ directories:' + ansiColors['grey']); + var _files:Array = []; + for(file in files){ + if(FileSystem.exists(file)){ + var containingDir = FileSystem.absolutePath('$file/..'); + var absPath = FileSystem.absolutePath('$file'); + var filename = absPath.substr(containingDir.length); + Sys.println(ansiColors['yellow'] + ' - $containingDir$filename' + ansiColors['grey']); + _files.push(file); + } + else{ + Sys.println(ansiColors['red'] + '$file does not exist' + ansiColors['grey']); + } + } + Sys.println(ansiColors['white'] + 'To confirm deletion type: "YES"' + ansiColors['grey']); + + + // Read user input + var stdin = Sys.stdin(); + var strIn = stdin.readLine(); + stdin.close(); + + // Check user input + if(strIn == "YES"){ + // Delete those fucking files + deleteFiles(files); + } + else{ + // Abort + Sys.println('Aborting operation.'); + return; + } + } + + + static function usage():Void + { + Sys.println('Usage: ${haxe.macro.Compiler.getDefine("HAXE_OUTPUT_FILE");} [OPTION] [FILE]...'); + Sys.println("Safely runs rm -rf with a confirm prompt so you don´t make an oopsie."); + Sys.println(""); + Sys.println("Available OPTIONS:"); + for(key in switches.keys()){ + Sys.println(' $key: ${switches[key]}'); + } + //Sys.println(""); + //Sys.println("Safely run rm -rf with a confirm prompt so you don´t make an oopsie"); + } + static function deleteFiles(files:Array):Void + { + for(file in files){ + Sys.print('Removing ${FileSystem.absolutePath('$file')} ...'); + Sys.command('rm -rf $file'); + Sys.println('Done!'); + } + Sys.println('Removed ${files.length} files'); + + } + +} \ No newline at end of file