Class: Asca::Tools::Configuration

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

Constant Summary collapse

ROOTDIR =
File.expand_path '~/.com.hurryup.asca'
JSONFILE =
File.expand_path 'config.json', ROOTDIR
CACHE_DIR =
File.expand_path 'cache', ROOTDIR

Class Method Summary collapse

Class Method Details

.get_config(key) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/asca/tools/configuration.rb', line 46

def get_config(key)
    if !File.exist?(JSONFILE)
        reset_config
    end
    file_content = File.read(JSONFILE)
    configuration = JSON.parse(file_content)
    return configuration[key]
end

.reset_configObject

reset config file



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/asca/tools/configuration.rb', line 12

def reset_config
    # remove all
    FileUtils.rm_rf(ROOTDIR)
    
    # create root dir
    Dir.mkdir ROOTDIR
    
    # create cache dir
    Dir.mkdir CACHE_DIR
    
    # init config file
    File.open(JSONFILE, 'w') { |file|
        file.write("{}")
    }
end

.update_config(key, value) ⇒ Object

update config



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/asca/tools/configuration.rb', line 29

def update_config(key, value)
    if !File.exist?(JSONFILE)
        reset_config
    end
    file_content = File.read(JSONFILE)
    configuration = JSON.parse(file_content)
    if value
        configuration[key] = value
    else
        configuration.delete(key)
    end
    File.open(JSONFILE, 'w') { |file| 
        file.write(JSON.pretty_generate(configuration))
    }
    return 0
end