Class: ImageVenue::Cli

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCli

Returns a new instance of Cli.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/image_venue/cli.rb', line 17

def initialize
  self.options = GetoptLong.new(
    ['-d', '--debug', GetoptLong::NO_ARGUMENT],
    ['-p', '--password', GetoptLong::REQUIRED_ARGUMENT],
    ['-u', '--username', GetoptLong::REQUIRED_ARGUMENT]
  )
  self.options_hash = {}
  self.options.each do |option, argument|
    self.options_hash[option] = argument
  end
  self.command = ARGV.shift
  self.arguments = ARGV.clone
  return nil
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



15
16
17
# File 'lib/image_venue/cli.rb', line 15

def arguments
  @arguments
end

#commandObject

Returns the value of attribute command.



14
15
16
# File 'lib/image_venue/cli.rb', line 14

def command
  @command
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#options_hashObject

Returns the value of attribute options_hash.



13
14
15
# File 'lib/image_venue/cli.rb', line 13

def options_hash
  @options_hash
end

Class Method Details

.mainObject



6
7
8
9
# File 'lib/image_venue/cli.rb', line 6

def main
  cli = self.new
  cli.main
end

Instance Method Details

#mainObject



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

def main
  ImageVenue.debug = self.options_hash.has_key?('-d')

  if self.command.nil? or self.command.empty?
    puts "USAGE: #{$0} -u <USERNAME> -p <PASSWORD> <COMMAND> [<ARGUMENT>]"
    return false
  else
    command_block = nil
    self.command = self.command.to_sym

    if self.command == :list
      command_block = lambda do |connection|
        connection.directories.each do |directory|
          puts directory.name
        end
      end

    elsif self.command == :files
      directory_name = self.arguments.shift
      unless directory_name.nil? or directory_name.empty?
        command_block = lambda do |connection|
          directory = connection.directories.find {|dir| dir.name == directory_name}
          if directory
            directory.files.each do |file|
              puts file.real_name
            end
          else
            puts "NOT_FOUND_ERROR"
          end
        end
      end

    elsif self.command == :create
      directory_name = self.arguments.shift
      unless directory_name.nil? or directory_name.empty?
        command_block = lambda do |connection|
          directory = ImageVenue::Directory.new(connection, directory_name)
          if directory.save
            puts "OK"
          else
            puts "ERROR"
          end
        end
      end

    elsif self.command == :upload
      directory_name = self.arguments.shift
      filepathes = self.arguments
      unless directory_name.nil? or directory_name.empty? or filepathes.nil? or filepathes.empty?
        command_block = lambda do |connection|
          directory = connection.directories.find {|dir| dir.name == directory_name}
          if directory
            directory.select
            filepathes.each do |filepath|
              file = ImageVenue::File.new(directory, filepath)
              if file.save
                puts "OK #{filepath}"
              else
                puts "ERROR #{filepath}"
              end
            end
          else
          end
        end
      end

    elsif self.command == :delete
      directory_name = self.arguments.shift
      unless directory_name.nil? or directory_name.empty?
        command_block = lambda do |connection|
          directory = connection.directories.find {|dir| dir.name == directory_name}
          if directory
            if directory.destroy
              puts "OK"
            else
              puts "ERROR"
            end
          else
            puts "NOT_FOUND_ERROR"
          end
        end
      end
    end

    if command_block
      ImageVenue.(options_hash['-u'], options_hash['-p']) do |connection|
        command_block.call(connection)
        connection.logout
      end
    end
  end
end