Sha256: 3f40d77a37980ccf64fcc8e18bb5545a81eb990ab85ca013a888ed2aca08daf5

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

require 'topicz/defaults'
require 'fileutils'
require 'optparse'
require 'yaml'

module Topicz::Commands

  class InitCommand

    def initialize(config_file = nil, arguments = [])
      @config_file = Topicz::DEFAULT_CONFIG_LOCATION
      option_parser.order! arguments
      unless arguments.empty?
        @directory = arguments.shift
      end
    end

    def option_parser
      OptionParser.new do |options|
        options.banner = 'Usage: init [options] <directory>'
        options.on('-c', '--config FILE') do |file|
          @config_file = file
        end
        options.separator ''
        options.separator 'Initializes a new topic repository. This creates a directory and writes its
location into a configuration file.'
        options.separator ''
        options.separator 'If the directory already exists, this command fails.'
        options.separator ''
        options.separator 'If a configuration file already exists, it will not be overwritten.'
      end
    end

    def execute
      unless @directory
        raise 'Pass the location of the new repository as an argument.'
      end
      if File.exist? @directory
        raise "A file or directory already exists at this location: #{@directory}."
      end
      create_repository
      create_configuration
    end

    def create_repository
      FileUtils.mkdir_p(@directory)
      puts "New topic repository created at: #{@directory}."
    end

    def create_configuration
      if File.exist? @config_file
        puts "Skipping creation of configuration file; one already exists at #{@config_file}."
      else
        File.open(@config_file, 'w') do |file|
          file.write(YAML.dump({'repository' => @directory}))
        end
        puts "Configuration file saved to: #{@config_file}."
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
topicz-0.3.0 lib/topicz/commands/init_command.rb
topicz-0.2.0 lib/topicz/commands/init_command.rb
topicz-0.1.1 lib/topicz/commands/init_command.rb
topicz-0.1.0 lib/topicz/commands/init_command.rb