Module: MLS::CLI

Defined in:
lib/mls/cli.rb

Defined Under Namespace

Modules: Documents

Class Method Summary collapse

Class Method Details

.optionsObject



7
8
9
10
11
12
# File 'lib/mls/cli.rb', line 7

def self.options
  if !class_variable_defined?(:@@options)
    @@options = {}
  end
  @@options
end

.parse_args(args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mls/cli.rb', line 14

def self.parse_args(args)
  OptionParser.new do |opts|
    opts.on("-aURL", "--auth=URL", "URL Credentials for MLS, S3 or B2") do |arg|
      url = URI.parse(arg)
      case url.scheme
      when 's3' # ACCESS_KEY:SECRET_KEY@BUCKET[/PREFIX][?parition=4]
        MLS::CLI.options[:s3] = {
          access_key_id:      URI.unescape(url.user),
          secret_access_key:  URI.unescape(url.password),
          bucket:             URI.unescape(url.host)
        }
        MLS::CLI.options[:s3][:prefix] = url.path if url.path && !url.path.empty?
        url.query.split('&').each do |qp|
          key, value = qp.split('=').map { |d| URI.unescape(d) }
          case key
          when 'partition'
            MLS::CLI.options[:s3][:partition] = true
            MLS::CLI.options[:s3][:partition_depth] = value.to_i
          end
        end
      when 'b2'
        MLS::CLI.options[:b2] = {
          account_id:         URI.unescape(url.user),
          application_key:    URI.unescape(url.password),
          bucket:             URI.unescape(url.host),
          prefix:             url.path&.empty? ? url.path : nil
        }
        url.query.split('&').each do |qp|
          key, value = qp.split('=').map { |d| URI.unescape(d) }
          case key
          when 'partition'
            MLS::CLI.options[:b2][:partition] = value.to_i
          end
        end
      when 'mls'
        arg = arg.sub('mls://', 'https://')
        MLS::CLI.options[:mls] = arg
        MLS::Model.establish_connection(adapter: 'sunstone', url: arg)
      end
    end
  end.parse!(args)
end