Skip to content

Commit

Permalink
test profile CLI commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kspurgin committed Oct 21, 2021
1 parent 04947c3 commit a2c378c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
13 changes: 7 additions & 6 deletions lib/cspace_config_untangler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,19 @@ def app_dir
setting :templatedir, default: default_templatedir, reader: true
setting :mapperdir, default: default_mapperdir, reader: true

config_file_names = Dir.new(default_configdir).children
.reject{ |e| e['readable'] }
.reject{ |e| e == '.keep' }
.map{ |fn| File.basename(fn).sub('.json', '') }

setting :profiles, default: config_file_names, reader: true
setting :main_profile_name, default: default_main_profile_name, reader: true
setting :log, default: logger, reader: true
setting :mapper_uri_base,
default: default_mapper_uri_base,
reader: true

def profiles
Dir.new(CCU.configdir).children
.reject{ |e| e['readable'] }
.reject{ |e| e == '.keep' }
.map{ |fn| File.basename(fn).sub('.json', '') }
end

def main_profile
Pathname.new(CCU.configdir)
.children(false)
Expand Down
1 change: 1 addition & 0 deletions lib/cspace_config_untangler/cli/helpers/profile_getter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Cli
module Helpers
class ProfileGetter
def self.call(opt_profiles)
return [CCU.main_profile] unless opt_profiles
return [CCU.main_profile] if opt_profiles.empty?

return self.all_profiles if opt_profiles == 'all'
Expand Down
35 changes: 17 additions & 18 deletions lib/cspace_config_untangler/cli/profiles_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ module CspaceConfigUntangler
module Cli
class ProfilesCli < Thor
include CCU::Cli::Helpers
desc 'all', 'Print the names of all known profiles'
desc 'all', 'Print the names of all known profiles to screen'
def all
puts [CCU.main_profile, CCU.profiles].flatten.uniq.sort
say([CCU.main_profile, CCU.profiles].flatten.uniq.sort.join("\n"))
end

desc 'check', 'Print the names of profiles that will be processed'
desc 'check', 'Prints to screen the names of profiles that will be processed'
def check
profiles = get_profiles
puts profiles
say(profiles.join("\n"))
end

desc 'compare', 'Outputs a comparison of two profiles in CSV format'
Expand All @@ -33,20 +33,17 @@ def check
LONGDESC
option :output, desc: 'Path to directory in which to output file. Name of the file is hardcoded, using the names of the profiles.', default: CCU::datadir, aliases: '-o'
def compare
if options[:profiles] == 'all'
profiles = get_profiles
else
profiles = options[:profiles].split(',').map{ |p| p.strip }
end
profiles = get_profiles

if profiles.length > 2
puts "Can only compare two profiles at a time"
exit
say('Can only compare two profiles at a time')
elsif profiles.length == 1
puts "Needs two profiles to compare"
exit
say('Needs two profiles to compare')
else
CCU::ProfileComparison.new(profiles, options[:output]).write_csv
comparer = CCU::ProfileComparison.new(profiles, options[:output])
comparer.write_csv
message = "#{comparer.summary}\n\nWrote detailed report to: #{comparer.output}"
say(message)
end
end

Expand All @@ -70,18 +67,20 @@ def by_extension

desc 'main', 'Print the name of the main profile'
def main
puts CCU.main_profile
say(CCU.main_profile)
end

desc 'readable', 'Reformats (in place) JSON profile configs so that they are not one very long line. Non-destructive if run over JSON multiple times.'
def readable
message = []
get_profiles.each{ |p|
puts "Reformatting #{p} config"
profile = CCU::Profile.new(profile: p).config
message << "Reformatting #{p} config"
oldprofile = JSON.parse(File.read("#{CCU.configdir}/#{p}.json"))
File.open("#{CCU.configdir}/#{p}.json", 'w'){ |f|
f.puts JSON.pretty_generate(profile)
f.puts JSON.pretty_generate(oldprofile)
}
}
say(message.join("\n"))
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/cspace_config_untangler/profile_comparison.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module CspaceConfigUntangler
class ProfileComparison
attr_reader :output
def initialize(profilearray, outputdir)
profiles = profilearray.map{ |p| CCU::Profile.new(profile: p) }
@profiles = profiles.map{ |p| p.name }
@output = "#{outputdir}/compare_#{@profiles[0]}_to_#{@profiles[1]}.csv"
@fields = profiles.map{ |p| p.fields.map{ |f| f.clean} }.map{ |p| by_path(p) }
@combined = combined_fields
@diff = diff_combined
@diff.each{ |k, arr| puts "#{k}: #{arr.size}" }
end

def write_csv
Expand All @@ -19,6 +19,10 @@ def write_csv
fields.each{ |f| csv << f.to_csv }
}
end

def summary
@diff.map{ |k, arr| "#{k}: #{arr.size}" }.join("\n")
end

private

Expand Down

0 comments on commit a2c378c

Please sign in to comment.