Class: Vman::Menu

Inherits:
Object
  • Object
show all
Defined in:
lib/vman/menu.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Menu

Returns a new instance of Menu.



10
11
12
13
# File 'lib/vman/menu.rb', line 10

def initialize(config)
  @prompt = TTY::Prompt.new
  @s3     = S3.new(config['bucket_uri'])
end

Instance Method Details

#deleteObject



79
80
81
82
83
84
85
86
87
# File 'lib/vman/menu.rb', line 79

def delete
  obj_versions = get_versions

  @prompt.select("Select a version to delete") do |prompt|
    obj_versions.each do |v|
      prompt.choice "#{v.id} (#{v.get.last_modified})", -> { prompt_delete(v) }
    end
  end
end

#listObject



70
71
72
73
74
75
76
77
# File 'lib/vman/menu.rb', line 70

def list
  objects = @s3.list

  @prompt.ok("All current objects in bucket:")
  objects.each_with_index do |obj, i|
    @prompt.say("#{i+1}. #{obj.key}")
  end
end

#retrieveObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vman/menu.rb', line 26

def retrieve
  obj_versions = get_versions

  @prompt.select("Select a version to retrieve") do |prompt|
    obj_versions.each do |v|
      prompt.choice "#{v.id} (#{v.get.last_modified})", -> {
        name = v.object_key + '_' + v.id
        download(v, name)
        @prompt.ok("#{name} downloaded to working directory")
      }
    end
  end
end

#showObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/vman/menu.rb', line 15

def show
  @prompt.select('Select an action') do |p|
    p.choice "Retrieve an object", -> { retrieve }
    p.choice "Store an object", -> { store }
    p.choice "List all versions of an object", -> { versions }
    p.choice "Show current version of an object", -> { version }
    p.choice "List all objects", -> { list }
    p.choice "Delete a version of an object", -> { delete }
  end
end

#storeObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vman/menu.rb', line 40

def store
  file        = prompt_file
  key         = File.basename(file)
  stored_file = @s3.store(file, key)

  if stored_file && stored_file.class == Aws::S3::Object
    @prompt.ok("Successfully uploaded \"#{key}\" to S3")
  else
    @prompt.error("Error uploading \"#{key}\" to S3")
  end
end

#versionObject



63
64
65
66
67
68
# File 'lib/vman/menu.rb', line 63

def version
  obj_version = get_version

  @prompt.ok("Current version for #{obj_version.object_key}:")
  @prompt.say("#{obj_version.get.version_id} (#{obj_version.get.last_modified})")
end

#versionsObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/vman/menu.rb', line 52

def versions
  obj_versions = get_versions

  @prompt.ok("Total number of versions: #{obj_versions.count}")
  obj_versions.each do |v|
    msg    = "  " + v.id + " (#{v.last_modified})"
    msg[0] = "" if v.is_latest
    @prompt.say("#{msg}")
  end
end