Class: Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Config

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength



7
8
9
10
# File 'lib/config.rb', line 7

def initialize(args={}) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
  set_values(args)
  check_values
end

Instance Attribute Details

#database_urlObject (readonly)

Returns the value of attribute database_url.



5
6
7
# File 'lib/config.rb', line 5

def database_url
  @database_url
end

#dry_runObject (readonly)

Returns the value of attribute dry_run.



5
6
7
# File 'lib/config.rb', line 5

def dry_run
  @dry_run
end

#files_locationObject (readonly)

Returns the value of attribute files_location.



5
6
7
# File 'lib/config.rb', line 5

def files_location
  @files_location
end

#if_backupObject (readonly)

Returns the value of attribute if_backup.



5
6
7
# File 'lib/config.rb', line 5

def if_backup
  @if_backup
end

#limitObject (readonly)

Returns the value of attribute limit.



5
6
7
# File 'lib/config.rb', line 5

def limit
  @limit
end

#org_idObject (readonly)

Returns the value of attribute org_id.



5
6
7
# File 'lib/config.rb', line 5

def org_id
  @org_id
end

#repo_idObject (readonly)

Returns the value of attribute repo_id.



5
6
7
# File 'lib/config.rb', line 5

def repo_id
  @repo_id
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



5
6
7
# File 'lib/config.rb', line 5

def threshold
  @threshold
end

#user_idObject (readonly)

Returns the value of attribute user_id.



5
6
7
# File 'lib/config.rb', line 5

def user_id
  @user_id
end

Instance Method Details

#abort_message(intro) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/config.rb', line 89

def abort_message(intro)
  "\n#{intro} Example usage:\n"+
  "\n  $ bin/travis_backup 'postgres://my_database_url' --threshold 6\n" +
  "\nor using in code:\n" +
  "\n  Backup.new(database_url: 'postgres://my_database_url', threshold: 6)\n" +
  "\nYou can also set it using environment variables or config/database.yml file.\n"
end

#argv_optionsObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/config.rb', line 97

def argv_options
  argv_copy = ARGV.clone
  options = {}
  OptionParser.new do |opt|
    opt.on('-b', '--backup') { |o| options[:if_backup] = o }
    opt.on('-d', '--dry_run') { |o| options[:dry_run] = o }
    opt.on('-l', '--limit X') { |o| options[:limit] = o.to_i }
    opt.on('-t', '--threshold X') { |o| options[:threshold] = o.to_i }
    opt.on('-f', '--files_location X') { |o| options[:files_location] = o }
    opt.on('-u', '--user_id X') { |o| options[:user_id] = o.to_i }
    opt.on('-r', '--repo_id X') { |o| options[:repo_id] = o.to_i }
    opt.on('-o', '--org_id X') { |o| options[:org_id] = o.to_i }
  end.parse!

  options[:database_url] = ARGV.shift if ARGV[0]
  argv_copy.each do |arg|
    ARGV.push(arg)
  end
  options
end

#check_valuesObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/config.rb', line 76

def check_values
  if !@threshold
    message = abort_message("Please provide the threshold argument. Data younger than it will be omitted. " +
      "Threshold defines number of months from now.")
    abort message
  end

  if !@database_url
    message = abort_message("Please provide proper database URL.")
    abort message
  end
end

#first_not_nil(*arr) ⇒ Object



118
119
120
# File 'lib/config.rb', line 118

def first_not_nil(*arr)
  arr.compact.first
end

#set_values(args) ⇒ Object



12
13
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/config.rb', line 12

def set_values(args)
  config = yaml_load('config/settings.yml')
  connection_details = yaml_load('config/database.yml')
  argv_opts = argv_options
  @if_backup = first_not_nil(
    args[:if_backup],
    argv_opts[:if_backup],
    ENV['IF_BACKUP'],
    config.dig('backup', 'if_backup'),
    true
  )
  @dry_run = first_not_nil(
    args[:dry_run],
    argv_opts[:dry_run],
    ENV['BACKUP_DRY_RUN'],
    config.dig('backup', 'dry_run'),
    false
  )
  @limit = first_not_nil(
    args[:limit],
    argv_opts[:limit],
    ENV['BACKUP_LIMIT'],
    config.dig('backup', 'limit'),
    1000
  )
  @threshold = first_not_nil(
    args[:threshold],
    argv_opts[:threshold],
    ENV['BACKUP_THRESHOLD'],
    config.dig('backup', 'threshold')
  )
  @files_location = first_not_nil(
    args[:files_location],
    argv_opts[:files_location],
    ENV['BACKUP_BACKUP_FILES_LOCATION'],
    config.dig('backup', 'files_location'),
    './dump'
  )
  @database_url = first_not_nil(
    args[:database_url],
    argv_opts[:database_url],
    ENV['DATABASE_URL'],
    connection_details.dig(ENV['RAILS_ENV'])
  )
  @user_id = first_not_nil(
    args[:user_id],
    argv_opts[:user_id],
    ENV['USER_ID'],
    config.dig('backup', 'user_id')
  )
  @repo_id = first_not_nil(
    args[:repo_id],
    argv_opts[:repo_id],
    ENV['REPO_ID'],
    config.dig('backup', 'repo_id')
  )
  @org_id = first_not_nil(
    args[:org_id],
    argv_opts[:org_id],
    ENV['ORG_ID'],
    config.dig('backup', 'org_id')
  )
end

#yaml_load(url) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/config.rb', line 122

def yaml_load(url)
  begin
    YAML.load(File.open(url))
  rescue => e
    {}
  end
end