require 'thor' require 'fileutils' module Immunio class CLI < Thor desc 'init', 'Initializes an Immunio configuration in the current app' method_option :key, type: :string, desc: 'The key generated for your app in Immunio' method_option :secret, type: :string, desc: 'The secret generated for your app in Immunio' def init if File.exist?(config_file) && File.read(config_file) =~ /key:\s+\w+|secret:\s+\w+/ say 'Immunio already initialized.', :green Kernel.exit 0 end key = options[:key] || ask('Enter the key generated for your app in Immunio:') secret = options[:secret] || ask('Enter the secret generated for your app in Immunio:') FileUtils.mkdir_p(File.dirname(config_file)) File.open(config_file, 'a') do |f| f.puts "key: #{key}" f.puts "secret: #{secret}" end say "Credentials written to #{config_file}", :green end private def config_file root = defined?(Rails) ? Rails.root : Dir.pwd File.join(root, 'config', 'immunio.yml') end end end