Skip to content

Commit

Permalink
kwargs instead of positional args for some methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleypliu committed Aug 11, 2020
1 parent c2a2464 commit 5347cce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
33 changes: 23 additions & 10 deletions lib/modules/sync_seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def delete_files(files, location)
end
end

def files_for_deletion(list_of_files_1, list_of_files_2, location)
files = list_of_files_1 - list_of_files_2
def files_for_deletion(local_list, remote_list, location)
files = local_list - remote_list

if files.empty?
puts "No files to delete"
Expand All @@ -49,7 +49,7 @@ def create_paths(relative_path)
{ local_path: File.join(@local_base, relative_path), remote_path: File.join(@remote_base, relative_path) }
end

def check_if_newer(parent_folder, local_item, remote_item, remote_path, local_path, base = @local_base)
def check_if_newer(parent_folder:, local_item:, remote_item:, remote_path:, local_path:, base: @local_base)
if parent_folder.include?(local_item)
yield if block_given?

Expand All @@ -73,7 +73,7 @@ def check_if_newer(parent_folder, local_item, remote_item, remote_path, local_pa
downloaded
end

def compare_folders(wildcard, local, remote, base = @local_base)
def compare_folders(wildcard:, local:, remote:, base: @local_base)
puts "Checking to see what files need to be deleted from #{local}"

remote_list = @session.sftp.dir.glob(remote, wildcard).map do |f|
Expand All @@ -88,7 +88,7 @@ def compare_folders(wildcard, local, remote, base = @local_base)
def check_inside_folder(folder, local_list)
paths = create_paths(folder)

compare_folders('**/*', paths[:local_path], paths[:remote_path], paths[:local_path])
compare_folders(wildcard: '**/*', local: paths[:local_path], remote: paths[:remote_path], base: paths[:local_path])

local_folder_content = Dir.glob('**/*', base: paths[:local_path])
remote_folder_content = @session.sftp.dir.glob(paths[:remote_path], '**/*')
Expand All @@ -100,24 +100,37 @@ def check_inside_folder(folder, local_list)
# Go through the various files and folders and check to see if they exist locally
local_file = file.name.force_encoding('UTF-8')

check = check_if_newer(local_folder_content, local_file, file, paths[:remote_path], paths[:local_path], paths[:local_path])
check = check_if_newer(
parent_folder: local_folder_content,
local_item: local_file,
remote_item: file,
remote_path: paths[:remote_path],
local_path: paths[:local_path],
base: paths[:local_path]
)

# Break out of loop if already downloaded
break if check == true
end
end

def main_task(local_list, remote_list, local_base, remote_base)
def main_task(local_list:, remote_list:, local_base:, remote_base:)
@local_base = local_base
@remote_base = remote_base

remote_list.each do |object|
# There are files with non-ASCII characters (i.e. accented) in the CMS files
name = object.name.force_encoding('UTF-8')
paths = create_paths(name)


check_if_newer(local_list, name, object, paths[:remote_path], paths[:local_path]) do

check_if_newer(
parent_folder: local_list,
local_item: name,
remote_item: object,
remote_path: paths[:remote_path],
local_path: paths[:local_path]
)
do
check_inside_folder(name, local_list) if object.attributes.directory?
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/tasks/staging_seeds.rake
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,24 @@ namespace :comfy do
new_session.start_session do |session|
# First get rid of any local top-level (i.e. which exist in the main
# directory of REMOTE) folders/files that don't exist remotely
new_session.compare_folders('*', to, FROM)
new_session.compare_folders(wildcard: '*', local: to, remote: FROM)

local_list = new_session.list_local_files(to)
remote_list = new_session.list_remote_files(FROM)

if answer == 'all'
new_session.main_task(local_list, remote_list, to, FROM)
puts "Downloading all folders..."
else
local_list.filter! { |f| f == answer }
remote_list.filter! do |f|
f.name.force_encoding('UTF-8') == answer
end

puts "Downloading a new set of #{answer}..."

new_session.main_task(local_list, remote_list, to, FROM)
end


new_session.main_task(local_list: local_list, remote_list: remote_list, local_base: to, remote_base: FROM)

puts "Finished downloads, now replacing your local seed data with your selection..."

# new_session.commence_comfy_import(answer)
Expand Down

0 comments on commit 5347cce

Please sign in to comment.