Sha256: 62844807eed38d62868fe0d66c7f6be2cf31637cbc98f905c4c18a134c99e934

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

require_relative 'repository_command'
require 'topicz/defaults'
require 'json'
require 'zaru'
require 'fileutils'

module Topicz::Commands

  class CreateCommand < RepositoryCommand

    def initialize(config_file = nil, arguments = [])
      super(config_file)
      @title = arguments.join(' ').strip
    end

    def option_parser
      OptionParser.new do |options|
        options.banner = 'Usage: create <name>'
        options.separator ''
        options.separator 'Creates a new topic with the specified name'
        options.separator ''
        options.separator 'Basically all this command does is create a new directory structure to hold a topic.'
        options.separator ''
        options.separator 'It is an error if a topic with the specified name already exists.'
      end
    end

    def execute
      if @repository.exist_title?(@title)
        raise "Topic already exists: '#{@title}'."
      end
      path = Zaru.sanitize! @title
      fullpath = File.join(@repository.root, path)
      if File.exist?(fullpath)
        raise "Directory already exists: '#{path}'."
      end
      FileUtils.mkdir fullpath
      Topicz::DIRECTORIES.each { |dir| FileUtils.mkdir(File.join(fullpath, dir)) }
      if path != @title
        File.open(File.join(fullpath, Topicz::TOPIC_FILE), 'w') do | file |
          file.write(YAML.dump({'title' => @title}))
        end
      end
      puts "Created topic '#{@title}'"
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

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