Class: Manifestly::CLI

Inherits:
Thor
  • Object
show all
Includes:
CommandLineReporter, Ui
Defined in:
lib/manifestly/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ui

included, #select

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/manifestly/cli.rb', line 17

def self.exit_on_failure?
  true
end

.file_option(description_action = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/manifestly/cli.rb', line 51

def self.file_option(description_action=nil)
  description = "The local manifest file"
  description += " to #{description_action}" if description_action

  method_option :file,
                desc: description,
                type: :string,
                banner: '',
                required: true
end

.repo_file_option(description = nil) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/manifestly/cli.rb', line 42

def self.repo_file_option(description=nil)
  description ||= "The name of the repository file, with path if applicable"
  method_option :repo_file,
                desc: description,
                type: :string,
                banner: '',
                required: true
end

.repo_optionObject



34
35
36
37
38
39
40
# File 'lib/manifestly/cli.rb', line 34

def self.repo_option
  method_option :repo,
                desc: "The github manifest repository to use, given as 'organization/reponame'",
                type: :string,
                banner: '',
                required: true
end

.search_paths_optionObject

Common command line options



25
26
27
28
29
30
31
32
# File 'lib/manifestly/cli.rb', line 25

def self.search_paths_option
  method_option :search_paths,
                desc: "A list of paths where git repositories can be found",
                type: :array,
                required: false,
                banner: '',
                default: '.'
end

Instance Method Details

#applyObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/manifestly/cli.rb', line 224

def apply
  begin
    manifest = load_manifest(file: options[:file]) || error!
    manifest.items.each{ |item| item.checkout_commit!(options[:update]) }
  rescue Manifestly::ManifestItem::MultipleSameNameRepositories => e
    say "Multiple repositories have the same name (#{e.message}) so we " +
        "can't apply the manifest. Try limiting the search_paths or " +
        "separate the duplicates."
    error!
  rescue Manifestly::Repository::CommitNotPresent => e
    say "Could not find commit #{e.sha} in repository #{e.repository.github_name_or_path}. " +
        "Try running again with the `--update` option."
    error!
  end


end

#createObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/manifestly/cli.rb', line 187

def create
  manifest = if options[:based_on]
    load_manifest(file: options[:based_on]) || error!
  else
    Manifest.new
  end

  if options[:interactive]
    present_create_menu(manifest)
  else
    repos_to_remove = get_repos_from_options(options[:remove])
    repos_to_remove.each do |repo_to_remove|
      manifest.remove_repository(repo_to_remove)
    end

    repos_to_add = get_repos_from_options(options[:add])
    repos_to_add.each do |repo_to_add|
      manifest.add_repository(repo_to_add)
    end

    write_manifest(manifest, false)
  end
end

#diffObject



449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/manifestly/cli.rb', line 449

def diff
  repository = Repository.load_cached(options[:repo], update: true)
  from_manifest = load_manifest(repository: repository, sha: options[:from_sha])
  to_manifest = load_manifest(repository: repository, sha: options[:to_sha])

  if !from_manifest || !to_manifest
    say("Could not load the 'from' manifest so cannot continue with the diff.") if !from_manifest
    say("Could not load the 'to' manifest so cannot continue with the diff.") if !to_manifest
    error!
  end

  manifest_diff = ManifestDiff.new(from_manifest, to_manifest)
  manifest_diff.to_markdown
end

#downloadObject



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/manifestly/cli.rb', line 284

def download
  repository = Repository.load_cached(options[:repo], update: true)

  commit_content = begin
    repository.get_commit_content(options[:sha])
  rescue Manifestly::Repository::CommitNotPresent
    say('That SHA is invalid')
    error!
  end

  save_as = options[:save_as]

  if save_as.nil?
    # Get the whole SHA so filenames are consistent
    sha = repository.find_commit(options[:sha]).sha
    save_as = "#{sha[0..9]}.manifest"
  end

  File.open(save_as, 'w') { |file| file.write(commit_content) }
  say "Downloaded #{save_as}.  #{commit_content.split("\n").count} line(s)."
end

#findObject



415
416
417
418
419
420
# File 'lib/manifestly/cli.rb', line 415

def find
  repository = Repository.load_cached(options[:repo], update: true)
  shas = repository.get_shas_with_tag(tag: options[:tag], file: options[:repo_file])
  shas = shas.take(options[:limit].to_i) if options[:limit]
  say shas.uniq.join("\n")
end

#listObject



315
316
317
318
319
# File 'lib/manifestly/cli.rb', line 315

def list
  repository = Repository.load_cached(options[:repo], update: true)
  commits = repository.file_commits(options[:repo_file])
  present_list_menu(commits, show_author: true)
end

#tagObject



365
366
367
368
369
370
371
372
373
374
# File 'lib/manifestly/cli.rb', line 365

def tag
  repository = Repository.load_cached(options[:repo], update: true)

  begin
    repository.tag_scoped_to_file(
      tag: options[:tag], sha: options[:sha], message: options[:message], push: true
    )
  rescue Manifestly::Repository::ShaAlreadyTagged
  end
end

#uploadObject



256
257
258
259
260
261
262
263
264
265
# File 'lib/manifestly/cli.rb', line 256

def upload
  repository = Repository.load_cached(options[:repo], update: true)

  begin
    repository.push_file!(options[:file], options[:repo_file], options[:message])
  rescue Manifestly::Repository::ManifestUnchanged
  ensure
    say repository.current_commit
  end
end

#versionObject



465
466
467
# File 'lib/manifestly/cli.rb', line 465

def version
  say "Manifestly #{Manifestly::VERSION} - https://github.com/openstax/manifestly"
end