class DTK::Client::Operation::ClientModuleDir
Operations for managing module folders
Constants
- NAMESPACE_SEPERATOR
Public Class Methods
create_file_with_content(file_path, content)
click to toggle source
# File lib/client/operation/client_module_dir.rb, line 118 def self.create_file_with_content(file_path, content) FileUtils.mkdir_p(File.dirname(file_path)) File.open(file_path, 'w') { |f| f << content } end
create_module_dir(module_type, module_name, opts = {})
click to toggle source
# File lib/client/operation/client_module_dir.rb, line 55 def self.create_module_dir(module_type, module_name, opts = {}) path = opts[:path] unless path base_module_path = base_path(module_type) path_parts = if (module_name.match(/(.*)#{NAMESPACE_SEPERATOR}(.*)/)) [base_module_path, "#{$1}", "#{$2}"] else [base_module_path, "#{module_name}"] end path = path_parts.compact.join('/') end if File.exists?(path) if opts[:remove_existing] FileUtils.rm_rf(path) else raise Error::Usage, "Directory '#{path}' is not empty; it must be deleted or moved before retrying the command" unless (Dir.entries(path) - %w{ . .. }).empty? return path end end FileUtils.mkdir_p(path) path end
create_module_dir_from_path(path, opts = {})
click to toggle source
# File lib/client/operation/client_module_dir.rb, line 84 def self.create_module_dir_from_path(path, opts = {}) if File.exists?(path) if opts[:remove_existing] FileUtils.rm_rf(path) else raise Error::Usage, "Directory '#{path}' is not empty; it must be deleted or moved before retrying the command" unless (Dir.entries(path) - %w{ . .. }).empty? return path end end FileUtils.mkdir_p(path) path end
create_service_dir(service_instance, opts = {})
click to toggle source
opts can have keys
:backup_if_exist - Boolean (default: false) :remove_existing - Boolean (default: false)
# File lib/client/operation/client_module_dir.rb, line 31 def self.create_service_dir(service_instance, opts = {}) path = opts[:path] path = "#{base_path(:service)}/#{service_instance}" if path.nil? if File.exists?(path) if opts[:remove_existing] FileUtils.rm_rf(path) else # TODO: put back in after referenced methods are ported over # if local copy of module exists then move that module to backups location # if opts[:backup_if_exist] # backup_dir = backup_dir(type, module_ref) # FileUtils.mv(target_repo_dir, backup_dir) # OsUtil.print_warning("Backup of existing module directory moved to '#{backup_dir}'") # else # raise Error::Usage, "Directory '#{path}' is not empty; it must be deleted or moved before retrying the command" #end raise Error::Usage, "Directory '#{path}' is not empty; it must be deleted or moved before retrying the command" unless (Dir.entries(path) - %w{ . .. }).empty? return path end end FileUtils.mkdir_p(path) path end
delete_directory_content(path)
click to toggle source
# File lib/client/operation/client_module_dir.rb, line 114 def self.delete_directory_content(path) FileUtils.rm_rf(Dir.glob("#{path}/*")) end
local_dir_exists?(type, name, opts = {})
click to toggle source
# File lib/client/operation/client_module_dir.rb, line 98 def self.local_dir_exists?(type, name, opts = {}) File.exists?("#{base_path(type)}/#{name}") end
ret_base_path(type, name)
click to toggle source
# File lib/client/operation/client_module_dir.rb, line 102 def self.ret_base_path(type, name) "#{base_path(type)}/#{name}" end
ret_path_with_current_dir(name)
click to toggle source
# File lib/client/operation/client_module_dir.rb, line 106 def self.ret_path_with_current_dir(name) "#{OsUtil.current_dir}/#{name.gsub(':','/')}" end
rm_f(path)
click to toggle source
# File lib/client/operation/client_module_dir.rb, line 110 def self.rm_f(path) FileUtils.rm_rf(path) end
Private Class Methods
base_path(type)
click to toggle source
# File lib/client/operation/client_module_dir.rb, line 129 def self.base_path(type) path = case type.to_sym when :service then Config[:instance_location] else raise Error, "Unexpected type (#{type}) when determining base path" end # see if path is relative or not final_path = path && path.start_with?('/') ? path : "#{OsUtil.dtk_local_folder}/#{path}" # remove last slash if set in configuration by mistake final_path.gsub(/\/$/,'') end
response_data_hash(hash = {})
click to toggle source
# File lib/client/operation/client_module_dir.rb, line 125 def self.response_data_hash(hash = {}) hash.inject({}) { |h, (k, v)| h.merge(k.to_s => v) } end