Class: NicInfo::Config

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

Overview

Handles configuration of the application

Constant Summary collapse

@@yaml_config =
<<YAML_CONFIG
output:

  # possible values are NONE, SOME, ALL
  messages: SOME

  # If specified, messages goes to this file
  # otherwise, leave it commented out to go to stderr
  #messages_file: /tmp/NicInfo.messages

  # possible values are TERSE, NORMAL, EXTRA
  data: NORMAL

  # If specified, data goest to this file
  # otherwise, leave it commented out to go to stdout
  #data_file: /tmp/NicInfo.data

  # Page output with system pager when appropriate.
  pager: true

  # Automatically wrap text when possible.
  auto_wrap: true

  # When auto wrapping, automatically determine the terminal
  # width if possible
  detect_width: true

  # The default terminal width if it is not to be detected
  # or cannot be detected
  default_width: 80

  # The color scheme to use. Possible values are
  # - DARK for terminals with dark background
  # - LIGHT for terminals with light background
  # - NONE for no color
  # color is turned off automatically if a pager is used and the pager is not 'less'.
  color_scheme: DARK

cache:

  # The maximum age an item from the cache will be used.
  # This value is in seconds
  cache_expiry: 3600

  # The maximum age an item will be in the cache before it is evicted
  # when the cache is cleaned.
  # This value is in seconds
  cache_eviction: 604800

  # Use the cache.
  # Values are true or false
  use_cache: true

  # Automatically clean the cache.
  clean_cache: true

bootstrap:

  # The base or bootstrap URL. Specifying this will bypass all built-in bootstrapping.
  #base_url: https://rdap-bootstrap.arin.net/bootstrap

  help_root_url: https://rdap-bootstrap.arin.net/bootstrap

  entity_root_url: https://rdap.arin.net/registry

  ip_root_url: https://rdap.arin.net/registry

  as_root_url: https://rdap.arin.net/registry

  domain_root_url: https://rdap.arin.net/registry

  ns_root_url: https://rdap.arin.net/registry

  # Check for updates of the IANA RDAP bootstrap files
  # If true, check for IANA bootstrap files once they have aged past bsfiles_age
  # Values are true or false
  check_bsfiles_age: true 

  # The age at which IANA RDAP bootstrap files are to be checked if check_bsfiles is true
  # This value is in seconds
  bsfiles_age: 604800

  # If the age of the IANA RDAP bootstrap files are considered old according to bsfiles_age
  # and check_bsfiles is true, then this value will cause a fetch of new files if true.
  # Values are true or false
  update_bsfiles: true

search:

  # Substring matching
  # NOT YET USED
  substring: true

security:

  # if HTTPS cannot be established, try HTTP
  try_insecure: true

config:
  # This should not be altered.
  version: 4

YAML_CONFIG

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_data) ⇒ Config

Intializes the configuration with a place to look for the config file If the file doesn’t exist, a default is used. Main routines will do something like NicInfo::Config.new( NicInfo::Config.formulate_app_data_dir() )



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/nicinfo/config.rb', line 34

def initialize app_data

  @options = OpenStruct.new
  @app_data = app_data
  @logger = NicInfo::Logger.new
  @conf_msgs = Array.new
  @factory = NicInfo::Factory.new( self )

  config_file_name = Config.formulate_config_file_name( @app_data )
  if File.exist?( config_file_name )
    @config = load_config( config_file_name )
  else
    @config = YAML.load( @@yaml_config )
  end

  configure_logger()

end

Instance Attribute Details

#conf_msgsObject

Returns the value of attribute conf_msgs.



29
30
31
# File 'lib/nicinfo/config.rb', line 29

def conf_msgs
  @conf_msgs
end

#configObject

Returns the value of attribute config.



29
30
31
# File 'lib/nicinfo/config.rb', line 29

def config
  @config
end

#factoryObject

Returns the value of attribute factory.



29
30
31
# File 'lib/nicinfo/config.rb', line 29

def factory
  @factory
end

#loggerObject

Returns the value of attribute logger.



29
30
31
# File 'lib/nicinfo/config.rb', line 29

def logger
  @logger
end

#optionsObject

Returns the value of attribute options.



29
30
31
# File 'lib/nicinfo/config.rb', line 29

def options
  @options
end

#rdap_bootstrap_dirObject

Returns the value of attribute rdap_bootstrap_dir.



