Class: BitbucketMigration::Configuration

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

Constant Summary collapse

DEFAULT_CONFIG =
'config.yml'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configfile) ⇒ Configuration

Returns a new instance of Configuration.



9
10
11
12
13
14
15
16
17
18
# File 'lib/bitbucket_migration/configuration.rb', line 9

def initialize(configfile)
  config = read(configfile)

  if(valid?(config))
    @file = configfile
    @username =config['username']
    @password =config['password']
    @team     =config['team']
  end
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



7
8
9
# File 'lib/bitbucket_migration/configuration.rb', line 7

def file
  @file
end

#passwordObject (readonly)

Returns the value of attribute password.



7
8
9
# File 'lib/bitbucket_migration/configuration.rb', line 7

def password
  @password
end

#teamObject (readonly)

Returns the value of attribute team.



7
8
9
# File 'lib/bitbucket_migration/configuration.rb', line 7

def team
  @team
end

#usernameObject (readonly)

Returns the value of attribute username.



7
8
9
# File 'lib/bitbucket_migration/configuration.rb', line 7

def username
  @username
end

Instance Method Details

#read(configfile = nil) ⇒ Object

Read configuration file



21
22
23
24
25
26
27
28
29
30
# File 'lib/bitbucket_migration/configuration.rb', line 21

def read(configfile=nil)
  file = configfile ? configfile : DEFAULT_CONFIG
  begin
    conf = YAML::load_file(file)
    return conf
  rescue
    puts "[Error] " + $!.to_s
    exit
  end
end

#valid?(yml) ⇒ Boolean

Check if configuration file has required keys and values

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bitbucket_migration/configuration.rb', line 33

def valid?(yml)
  expected = ['username','password','team']

  # check if required keys present
  valid_keys = expected.all? { |e| yml.keys.include?(e) }

  # check if values exists and not nil
  valid_values = yml.all? { |k,v| v.nil? || v.empty? || v.size == 0 }

  return valid_keys || valid_values ? true : false
end