Class: DirCat::CommandDiff

Inherits:
OptParseCommand::Command
  • Object
show all
Defined in:
lib/dircat/cat_on_yaml_cli/command_diff.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.commandObject



7
8
9
# File 'lib/dircat/cat_on_yaml_cli/command_diff.rb', line 7

def self.command
  "diff"
end

.descriptionObject



11
12
13
# File 'lib/dircat/cat_on_yaml_cli/command_diff.rb', line 11

def self.description
  "Show diff from two catalogs"
end

.usageObject



15
16
17
18
19
20
21
# File 'lib/dircat/cat_on_yaml_cli/command_diff.rb', line 15

def self.usage
  <<-EOS
Usage: dircat diff [options] <filedircat1> <filedircat2>
diff first catalog from second (<filedircat1> - <filedircat2>)
and then print the difference with the format specified on output
  EOS
end

Instance Method Details

#exec(main, options, rest) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dircat/cat_on_yaml_cli/command_diff.rb', line 31

def exec(main, options, rest)
  if rest.length < 2
    puts "you must provide two args (catalogs or directory)"
    puts "-h to print help"
    return false
  end

  cat_filename1 = rest[0]
  cat_filename2 = rest[1]

  #
  # process first argument
  #
  if File.directory?(cat_filename1)
    puts "build first set from directory #{cat_filename1}"
    s1 = CatOnYaml.from_dir(cat_filename1)
  elsif File.exists?(cat_filename1)
    puts "load catalog #{cat_filename1}"
    s1 = CatOnYaml.from_file(cat_filename1)
  else
    puts "#{cat_filename1} is not a catalog file or directory"
    return 1
  end

  #
  # process second argument
  #
  if File.directory?(cat_filename2)
    puts "build first set from directory #{cat_filename2}"
    s2 = CatOnYaml.from_dir(cat_filename2)
  elsif File.exists?(cat_filename2)
    puts "load catalog #{cat_filename2}"
    s2 = CatOnYaml.from_file(cat_filename2)
  else
    puts "#{cat_filename2} is not a catalog file or directory"
    return 1
  end

  s3 = s1 - s2

  if s3.empty?
    puts "no difference (first catalog contains all file of second catalog)"
  else
    puts "differences (file in first catalog not contained in the second catalog)"
    case options.format
      when "simple"
        s3.fmt_simple
      when "ruby"
        s3.fmt_ruby(".")
      else
        s3.fmt_simple
    end
  end
  true
end

#option_parser(options) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/dircat/cat_on_yaml_cli/command_diff.rb', line 23

def option_parser(options)
  parser = super(options)
  parser.on("-f FORMAT", "--fmt FORMAT", "format (simple, ruby)") do |v|
    options.format = v
  end
  parser
end