Class: Morpheus::Cli::ConfigFile
- Inherits:
-
Object
- Object
- Morpheus::Cli::ConfigFile
- Includes:
- Term::ANSIColor
- Defined in:
- lib/morpheus/cli/config_file.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(fn) ⇒ ConfigFile
constructor
A new instance of ConfigFile.
- #load_file ⇒ Object
-
#reload_file ⇒ Object
this will load any local changes, and then resave everything in the config (shell).
- #save_file ⇒ Object
Constructor Details
#initialize(fn) ⇒ ConfigFile
Returns a new instance of ConfigFile.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/morpheus/cli/config_file.rb', line 24 def initialize(fn) @config = {} # only create the file if we're using the default, otherwise error if fn @filename = File.(fn) else @filename = File.(Morpheus::Cli.config_filename) if !Dir.exists?(File.dirname(@filename)) FileUtils.mkdir_p(File.dirname(@filename)) end if !File.exists?(@filename) print "#{Term::ANSIColor.dark}Initializing default config file#{Term::ANSIColor.reset}\n" if Morpheus::Logging.debug? FileUtils.touch(@filename) save_file() end end load_file() end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
22 23 24 |
# File 'lib/morpheus/cli/config_file.rb', line 22 def config @config end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
21 22 23 |
# File 'lib/morpheus/cli/config_file.rb', line 21 def filename @filename end |
Class Method Details
.init(filename = nil) ⇒ Object
10 11 12 |
# File 'lib/morpheus/cli/config_file.rb', line 10 def init(filename=nil) @instance ||= Morpheus::Cli::ConfigFile.new(filename) end |
.instance ⇒ Object
14 15 16 17 |
# File 'lib/morpheus/cli/config_file.rb', line 14 def instance #@instance ||= init(Morpheus::Cli.config_filename) @instance or raise "#{self}.init() must be called!" end |
Instance Method Details
#load_file ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/morpheus/cli/config_file.rb', line 43 def load_file #puts "loading config #{@filename}" @config = {} if !@filename return false end if !File.exist?(@filename) raise "Morpheus cli config file not found: #{@filename}" end file_contents = File.read(@filename) file_contents.split config_text = File.open(@filename).read config_lines = config_text.split(/\n/) #config_lines = config_lines.reject {|line| line =~ /^\#/} # strip comments config_lines.each_with_index do |line, line_index| line_num = line_index + 1 line = line.strip #puts "parsing config line #{line_num} : #{line}" next if line.empty? next if line =~ /^\#/ # skip comments if line =~ /^alias\s+/ alias_name, command_string = Morpheus::Cli::CliRegistry.parse_alias_definition(line) if alias_name.empty? || command_string.empty? print "#{dark} #=> bad config line #{line_num} invalid alias definition: #{line}\n" if Morpheus::Logging.debug? else # @config[:aliases] ||= [] # @config[:aliases] << {name: alias_name, command: command_string} begin Morpheus::Cli::CliRegistry.instance.add_alias(alias_name, command_string) rescue => err print "#{dark} #=> #{err.}#{reset}\n" if Morpheus::Logging.debug? end end elsif line =~ /^colorize/ Term::ANSIColor::coloring = true elsif line =~ /^uncolorize/ Term::ANSIColor::coloring = false # what else shall we make configurable? else print "#{dark} #=> config line #{line_num} unrecognized: #{line}#{reset}\n" if Morpheus::Logging.debug? end end # if @config[:aliases] # @config[:aliases].each do |it| # Morpheus::Cli::CliRegistry.instance.add_alias(it[:name], it[:command]) # end # end #puts "done loading config from #{}" return @config end |
#reload_file ⇒ Object
this will load any local changes, and then resave everything in the config (shell)
121 122 123 124 |
# File 'lib/morpheus/cli/config_file.rb', line 121 def reload_file load_file save_file end |
#save_file ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/morpheus/cli/config_file.rb', line 98 def save_file if !@filename print "#{Term::ANSIColor.dark}Skipping config file save because filename has not been set#{Term::ANSIColor.reset}\n" if Morpheus::Logging.debug? return false end print "#{dark} #=> Saving config file #{@filename}#{reset}\n" if Morpheus::Logging.debug? out = "" out << "# .morpheusrc file #{@filename}" out << "\n" out << "# Auto-generated by morpheus #{Morpheus::Cli::VERSION} on #{Time.now.iso8601}" out << "\n\n" out << "# aliases" out << "\n" Morpheus::Cli::CliRegistry.instance.all_aliases.each do |k, v| out << "alias #{k}='#{v}'" out << "\n" end out << "\n" File.open(@filename, 'w') {|f| f.write(out) } return true end |