Sha256: 285bdd029d38427c65f83c0f86522655826924ac7aead2285a51dacefc35ac97

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

require 'yaml'
require 'ostruct'
require 'git'

module Gync
  extend self
  class Error < Exception; end

  def print_help
    puts "HELP MESSAGE"
  end
  
  def run(command)
    @opts = opts_for command
    git_pull
    system command
    git_push
  rescue Exception => err
    STDERR.puts err
    raise err
    exit 1
  end

  def home_dir
    ENV['HOME']
  end

  def config_file
    File.join(home_dir, '.gync.yml')
  end

  def git_pull
    git = Git.open(@opts.local)
    git.fetch
    git.merge 'origin/master'
  end

  def git_push
    git = Git.open(@opts.local)
    changed_files = git.status.changed.keys
    git.add changed_files
    git.commit "Gync commit"
    git.push "origin", "master"
  end

  def opts_for(command)
    opts = YAML.load_file(config_file)
    # validate
    result = opts[command] || raise("There is no `#{command}` section #{config_file}")
    result['local'] || raise("There is no `#{command}.local` section #{config_file}")
    result['remote'] || raise("There is no `#{command}.remote` section #{config_file}")
    OpenStruct.new(result)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gync-0.0.2 lib/gync.rb
gync-0.0.1 lib/gync.rb
gync-0.0.0 lib/gync.rb