#!/usr/bin/env ruby $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift(File.dirname(__FILE__)) require 'gritano' require 'etc' def help puts " gritano [install|user|repo] [command] parameters Examples: gritano install gritano update gritano user add username gritano user rm username gritano user addkey username keyname < key.pub gritano user rmkey username keyname gritano user addadmin username gritano user rmadmin username gritano user list gritano user keys username gritano user repos username gritano repo add reponame.git gritano repo rm reponame.git gritano repo addread reponame.git username gritano repo addwrite reponame.git username gritano repo rmread reponame.git username gritano repo rmwrite reponame.git username gritano repo list gritano repo users reponame.git -- v#{File.open(File.join(File.dirname(__FILE__), '..', 'VERSION')).readlines.join}" end def migrate ActiveRecord::Base.establish_connection(YAML::load(File.open(File.join(Etc.getpwuid.dir, '.gritano', 'database.yml')))) ActiveRecord::Migrator.migrate(File.join(File.dirname(__FILE__),'../db/migrate'), ENV["VERSION"] ? ENV["VERSION"].to_i : nil ) end def install Dir.mkdir(File.join(Etc.getpwuid.dir, '.gritano')) unless File.exist?(File.join(Etc.getpwuid.dir, '.gritano')) Dir.mkdir(File.join(Etc.getpwuid.dir, '.ssh')) unless File.exist?(File.join(Etc.getpwuid.dir, '.ssh')) File.open(File.join(Etc.getpwuid.dir, '.gritano', 'database.yml'), "w") do |f| f.write("adapter: sqlite3\ndatabase: #{File.join(Etc.getpwuid.dir, '.gritano', 'database.db')}\n") end FileUtils.rm(File.join(Etc.getpwuid.dir, '.gritano', 'database.db')) if File.exist?(File.join(Etc.getpwuid.dir, '.gritano', 'database.db')) migrate end def version puts "v#{File.open(File.join(File.dirname(__FILE__),'../VERSION')).readlines.join}" end def check_git if `which git` == "" puts "Error: git must be installed on the local system" exit end end def check_gritano unless File.exist?(File.join(Etc.getpwuid.dir, '.gritano')) puts "Error: First run 'gritano install'" exit end end check_git if ARGV.length == 1 if ARGV[0] =='install' install elsif ARGV[0] =='update' migrate elsif ARGV[0] =='version' version else help end elsif ARGV.length == 0 help else check_gritano ActiveRecord::Base.establish_connection(YAML::load(File.open(File.join(Etc.getpwuid.dir, '.gritano', 'database.yml')))) console = Gritano::Console.new(STDIN) console.repo_path = Etc.getpwuid.dir console.ssh_path = File.join(Etc.getpwuid.dir, '.ssh') begin cmd_return = console.execute(ARGV) if cmd_return[0] puts "#{cmd_return[1]}" else puts "error: #{cmd_return[1]}" end rescue NoMethodError puts help end end