bin/jou

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
require 'methadone'
require_relative '../lib/jou.rb'
require_relative '../lib/jou/journal_config.rb'

class App
  include Methadone::Main

  main do
    self.use_config

    if options[:'set-perm'] && options[:'set-perm'] != JournalConfig.config[:journal_file]
      config_file = Dir.home + "/.jou"
      JournalConfig.configure_with(config_file)
      JournalConfig.config[:journal_file] = options[:'set-perm']
      JournalConfig.create(config_file)
    end

    if options[:'set-temp']
      JournalConfig.config[:journal_file] = options[:'set-temp']
    end

    if options[:'add-entry']
      self.add_entry(options[:'add-entry'])
    end

    if options.empty?
      self.print_recent
    end
  end

  def self.use_config
    config_file = Dir.home + "/.jou"
    JournalConfig.configure_with(config_file)
  end

  def self.print_recent
    j = Journal.new(JournalConfig.config[:journal_file])
    # Get current content if the file exists
    j.serialize if File.exist?(JournalConfig.config[:journal_file])

    puts j.get_recent(1)
  end

  def self.add_entry(string)
    j = Journal.new(JournalConfig.config[:journal_file])

    # Get current content if the file exists
    j.serialize if File.exist?(JournalConfig.config[:journal_file])

    j.add(string)
    puts "Adding entry to journal: #{string}"

    # Sort and save
    j.reverse_sort
    j.deserialize
  end

  description "Jou is a simple command line utility that helps with maintaining a journal file in markdown syntax."
  on("-a", "--add-entry VALUE", "Add an entry")
  on("-s", "--set-perm VALUE", "Set a new default file to write to and read from.")
  on("-t", "--set-temp VALUE", "Set a file to write to without overwriting the config file.")

  version Jou::VERSION

  go!
end