require 'fileutils' require 'yaml' module App42 module Command module UserToken # make sure configuration root directory exist? # create new dir unless file.exist? def ensure_config_dir FileUtils.mkdir_p(config_path) unless File.exist? config_path end # Load yaml config file # make sure file contain api and secret key def check_key_file? info = YAML.load_file(key_path) if File.exist? key_path true unless info.nil? || info['api_key'].nil? || info['secret_key'].nil? end # Load yaml config file # read api and secret key def get_keys keys = YAML.load_file(key_path) if File.exist? key_path return nil, nil if keys.nil? || keys.empty? return keys['api_key'], keys['secret_key'] unless keys.empty? || keys.nil? end # expand key file path # And open in write mode and configure api and secret key def local_app42_key api_key, secret_key key_file = File.expand_path(App42::KEYS_FILE) File.open(key_file, "w") { |f| f.puts "api_key:" f.puts " #{api_key}" f.puts "secret_key:" f.puts " #{secret_key}" } end # Extract key path # and remove from configuration file def remove_key_file FileUtils.rm_f(key_path) end # Expand key path def key_path File.expand_path(App42::KEYS_FILE) end # Extract app42pass config dir path # and remove from configuration file def config_path File.expand_path(App42::CONFIG_DIR) end # collect key path from constants # mkdir key file def ensure_key_file FileUtils.mkdir_p(key_path) unless File.exist? key_path end end end end