Class: MagnoliaClient::ImportExportSetUp

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

Overview

Parse the arguments to instanciate the proper ImportExport object

Constant Summary collapse

CMD_IMPORT =
"import"
CMD_EXPORT =
"export"
IMPORT_BEHAVIORS =
[:new,:remove,:replace]

Instance Method Summary collapse

Instance Method Details

#get_action(options) ⇒ Object



128
129
130
131
# File 'lib/magnolia_i_e.rb', line 128

def get_action(options)
  return Export.new(options) if options.action == CMD_EXPORT
  return Import.new(options) if options.action == CMD_IMPORT
end

#get_commands(options) ⇒ Object

return the array of commands properly initialized



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/magnolia_i_e.rb', line 104

def get_commands(options)
  commands = Array.new
  if !options.batch_file 
    commands << get_action(options)
  else
    open(File.expand_path(options.batch_file)) do |file|
      file.each do |l| 
        if not l[0] == '#'  
          opts = l.split(/\s+/)
                        
          options.workspace = opts[0]
          options.repository_path = opts[1]
          options.import_file = opts[2] if options.action == CMD_IMPORT
                        
          commands << get_action(options)
        else 
          # skip the line. This is a comment
        end
      end 
    end
    commands
  end        
end

#parse(argv) ⇒ Object

this method does the parsing of the arguments. Validates them return the ImportExport object



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
# File 'lib/magnolia_i_e.rb', line 72

def parse(argv) 
  options = CoolOptions.parse!('[options]') do |opt|       
    opt.on 'action STRING','one of import/export'
    opt.on 'out STRING','path to directory where to put the content of the answer', ""
    opt.on 'repository-path STRING','path on the repository to take into account', "/help"
    opt.on 'workspace STRING',' the repository/workspace to take into account', "website"
    opt.on 'user STRING','user name to authenticate with', 'superuser'
    opt.on 'password STRING','password', 'superuser'
    opt.on 'f)import-file STRING','path to the xml file to import', ''
    opt.on 'server-url STRING','base url of the magnolia service', 'http://localhost:8080/magnoliaAuthor'
    opt.on 'behavior STRING', 'import behavior. One of new/remove/replace or 0/1/2', "0"
    opt.on 'verbose', 'give some more processing info on the command line', false
    opt.on 'console', 'output to standard output, bypass the out option', false
    opt.on 'buffer', 'Use a buffer to stream the output from the server', false
    opt.on 'z)batch-file STRING','start a batch execution of actions from a file. Path to that file', nil
            
    opt.after do |r|
      r.out = File.expand_path(r.out)
      opt.error("Invalid action:"+r.action) unless (r.action == CMD_IMPORT || r.action == CMD_EXPORT)        
      uri = URI.parse(r.server_url)
      opt.error("host is not reachable:"+uri.host+" on port:"+uri.port.to_s) unless Ping.pingecho(uri.host,10,uri.port)  
      r.behavior = parse_behavior(r.behavior) if r.action == CMD_IMPORT
    end        
            
  end
        
  return get_commands(options)
        
end

#parse_behavior(value) ⇒ Object

Parse the import behavior, from an integer or a string Do this while parsing to get proper error output when needed



136
137
138
139
140
141
142
# File 'lib/magnolia_i_e.rb', line 136

def parse_behavior(value)
  IMPORT_BEHAVIORS.each_with_index { |behavior, index|
    return index if value.to_i == index
    return index if value.to_s == behavior.to_s
  }
  raise "Invalid behavior:#{value}"
end