29
30
31
# File 'lib/nicinfo/config.rb', line 29

def rdap_bootstrap_dir
  @rdap_bootstrap_dir
end

#rdap_cache_dirObject

Returns the value of attribute rdap_cache_dir.



29
30
31
# File 'lib/nicinfo/config.rb', line 29

def rdap_cache_dir
  @rdap_cache_dir
end

Class Method Details

.cleanObject



232
233
234
235
236
# File 'lib/nicinfo/config.rb', line 232

def self.clean

  FileUtils::rm_r( formulate_app_data_dir() )

end

.formulate_app_data_dirObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/nicinfo/config.rb', line 238

def self.formulate_app_data_dir
  if Gem.win_platform?
    data_dir = File.join(ENV['APPDATA'], "NicInfo")
  elsif RUBY_PLATFORM =~ /linux/
    data_dir = File.join(ENV['HOME'], ".NicInfo")
  elsif RUBY_PLATFORM =~ /darwin/
    data_dir = File.join(ENV['HOME'], ".NicInfo")
  elsif RUBY_PLATFORM =~ /freebsd/
    data_dir = File.join(ENV['HOME'], ".NicInfo")
  elsif RUBY_PLATFORM =~ /cygwin/
    data_dir = File.join(ENV['HOME'], ".NicInfo")
  else
    raise ScriptError, "system platform is not recognized."
  end
  return data_dir
end

.formulate_config_file_name(data_dir) ⇒ Object



255
256
257
# File 'lib/nicinfo/config.rb', line 255

def self.formulate_config_file_name data_dir
  File.join( data_dir, "config.yaml" )
end

Instance Method Details

#bsfiles_last_update_filenameObject



143
144
145
# File 'lib/nicinfo/config.rb', line 143

def bsfiles_last_update_filename
  File.join(@rdap_bootstrap_dir, NicInfo::BSFILE_LAST_CHECK_FILENAME)
end

#check_bsfiles_age?Boolean

Returns:

  • (Boolean)


147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/nicinfo/config.rb', line 147

def check_bsfiles_age?
  retval = false
  if @config[ NicInfo::BOOTSTRAP ][ NicInfo::CHECK_BSFILES_AGE ]
    t = get_bsfiles_last_update_time
    if t != nil
      configed_age = @config[ NicInfo::BOOTSTRAP ][ NicInfo::BSFILES_AGE ]
      age = t + configed_age
      retval = true if age < Time.now
    else
      retval = true
    end
  end
  return retval
end

#check_config_versionObject



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/nicinfo/config.rb', line 95

def check_config_version
  # check to see if the configuration is old
  config_section = @config[NicInfo::CONFIG]
  if config_section != nil
    config_version = config_section[NicInfo::VERSION_CONFIG]
  end
  if config_version == nil || config_version < NicInfo::CONFIG_VERSION
    # if a reset hasn't been asked for
    if !@options.reset_config
      @logger.mesg( "Your configuration is old. Use --reset to create a new one.", NicInfo::AttentionType::ERROR )
    end
  end
end

#configure_loggerObject

Configures the logger



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/nicinfo/config.rb', line 203

def configure_logger
  output = @config[ NicInfo::OUTPUT ]
  return if output == nil

  @logger.message_level = output[ NicInfo::MESSAGES ]
  @logger.validate_message_level

  messages_file = output[ NicInfo::MESSAGES_FILE ]
  if messages_file != nil
    @logger.message_out = File.open( messages_file, "w+" )
  end

  @logger.data_amount = output[ NicInfo::DATA ]
  @logger.validate_data_amount

  data_file = output[ NicInfo::DATA_FILE ]
  if data_file != nil
    @logger.data_out= File.open( data_file, "w+" )
  end

  @logger.pager=output[ NicInfo::PAGER ]
  @logger.auto_wrap=output[ NicInfo::AUTO_WRAP ]
  @logger.default_width=output[ NicInfo::DEFAULT_WIDTH ]
  @logger.detect_width=output[ NicInfo::DETECT_WIDTH ]

  @logger.color_scheme = output[ NicInfo::COLOR_SCHEME ]
  @logger.validate_color_scheme
end

#copy_bsfilesObject



120
121
122
123
# File 'lib/nicinfo/config.rb', line 120

def copy_bsfiles
  src_dir = File.join( File.dirname( __FILE__ ), NicInfo::BOOTSTRAP_FILE_DIR )
  FileUtils::cp_r( src_dir, @rdap_bootstrap_dir )
