Class: DirCat::CommandBuild

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

Overview

Build a catalogue starting from a directory

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.commandObject



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

def self.command
  "build"
end

.descriptionObject



15
16
17
# File 'lib/dircat/cat_on_yaml_cli/command_build.rb', line 15

def self.description
  "Build a catalogue starting from a directory"
end

.usageObject



19
20
21
# File 'lib/dircat/cat_on_yaml_cli/command_build.rb', line 19

def self.usage
  "Usage: dircat build [options]"
end

Instance Method Details

#defaults(options) ⇒ Object



23
24
25
# File 'lib/dircat/cat_on_yaml_cli/command_build.rb', line 23

def defaults(options)
  options.force = false
end

#exec(main, options, rest) ⇒ Object



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/dircat/cat_on_yaml_cli/command_build.rb', line 54

def exec(main, options, rest)

  if rest.length < 1
    $stderr.puts "directory (from which build catalog) is missing"
    $stderr.puts "-h to print help"
    return false
  end

  dirname = rest[0]
  dirname = File.expand_path(dirname)

  #
  # experimental find
  #
  if options.find
    f = File.join(dirname, ".dircat.yml")
    until File.exist?(f) or dirname == "/"
      dirname = File.split(dirname)[0]
    end
    if File.exist?(f)
      puts "file exists!!"
    else
      puts "don't exists"
    end
    exit
  end
  #
  # end experimental find
  #

  unless FileTest.directory?(dirname)
    $stderr.puts "'#{dirname}' not exists or is not a directory"
    return 0
  end

  cat_opts = { }

  #
  # option verbose, progress
  #
  cat_opts[:verbose_level] = 0
  if options.verbose
    cat_opts[:verbose_level] = 1
  end

  cat_opts[:show_progress] = options.show_progress

  #
  # option: output, force
  #
  output = $stdout
  if options.output
    filename = options.output
  else
    filename = "cat_" + File.basename(dirname) + "_" + Date.today.strftime("%Y%m%d") + ".yaml"
  end
  if File.exist?(filename) and not options.force
    $stderr.puts "catalog '#{filename}' exists use --force or -f to overwrite"
    return 0
  end

  #
  # go!
  #

  Signal.trap('INT') { puts "intercepted ctr+c"; exit }

  $stderr.puts "Writing file '#{filename}'"

  output     = File.open(filename, "w")
  start_time = Time.now
  cat        = CatOnYaml.from_dir(dirname, cat_opts)
  end_time   = Time.now
  cat.save_to(output)

  if output != $stdout
    output.close
  end
  $stderr.puts cat.report
  $stderr.puts "elapsed: #{end_time - start_time}"
  $stderr.puts "written to #{filename}" if output != $stdout

  true
end

#option_parser(options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dircat/cat_on_yaml_cli/command_build.rb', line 27

def option_parser(options)
  parser = super(options)
  parser.on("-f", "--force", "force write on existent file") do |v|
    options.force = true
  end

  parser.on("-o [FILE]", "--output [FILE]", String) do |v|
    if options.output
      puts "only one file of output can be used"
      options.exit = true
    end
    options.output = v
  end

  # TODO: progress is true for default
  options.show_progress = true
  parser.on("-p", "--show-progress", "show progress during catalog building") do
    options.show_progress = true
  end

  parser.on("--find", "try to find a catalog from this directory to path") do
    options.find = true
  end

  parser
end