Sha256: 7a2aa8f9437926c599871d8512a63f62606a411e02fabf86c15ae055a1ab56b5

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

require 'fileutils'

module Proviso::Command
  class Config < Base

    def list
      display "List Config"
      if File.exists?(yaml_file)
        display open(yaml_file, 'r').read
      else
        error "config does not exists run config:create"
      end
    end
        
    
    def add
      if @args.length == 3
        modify_config_file
        display "successfully added config setting"
      else
        error "section, key and value pair required. eg. config:add [section] [key] [value]"
      end
    end
    
    def update
      if @args.length == 3
        modify_config_file
        display "successfully updated config setting"
      else
        error "section, key and value pair required. eg. config:update [section] [key] [value]"
      end
    end
    
    def create
      data = { "config" => { "hello" => 'world'}}
      if @args.length == 1
        data = YAML.load_file(yaml_file) if File.exists?(yaml_file)
        data.merge!(@args.first => {})
      else
        FileUtils.mkdir_p(File.join(home_directory, PROVISO_PATH))
      end
      open(yaml_file, 'w') { |f| f.write data.to_yaml }  
      display "created config file"
    end
    
  private
        
    def modify_config_file
      config_hash = YAML.load_file(yaml_file)
      config_hash[@args[0]].merge!({ @args[1] => @args[2] })
      open(yaml_file, 'w') { |f| f.write config_hash.to_yaml }      
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
proviso-0.2.0 lib/proviso/commands/config.rb
proviso-0.2.0.beta4 lib/proviso/commands/config.rb