Class: Configure

Inherits:
Object
  • Object
show all
Includes:
CommandProc
Defined in:
lib/clui_config.rb

Constant Summary collapse

CONFIG_DIRECTORY =
".vdms.d"
CONFIG_FILENAME =
"config"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CommandProc

#help, included

Methods included from ConfigReader

#param

Constructor Details

#initialize(name) ⇒ Configure

Returns a new instance of Configure.



7
8
# File 'lib/clui_config.rb', line 7

def initialize(name)
end

Class Method Details

.config_parameter(name) ⇒ Object



86
87
88
89
90
91
# File 'lib/clui_config.rb', line 86

def self.config_parameter(name)
  instance = get_instance(nil)
  instance.load_config_values
  result = @@config_values[name]
  result = result ? result : ""
end

Instance Method Details

#file_contents(filename) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/clui_config.rb', line 46

def file_contents(filename)
  if (File.file? filename)
    File.open(filename, "r")
  else
    []
  end
end

#get(args) ⇒ Object



93
94
95
# File 'lib/clui_config.rb', line 93

def get(args)
  
end

#get_config_file ⇒ Object



13
14
15
16
17
18
# File 'lib/clui_config.rb', line 13

def get_config_file
  if (! File.directory?(CONFIG_DIRECTORY))
    FileUtils.mkdir(CONFIG_DIRECTORY)
  end
  CONFIG_DIRECTORY + File::SEPARATOR + CONFIG_FILENAME
end

#init(args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/clui_config.rb', line 54

def init(args)
  if (args.length != 2)
    puts "usage: init <account-num> <token>"
    puts "go to my.edgecast.com --> Settings to find it"
  else
    set(["acct-num", args[0]])
    set(["token", args[1]])
    set(["rest_base_url", "https://api.edgecast.com/v2/"])
  end
end

#load_config_values ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/clui_config.rb', line 65

def load_config_values
  if defined? @@config_values
    return
  else
    @@config_values = {}
    if (! File.file?(get_config_file))
      return
    end
    File.open(get_config_file).each { |line|
      line = line.strip
      if line[0] == '#'
        continue
      end
      name, value = parse_line(line)
      if value
        @@config_values[name] = value
      end
    }
  end
end

#parse_line(line) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/clui_config.rb', line 20

def parse_line(line)
  idx = line.index('=')
  if (idx)
    return line[0...idx], line[idx+1..-1]
  else
    line
  end
end

#replace_nv(name, value, new_file, old_file_contents) ⇒ Object



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

def replace_nv(name, value, new_file, old_file_contents)
  new_config_line = name + '=' + value
  wrote_line = false
  old_file_contents.each { |line|
    old_name, old_value = parse_line(line)
    if (old_name == name)
      new_file.puts(new_config_line)
      wrote_line = true
    else
      new_file.puts(line)
    end
  }
  if ( ! wrote_line)
    new_file.puts(new_config_line)
  end
end

#set(args) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/clui_config.rb', line 97

def set(args)
  if (args.length != 2)
    puts "usage: set <name> <value>"
    error
  end
  name = args[0]
  value = args[1]
  config_file = get_config_file
  new_config_file = config_file + ".tmp"
  updated_file = File.open(new_config_file, "w")
  replace_nv(name, value, updated_file, file_contents(config_file))
  updated_file.close
  FileUtils.mv(new_config_file, config_file, :force => true)
end