diff options
| author | Paul Buetow <paul@mimecast.com> | 2018-05-27 20:04:04 +0100 |
|---|---|---|
| committer | Paul Buetow <paul@mimecast.com> | 2018-05-27 20:04:04 +0100 |
| commit | dd28438cf8b9cc0541641727b8fe79d7fe5e0c56 (patch) | |
| tree | f46d6b0d781ae46ff5ce9bf1ae9fd1a6e2f2e28e | |
| parent | 0396e325dcb721cf2ce7daa54aa3c1eb350e4b63 (diff) | |
fortune not found
Quick commit
4 files changed, 138 insertions, 47 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d563759 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +export diff --git a/data/f1c98ccb91d3bc7f1aac306c67f866e31b2296e25f5f04a81bde924385e08b16/c549addc80367e17fd46b5b6a094ee7f9958d5c92fba35f519e64c5a4304dde6/999da4de4d657b8e65d59368748cf17f16cf07f23ee9c164eb43de2c362f606b/a42517945927c258f319d7eb4e70a438178fd18c66ad0951424553c0bb30f773/b23b6d25324f518146005aea01fd7a7c64ec137532780572e3852f9d8e8556f4/a1d9890884c1b4b960c279cfe7554a900d169422d6cec980beef67761487d3b9/dffd8a36f401609f6d01e410c2ff32560f594d0f163953406309973739328661/1561de40abee6d51ecb4f474bfc4da0a108c4bd6492454c72314cf0ffd787da2.data b/data/f1c98ccb91d3bc7f1aac306c67f866e31b2296e25f5f04a81bde924385e08b16/c549addc80367e17fd46b5b6a094ee7f9958d5c92fba35f519e64c5a4304dde6/999da4de4d657b8e65d59368748cf17f16cf07f23ee9c164eb43de2c362f606b/a42517945927c258f319d7eb4e70a438178fd18c66ad0951424553c0bb30f773/b23b6d25324f518146005aea01fd7a7c64ec137532780572e3852f9d8e8556f4/a1d9890884c1b4b960c279cfe7554a900d169422d6cec980beef67761487d3b9/dffd8a36f401609f6d01e410c2ff32560f594d0f163953406309973739328661/1561de40abee6d51ecb4f474bfc4da0a108c4bd6492454c72314cf0ffd787da2.data deleted file mode 100644 index e53b880..0000000 --- a/data/f1c98ccb91d3bc7f1aac306c67f866e31b2296e25f5f04a81bde924385e08b16/c549addc80367e17fd46b5b6a094ee7f9958d5c92fba35f519e64c5a4304dde6/999da4de4d657b8e65d59368748cf17f16cf07f23ee9c164eb43de2c362f606b/a42517945927c258f319d7eb4e70a438178fd18c66ad0951424553c0bb30f773/b23b6d25324f518146005aea01fd7a7c64ec137532780572e3852f9d8e8556f4/a1d9890884c1b4b960c279cfe7554a900d169422d6cec980beef67761487d3b9/dffd8a36f401609f6d01e410c2ff32560f594d0f163953406309973739328661/1561de40abee6d51ecb4f474bfc4da0a108c4bd6492454c72314cf0ffd787da2.data +++ /dev/null @@ -1 +0,0 @@ -nz*uy`Έ_
\ No newline at end of file diff --git a/data/f1c98ccb91d3bc7f1aac306c67f866e31b2296e25f5f04a81bde924385e08b16/c549addc80367e17fd46b5b6a094ee7f9958d5c92fba35f519e64c5a4304dde6/999da4de4d657b8e65d59368748cf17f16cf07f23ee9c164eb43de2c362f606b/a42517945927c258f319d7eb4e70a438178fd18c66ad0951424553c0bb30f773/b23b6d25324f518146005aea01fd7a7c64ec137532780572e3852f9d8e8556f4/a1d9890884c1b4b960c279cfe7554a900d169422d6cec980beef67761487d3b9/dffd8a36f401609f6d01e410c2ff32560f594d0f163953406309973739328661/1561de40abee6d51ecb4f474bfc4da0a108c4bd6492454c72314cf0ffd787da2.index b/data/f1c98ccb91d3bc7f1aac306c67f866e31b2296e25f5f04a81bde924385e08b16/c549addc80367e17fd46b5b6a094ee7f9958d5c92fba35f519e64c5a4304dde6/999da4de4d657b8e65d59368748cf17f16cf07f23ee9c164eb43de2c362f606b/a42517945927c258f319d7eb4e70a438178fd18c66ad0951424553c0bb30f773/b23b6d25324f518146005aea01fd7a7c64ec137532780572e3852f9d8e8556f4/a1d9890884c1b4b960c279cfe7554a900d169422d6cec980beef67761487d3b9/dffd8a36f401609f6d01e410c2ff32560f594d0f163953406309973739328661/1561de40abee6d51ecb4f474bfc4da0a108c4bd6492454c72314cf0ffd787da2.index Binary files differdeleted file mode 100644 index fa602a6..0000000 --- a/data/f1c98ccb91d3bc7f1aac306c67f866e31b2296e25f5f04a81bde924385e08b16/c549addc80367e17fd46b5b6a094ee7f9958d5c92fba35f519e64c5a4304dde6/999da4de4d657b8e65d59368748cf17f16cf07f23ee9c164eb43de2c362f606b/a42517945927c258f319d7eb4e70a438178fd18c66ad0951424553c0bb30f773/b23b6d25324f518146005aea01fd7a7c64ec137532780572e3852f9d8e8556f4/a1d9890884c1b4b960c279cfe7554a900d169422d6cec980beef67761487d3b9/dffd8a36f401609f6d01e410c2ff32560f594d0f163953406309973739328661/1561de40abee6d51ecb4f474bfc4da0a108c4bd6492454c72314cf0ffd787da2.index +++ /dev/null @@ -7,6 +7,40 @@ require "openssl" require 'digest/sha2' require 'base64' +$data_dir = "./../.geheimdata" +$export_dir = "./export" + +module Git + def initialize + super() + @wd = Dir.pwd + end + + def git_add(file:) + dirname, basename = File.dirname(file), File.basename(file) + Dir.chdir(dirname) + puts %x{git add "#{basename}"} + puts %x{git commit -m "Add #{file}"} + Dir.chdir(@wd) + end + + def git_rm(file:) + dirname, basename = File.dirname(file), File.basename(file) + Dir.chdir(dirname) + puts %x{git rm "#{basename}"} + puts %x{git commit -m "Remove #{file}"} + Dir.chdir(@wd) + end + + def git_sync + puts "Synchronising #{$data_dir}" + Dir.chdir($data_dir) + puts %x{git pull origin master} + puts %x{git push origin master} + Dir.chdir(@wd) + end +end + module Encryption def initialize super() @@ -49,14 +83,8 @@ module Encryption end end -class Config - def initialize - # Nice example/reference: https://gist.github.com/byu/99651 - @@data_dir = "./data" - end -end - -class CommitFile < Config +class CommitFile + include Git def initialize super() end @@ -77,17 +105,19 @@ class CommitFile < Config File.open(file, "w") do |fd| fd.write(content) end + git_add(file: file) end end class GeheimData < CommitFile include Encryption + include Git attr_accessor :data def initialize(data_file:, data: nil) super() - @data_path = "#{@@data_dir}/#{data_file}" + @data_path = "#{$data_dir}/#{data_file}" if data.nil? @data = decrypt(encrypted: File.read(@data_path)) else @@ -101,7 +131,21 @@ class GeheimData < CommitFile def rm puts "Deleting #{@data_path}" - File.unlink(@data_path) + git_rm(file: @data_path) + end + + def export(destination_file:) + unless File.directory?($export_dir) + puts "Creating #{$export_dir}" + FileUtils.mkdir_p($export_dir) + end + + destination_path = "#{$export_dir}/#{destination_file}" + puts "Exporting to #{destination_path}" + + File.open(destination_path, "w") do |fd| + fd.write(@data) + end end def commit @@ -116,7 +160,7 @@ class Index < CommitFile def initialize(index_file:, description: nil) super() @data_file = index_file.sub(".index", ".data") - @index_path = "#{@@data_dir}/#{index_file}" + @index_path = "#{$data_dir}/#{index_file}" @hash = File.basename(index_file).sub(".index", "") if description.nil? @@ -149,7 +193,7 @@ class Index < CommitFile def rm puts "Deleting #{@index_path}" - File.unlink(@index_path) + git_rm(file: @index_path) end def commit @@ -157,32 +201,45 @@ class Index < CommitFile end end -class Geheim < Config +class Geheim def initialize super() - unless File.directory?(@@data_dir) - puts "Creating #{@@data_dir}" - FileUtils.mkdir_p(@@data_dir) + unless File.directory?($data_dir) + puts "Creating #{$data_dir}" + FileUtils.mkdir_p($data_dir) end end - def ls(search_term: nil, show: false) + def ls(search_term: nil, show: false, export: false) indexes = Array.new walk_indexes(search_term: search_term) do |index| indexes << index end indexes.sort.each do |index| print index - print index.get_data if show and !index.is_binary? + if show and !index.is_binary? + print index.get_data + elsif export + index.get_data.export(destination_file: File.basename(index.description)) + end end end - def add(description:) + def add(description:, file: nil) hash = hash_path(description) - print "Data: " - data = $stdin.gets.chomp + if file.nil? + print "Data: " + data = $stdin.gets.chomp + elsif !File.exists?(file) + puts "ERROR: #{file} does not exist!" + exit(3) + else + description += "/#{File.basename(file)}" + puts "Importing #{file}" + data = File.read(file) + end index = Index.new(index_file: "#{hash}.index", description: description) data = index.get_data(data: data) @@ -214,8 +271,8 @@ class Geheim < Config end private def walk_indexes(search_term:) - Dir.glob("#{@@data_dir}/**/*.index").each do |index_file| - index = Index.new(index_file: index_file.sub(@@data_dir, "")) + Dir.glob("#{$data_dir}/**/*.index").each do |index_file| + index = Index.new(index_file: index_file.sub($data_dir, "")) if search_term.nil? or index.description.include?(search_term) yield index end @@ -229,35 +286,69 @@ class Geheim < Config end path.join("/") end +end + +class CLI + include Git + + def initialize(interactive: false) + super() + @interactive = interactive + end def help puts <<-END - geheim ls - geheim SEARCHTERM - geheim show SEARCHTERM - geheim add DESCRIPTION - geheim rm SEARCHTERM - geheim help + ls + SEARCHTERM + show SEARCHTERM + add DESCRIPTION + import DESCRIPTION FILE + rm SEARCHTERM + sync + help + shell END end + + def shell_loop(argv) + loop do + geheim = Geheim.new + action = argv[0] + case action + when 'ls' + geheim.ls + when 'show' + geheim.ls(search_term: argv[1], show: true) + when 'export' + geheim.ls(search_term: argv[1], export: true) + when 'add' + geheim.add(description: argv[1]) + when 'import' + geheim.add(description: argv[1], file: argv[2]) + when 'rm' + geheim.rm(search_term: argv[1]) + when 'help' + help + when 'shell' + @interactive = true + puts "Switching to interactive mode" + when 'exit' + @interactive = false + puts "Good bye" + when 'sync' + git_sync + else + geheim.ls(search_term: action) + end + + break unless @interactive + print "% " + argv = $stdin.gets.chomp.split(" ") + end + end end begin - action = ARGV[0] - geheim = Geheim.new - - case action - when 'ls' - geheim.ls - when 'show' - geheim.ls(search_term: ARGV[1], show: true) - when 'add' - geheim.add(description: ARGV[1]) - when 'rm' - geheim.rm(search_term: ARGV[1]) - when 'help' - geheim.help - else - geheim.ls(search_term: action) - end + cli = CLI.new + cli.shell_loop(ARGV) end |
