#!/usr/bin/env ruby require "kichi" require "aws-sdk-core" require "yaml" require "fileutils" require 'shellwords' module Kichi subcommand = ARGV.shift class Commands def initialize @settings_dir = File.join(Dir.home, ".kichi") end def use what = ARGV.shift case what when "s3" sts = Aws::STS::Client.new s3 = Aws::S3::Client.new account_id = sts.get_caller_identity.account begin bucket_name = "kichi-store-#{account_id}" s3.head_bucket(bucket: bucket_name) rescue Aws::S3::Errors::NotFound => e puts "Bucket not found, creating..." s3.create_bucket(bucket: bucket_name) rescue => e puts "You may not have permission to view the kichi bucket" end FileUtils.mkdir_p @settings_dir File.write(File.join(@settings_dir, "settings"), {s3_bucket: bucket_name}.to_yaml) else puts "Unknown store '#{what}'" end end def set var_name = ARGV.shift var_content = ARGV.join(" ") kichi.set_var(var_name, var_content) end def get var_name = ARGV.shift var_content = kichi.get_var(var_name) puts "#{var_name}=#{Shellwords.escape(var_content)}" end def create var_name = ARGV.shift kichi.create_env(var_name) end def add var_name = ARGV.shift env_name = ARGV.shift alias_name = ARGV.shift kichi.add_to_env(env_name, var_name, alias_name) end def addfile var_name = ARGV.shift env_name = ARGV.shift alias_name = ARGV.shift kichi.add_to_env(env_name, var_name, alias_name, true) end def remove var_name = ARGV.shift env_name = ARGV.shift kichi.remove_from_env(env_name, var_name) end def list env_name = ARGV.shift kichi.get_env(env_name).each do |k,v| if v[:type] == :file if v[:alias] puts "#{v[:alias]} (#{k}_PATH)" else puts "#{k}_PATH" end else if v[:alias] puts "#{v[:alias]} (#{k})" else puts "#{k}" end end end end def in env_name = ARGV.shift action = ARGV.shift if action != "run" throw "Expected run command after env_name" end file_dir = File.join(@settings_dir, "files") FileUtils.mkdir_p file_dir envs = kichi.get_env(env_name).map do |k,v| key = k if v[:type] == :envvar value = kichi.get_var(k) if v[:alias] key = v[:alias] end [key, value] elsif v[:type] == :file contents = kichi.download(k) location = File.join(file_dir, k) File.write(location, contents) if v[:alias] key = v[:alias] end ["#{key}_PATH", location] end end.to_h exec(envs, ARGV.join(" ")) end def cp filename = ARGV.shift var_name = ARGV.shift kichi.upload(var_name, filename) end def dl var_name = ARGV.shift content = kichi.download(var_name) File.write(var_name, content) puts "File saved to #{var_name}" end def method_missing(name,*args,&block) puts "Unknown command '#{name}'" end private def kichi if !File.exists?(File.join(@settings_dir, "settings")) puts "Please setup kichi by calling kichi use s3" exit! end settings = YAML.load_file(File.join(@settings_dir, "settings")) Kichi.new settings end end Commands.new.send(:"#{subcommand}") end