Skip to content

Instantly share code, notes, and snippets.

@coryodaniel
Last active October 7, 2019 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coryodaniel/8582f3502c1b78b0b0895c9bdd4786cc to your computer and use it in GitHub Desktop.
Save coryodaniel/8582f3502c1b78b0b0895c9bdd4786cc to your computer and use it in GitHub Desktop.
pasta - copy and paste filesystems
#! /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