end

#get_bsfiles_last_update_timeObject



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/nicinfo/config.rb', line 131

def get_bsfiles_last_update_time
  retval = nil
  fname = bsfiles_last_update_filename
  if File.exist?( fname )
    f = File.open( fname, "r" )
    data = f.read
    f.close
    retval = Time.parse( data )
  end
  return retval
end

#load(name) ⇒ Object



178
179
180
181
182
183
# File 'lib/nicinfo/config.rb', line 178

def load name
  data_file = File.open( File.join( @app_data, name ), "r" )
  retval = data_file.read
  data_file.close
  return retval
end

#load_as_yaml(name, default = nil) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/nicinfo/config.rb', line 185

def load_as_yaml name, default = nil
  file_name = make_file_name( name )
  retval = default
  if File.exist?( file_name )
    data_file = File.open( File.join( @app_data, name ), "r" )
    retval = YAML::load( data_file )
    data_file.close
  elsif default == nil
    raise "#{file_name} does not exist"
  end
  return retval
end

#load_config(config_file_name) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/nicinfo/config.rb', line 109

def load_config config_file_name
  @config = YAML.load( @@yaml_config )
  @config[NicInfo::CONFIG].delete(NicInfo::VERSION_CONFIG)
  merger = proc do |key, v1, v2|
    Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) :
            Array === v1 && Array === v2 ? v1 | v2 :
                    [:undefined, nil, :nil].include?(v2) ? v1 : v2
  end
  @config.merge!( YAML.load( File.open( config_file_name ) ), &merger )
end

#make_file_name(name) ⇒ Object



198
199
200
# File 'lib/nicinfo/config.rb', line 198

def make_file_name name
  File.join( @app_data, name )
end

#save(name, data) ⇒ Object



166
167
168
169
170
# File 'lib/nicinfo/config.rb', line 166

def save name, data
  data_file = File.open( File.join( @app_data, name ), "w" )
  data_file.write data
  data_file.close
end

#save_as_yaml(name, obj) ⇒ Object



172
173
174
175
176
# File 'lib/nicinfo/config.rb', line 172

def save_as_yaml name, obj
  data_file = File.open( File.join( @app_data, name ), "w" )
  data_file.puts YAML::dump(obj)
  data_file.close
end

#set_bsfiles_last_update_time(t = Time.now) ⇒ Object



125
126
127
128
129
# File 'lib/nicinfo/config.rb', line 125

def set_bsfiles_last_update_time t = Time.now
  f = File.open(bsfiles_last_update_filename, "w" )
  f.write t.strftime( "%Y-%m-%d %H:%M:%S")
  f.close
end

#setup_workspaceObject

Setups work space for the application and lays down default config If directory is nil, then it uses its own value



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
# File 'lib/nicinfo/config.rb', line 55

def setup_workspace

  @rdap_bootstrap_dir = File.join( @app_data, NicInfo::BOOTSTRAP_FILE_DIR )
  if ! File.exist?( @app_data )

    @logger.trace "Creating configuration in " + @app_data
    Dir.mkdir( @app_data )
    f = File.open( Config.formulate_config_file_name( @app_data ), "w" )
    f.puts @@yaml_config
    f.close

    @rdap_cache_dir = File.join( @app_data, "rdap_cache" )
    Dir.mkdir( @rdap_cache_dir )

    copy_bsfiles

  else

    if @options.reset_config
      config_file_name = Config.formulate_config_file_name( @app_data )
      @logger.trace "Resetting configuration in " + config_file_name
      f = File.open( config_file_name, "w" )
      f.puts @@yaml_config
      f.close
      @config = YAML.load( File.open( config_file_name ) )
      @logger.trace "Resetting bootstrap files in " + @rdap_bootstrap_dir
      begin
        FileUtils::rm_r( @rdap_bootstrap_dir )
      rescue Errno::ENOENT
        # do nothing
      end
      copy_bsfiles
    end
    @logger.trace "Using configuration found in " + @app_data
    @rdap_cache_dir = File.join( @app_data, "rdap_cache" )

  end

end

#update_bsfiles?(aged) ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/nicinfo/config.rb', line 162

def update_bsfiles? aged
  return aged && @config[ NicInfo::BOOTSTRAP ][ NicInfo::UPDATE_BSFILES ]
end