Class: Mast::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/mast/cli.rb

Overview

Manifest Console Command

Constant Summary collapse

DIGESTS =
[:md5, :sha1, :sha128, :sha256, :sha512]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCli

Returns a new instance of Cli.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mast/cli.rb', line 24

def initialize
  @options = {}
  @options[:all]     = false
  @options[:file]    = nil
  @options[:bang]    = nil
  @options[:digest]  = nil
  @options[:exclude] = []
  @options[:ignore]  = []
  @options[:include] = []

  @command = []

  @quiet = false
end

Instance Attribute Details

#optionsObject (readonly)

Options for Manifest class taken from commandline arguments.



21
22
23
# File 'lib/mast/cli.rb', line 21

def options
  @options
end

#quietObject

Returns the value of attribute quiet.



18
19
20
# File 'lib/mast/cli.rb', line 18

def quiet
  @quiet
end

Class Method Details

.runObject



12
13
14
# File 'lib/mast/cli.rb', line 12

def self.run
  new.run
end

Instance Method Details

#cleanObject

Clean (or clobber if you prefer) non-manifest files.



216
217
218
219
220
221
222
223
224
225
# File 'lib/mast/cli.rb', line 216

def clean
  answer = confirm_clean(manifest.cleanlist)
  case answer.downcase
  when 'y', 'yes'
    manifest.clean
  else
    report_cancelled('Clean')
    exit!
  end
end

#diffObject

Show diff comparison between listed and actual.



187
188
189
190
# File 'lib/mast/cli.rb', line 187

def diff
  result = manifest.diff
  report_difference(result)
end

#generateObject

Default command – output manifest.



150
151
152
153
154
155
156
# File 'lib/mast/cli.rb', line 150

def generate
  #if file
  #  update
  #else
    manifest.generate
  #end
end

#helpObject

Display command help information.



242
243
244
# File 'lib/mast/cli.rb', line 242

def help
  report_help
end

#listObject

List files in manifest file.



177
178
179
# File 'lib/mast/cli.rb', line 177

def list
  puts manifest.filelist
end

#newObject

Files found, but not listed in manifest.



201
202
203
204
205
206
# File 'lib/mast/cli.rb', line 201

def new
  list = manifest.whatsnew
  unless list.empty?
    report_whatsnew(list)
  end
end

#oldObject

Files listed in manifest, but not found.



193
194
195
196
197
198
# File 'lib/mast/cli.rb', line 193

def old
  list = manifest.whatsold
  unless list.empty?
    report_whatsold(list)
  end
end

#option_parserObject

Parse command line options.



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
138
139
140
141
142
143
144
145
146
147
# File 'lib/mast/cli.rb', line 80

def option_parser
  OptionParser.new do |opt|
    opt.on "-f", "--file FILE", "Path to manifest file. Looks for file matching /MANIFEST(|.txt)/i by default." do |file|
      @options[:file] = file
    end
    opt.on "-g", "--digest TYPE", "Include cryptographic signature. Type can be either md5, sha1, sha128, sha256, or sha512." do |digest|
      @options[:digest] = digest
    end
    opt.on "-x", "--exclude GLOB", "Exclude file or dir from the manifest matching against full pathname. Can be used repeatedly." do |glob|
      @options[:exclude] << glob
    end
    opt.on "-i", "--ignore GLOB",
           "Exclude file or dir from manifest matching against an entry's basename. Can be used repeatedly." do |glob|
      @options[:ignore] << glob
    end
    opt.on "-a", "--all", "Include all files. This deactivates default exclusions so it is possible to make complete list of all contents." do |bool|
      @options[:all] = true
    end
    opt.on "--bang", "-b", "Generate manifest using the options from the bang line of the manifest file." do |bool|
      @options[:bang] = true
    end
    opt.on "--dir", "-d", "When creating a list include directory paths; by default only files are listed." do |bool|
      @options[:dir] = bool
    end
    opt.on "--[no-]head", "Suppress mast header from output." do |bool|
      @options[:headless] = !bool
    end
    opt.on "-c", "--create", "Generate a new manifest. (default)" do
      @command << :create
    end
    opt.on "-u", "--update", "Update an existing manifest." do
      @command << :update
    end
    opt.on "-l", "--list", "List the files given in the manifest file. (Use -f to specify an alternate file.)" do
      @command << :list
    end
    opt.on "-D", "--diff", "Diff manifest file against actual." do
      @command << :diff
    end
    opt.on "-n", "--new", "List existent files that are not given in the manifest." do
      @command << :new
    end
    opt.on "-o", "--old", "List files given in the manifest but are non-existent." do
      @command << :old
    end
    opt.on "-v", "--verify", "Verify that a manifest matches actual." do
      @command << :verify
    end
    opt.on "--clean", "Remove non-manifest files. (Will ask for confirmation first.)" do
      @command << :clean
    end
    opt.on "-r", "--recent", "Verify that a manifest is more recent than actual." do
      @command << :recent
    end
    opt.on "-h", "--help", "Display this help message." do
      @command << :help
    end
    opt.on "-H" do
      puts opt; exit
    end
    opt.on "-q", "--quiet", "Suppress all extraneous output." do
      @quiet = true
    end
    opt.on "--debug", "Run in debug mode." do
      $DEBUG = true
    end
  end
end

#recentObject

Verify manifest, then check to see that it is not older than files it lists.



229
230
231
232
233
234
235
236
237
238
239
# File 'lib/mast/cli.rb', line 229

def recent
  check = manifest.verify
  if !check
    report_verify(check)
    exit -1 
  end
  if !FileUtils.uptodate?(manifest.file, manifest.filelist)
    report_outofdate
    exit -1
  end
end

#run(argv = nil) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/mast/cli.rb', line 40

def run(argv=nil)
  begin
    run_command(argv)
  rescue => err
    raise err if $DEBUG
    report err
  end
end

#run_command(argv) ⇒ Object

Run command.



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
# File 'lib/mast/cli.rb', line 50

def run_command(argv)
  argv = (argv || ARGV).dup
 
  @original_arguments = argv.dup

  option_parser.parse!(argv)

  @options[:include] = argv.empty? ? nil : argv #.dup

  if @command.size > 1
    raise ArgumentError, "Please issue only one command."
  end

  case @command.first
  when :help     then help
  when :create   then generate
  when :update   then update
  when :list     then list
  when :diff     then diff
  when :new      then new
  when :old      then old
  when :verify   then verify
  when :clean    then clean
  when :recent   then recent
  else
    generate
  end
end

#updateObject Also known as: up

Update a MANIFEST file for this package.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/mast/cli.rb', line 159

def update
  if manifest.verify  
  else
    begin
      diff = manifest.diff
      file = manifest.update
    rescue Manifest::NoManifestError => e
      puts e.message
      exit -1
    end
    report_difference(diff)
    #report_updated(file)
  end
end

#verifyObject



209
210
211
212
213
# File 'lib/mast/cli.rb', line 209

def verify
  check = manifest.verify
  report_verify(check)
  exit -1 unless check
end