Class: Qcmd::Configuration

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

Class Method Summary collapse

Class Method Details

.configObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/qcmd/configuration.rb', line 33

def config
  @config ||= begin
                if !File.exists?(config_file)
                  File.open(config_file, 'w') {|f|
                    default = JSON.pretty_generate({'aliases' => Qcmd::Aliases.defaults})
                    Qcmd.debug "[Configuration config] writing defaults: #{ default }"
                    f.write default
                  }
                end

                JSON.load(File.open(config_file))
              rescue
                Qcmd.log(:error, "Failed to load configuration, make sure #{ config_file } is valid JSON.\n\n")
                raise
              end
end

.config_fileObject

and the actual files



73
74
75
# File 'lib/qcmd/configuration.rb', line 73

def config_file
  File.join(home_directory, "settings.json")
end

.historyObject

not really config file things, but related to config & settings storage



63
64
65
# File 'lib/qcmd/configuration.rb', line 63

def history
  @history ||= open_file_for_appending(history_file)
end

.history_fileObject



77
78
79
# File 'lib/qcmd/configuration.rb', line 77

def history_file
  File.join(home_directory, "history.log")
end

.home_directoryObject



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

def home_directory
  @home_directory ||= begin
                        full_path = File.join(File.expand_path('~'), qcmd_directory)
                        begin
                          if !File.exists?(full_path)
                            FileUtils.mkdir_p(full_path)
                          end
                          full_path
                        rescue => ex
                          puts "Failed to create qcmd's home directory at #{ full_path }"
                          puts ex.message

                          exit 1
                        end
                      end
end

.logObject



67
68
69
# File 'lib/qcmd/configuration.rb', line 67

def log
  @log ||= open_file_for_appending(log_file)
end

.log_fileObject



81
82
83
# File 'lib/qcmd/configuration.rb', line 81

def log_file
  File.join(home_directory, "debug.log")
end

.open_file_for_appending(fname) ⇒ Object



27
28
29
30
31
# File 'lib/qcmd/configuration.rb', line 27

def open_file_for_appending(fname)
   f = File.new(fname, 'a')
   f.sync = true
   f
end

.qcmd_directoryObject



6
7
8
# File 'lib/qcmd/configuration.rb', line 6

def qcmd_directory
  ".qcmd"
end

.saveObject



55
56
57
58
59
# File 'lib/qcmd/configuration.rb', line 55

def save
  File.open(config_file, 'w') {|conf_file|
    conf_file.write(JSON.pretty_generate(config))
  }
end

.update(key, value) ⇒ Object



50
51
52
53
# File 'lib/qcmd/configuration.rb', line 50

def update key, value
  config[key] = value
  save
end