Class: Bitbucket::Cli::Config

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

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



5
6
7
8
9
10
# File 'lib/bitbucket/cli/config.rb', line 5

def initialize
  @default_config = {
    :accounts => [],
  }
  config_from_file
end

Instance Method Details

#add_account(name, password) ⇒ Object



12
13
14
15
# File 'lib/bitbucket/cli/config.rb', line 12

def  name, password
  @config[:accounts] << {:name => name, :password => password}
  write_file
end

#config_from_fileObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/bitbucket/cli/config.rb', line 36

def config_from_file
  begin
    config = YAML::load(IO.read(path))
  rescue Errno::ENOENT
    $stderr.puts(:warning, "YAML configuration file couldn't be found. Using defaults.");
  rescue Psych::SyntaxError
    $stderr.puts(:warning, "YAML configuration file contains invalid syntax. Using defaults.");
  end
  configure(config)
end

#configure(config = nil) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/bitbucket/cli/config.rb', line 51

def configure config=nil
  if config
    @config = config
  else
    @config = @default_config
  end
end

#get_account(name = '') ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/bitbucket/cli/config.rb', line 17

def  name=''
  return nil if @config.nil?
  return nil if @config[:accounts].empty?
  return @config[:accounts].first if name == ''
  accounts = @config[:accounts].select {|a| a.has_key?(name)}
  return nil if accounts.empty?
  return accounts.first
end

#pathObject



32
33
34
# File 'lib/bitbucket/cli/config.rb', line 32

def path
  File.join(ENV['HOME'], '.gitbb')
end

#write(key, val) ⇒ Object



27
28
29
30
# File 'lib/bitbucket/cli/config.rb', line 27

def write key, val
  @config[key] = val
  write_file
end

#write_fileObject



47
48
49
# File 'lib/bitbucket/cli/config.rb', line 47

def write_file
  File.open(path, 'w') {|f| f.write @config.to_yaml }
end