Class: Adminix::Config

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

Constant Summary collapse

DEFAULT_HOST =
'http://api.adminix.io'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commands ⇒ Object

Returns the value of attribute commands.



10
11
12
# File 'lib/adminix/config.rb', line 10

def commands
  @commands
end

#host ⇒ Object

Returns the value of attribute host.



10
11
12
# File 'lib/adminix/config.rb', line 10

def host
  @host
end

#monitor_logs_paths ⇒ Object

Returns the value of attribute monitor_logs_paths.



10
11
12
# File 'lib/adminix/config.rb', line 10

def monitor_logs_paths
  @monitor_logs_paths
end

#options ⇒ Object

Returns the value of attribute options.



10
11
12
# File 'lib/adminix/config.rb', line 10

def options
  @options
end

#secret_key ⇒ Object

Returns the value of attribute secret_key.



10
11
12
# File 'lib/adminix/config.rb', line 10

def secret_key
  @secret_key
end

#service_address ⇒ Object

Returns the value of attribute service_address.



10
11
12
# File 'lib/adminix/config.rb', line 10

def service_address
  @service_address
end

#service_id ⇒ Object

Returns the value of attribute service_id.



10
11
12
# File 'lib/adminix/config.rb', line 10

def service_id
  @service_id
end

#service_name ⇒ Object

Returns the value of attribute service_name.



10
11
12
# File 'lib/adminix/config.rb', line 10

def service_name
  @service_name
end

Class Method Details

.setup(opts) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/adminix/config.rb', line 21

def self.setup(opts)
  conf = self.instance

  conf.service_id = opts[:service_id]
  conf.service_name = opts[:service_name]
  conf.secret_key = opts[:secret_key]

  conf.export
end

Instance Method Details

#attributes ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/adminix/config.rb', line 31

def attributes
  {
    service_name: service_name,
    service_id: service_id,
    service_address: service_address,
    secret_key: secret_key,
    options: options || [],
    commands: commands || []
  }
end

#config_file_path ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/adminix/config.rb', line 68

def config_file_path
  return @config_path unless @config_path.nil?

  paths = [
    "#{ENV['HOME']}/.adminix.json",
    '.adminix.json'
  ]
  paths << ENV['ADMINIX_CONFIG_PATH'] if !ENV['ADMINIX_CONFIG_PATH'].nil?

  config_path = nil

  paths.reverse.each do |path|
    if File.exists?(path)
      @config_path = path
      break
    end
  end

  @config_path or raise "adminix.json doesn't exists"
end

#errors ⇒ Object



130
131
132
# File 'lib/adminix/config.rb', line 130

def errors
  @errors
end

#export(path = nil) ⇒ Object



46
47
48
49
50
51
# File 'lib/adminix/config.rb', line 46

def export(path=nil)
  file_path = path || "#{ENV['HOME']}/.adminix.json"
  file_content = JSON.pretty_generate(attributes)
  File.delete(file_path) if File.exists?(file_path)
  File.open(file_path, 'w') { |file| file.write(file_content) }
end

#file_exists? ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/adminix/config.rb', line 42

def file_exists?
  File.exists?("#{ENV['HOME']}/.adminix.json")
end

#import ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/adminix/config.rb', line 53

def import
  content = File.read(config_file_path)
  hs = JSON.parse(content)

  self.service_name = hs['service_name']
  self.service_id = hs['service_id']
  self.service_address = hs['service_address']
  self.secret_key = hs['secret_key']
  self.host = hs['host'] || ENV['ADMINIX_CONFIG_HOST'] || DEFAULT_HOST
  self.options = hs['options'] || []
  self.commands = hs['commands'] || []
  
  valid?
end

#valid? ⇒ Boolean

Returns:

  • (Boolean)


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
124
125
126
127
128
# File 'lib/adminix/config.rb', line 89

def valid?
  errors = {}

  if service_name.nil?
    errors[:service_name] ||= []
    errors[:service_name] << "should't be blank"
  end

  if service_id.nil?
    errors[:service_id] ||= []
    errors[:service_id] << "should't be blank"
  end

  if secret_key.nil?
    errors[:secret_key] ||= []
    errors[:secret_key] << "should't be blank"
  end

  unless monitor_logs_paths.nil?
    unless monitor_logs_paths.is_a? Array
      errors[:monitor_logs_paths] << "should be array"
    end
  end

  unless options.nil?
    unless options.is_a? Array
      errors[:options] << "should be array"
    end
  end

  unless commands.nil?
    unless commands.is_a? Array
      errors[:commands] << "should be array"
    end
  end

  @errors = errors

  errors.count == 0
end