Last active
October 7, 2019 18:50
-
-
Save coryodaniel/8582f3502c1b78b0b0895c9bdd4786cc to your computer and use it in GitHub Desktop.
pasta - copy and paste filesystems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
require 'optparse' | |
require 'json' | |
require "base64" | |
require 'fileutils' | |
$out = "/tmp/lol" | |
options = {} | |
OptionParser.new do |opts| | |
opts.banner = "Usage: pasta.rb [options]" | |
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v| | |
options[:verbose] = v | |
end | |
opts.on("-pSTR", "--paste=STR", "String to paste") do |p| | |
options[:paste] = p | |
end | |
opts.on("-cPATH", "--copy=PATH", "Path to copy") do |c| | |
options[:copy] = c | |
end | |
end.parse! | |
if options[:copy] | |
wildcard_path = File.join(options[:copy], "**", "*") | |
files = Dir[wildcard_path].reject {|fn| File.directory?(fn) } | |
manifest = files.reduce({}) do |agg, filename| | |
agg[filename] = File.read(filename) | |
agg | |
end | |
json = JSON.generate(manifest) | |
output = Base64.strict_encode64(json) | |
puts output | |
elsif options[:paste] | |
json = Base64.decode64(options[:paste]) | |
manifest = JSON.parse(json) | |
manifest.each do |name, data| | |
filepath = File.expand_path(File.join($out, name)) | |
directory = File.dirname(filepath) | |
FileUtils.mkdir_p(directory) | |
File.open(filepath, "w+") do |f| | |
if options[:verbose] | |
puts "Creating: #{filepath}" | |
end | |
f.puts data | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment