Skip to content

Instantly share code, notes, and snippets.

@stick
Created April 11, 2011 15:30
Show Gist options
  • Save stick/913705 to your computer and use it in GitHub Desktop.
Save stick/913705 to your computer and use it in GitHub Desktop.
ugly script so var
#!/usr/bin/env ruby
begin
require 'rubygems'
rescue LoadError
end
# require 'YAML'
require 'yaml'
require 'pp'
require 'active_record'
# require 'lib/db'
require 'optparse'
#require 'config'
require 'ostruct'
ENVIRONMENT = ENV['SETEC_ENV'] || "production"
CmdlineOpts = OpenStruct.new
ARGV.options do |opts|
opts.banner = "Usage: #{File.basename($0)} [options]"
opts.separator ""
# start options
opts.on('--init', 'Initialize setec database') do |init|
CmdlineOpts.createdb = init
end
opts.on('--config[=OPTIONAL]', 'Configuration file', 'Default (/etc/setec/settings.conf)') do |config|
CmdlineOpts.config = config
end
opts.separator ""
opts.on_tail('--help', "Shows this help text.") do
$stderr.puts opts
exit
end
opts.parse!
end
unless CmdlineOpts.config
case ENVIRONMENT
when 'development'
CmdlineOpts.config = File.join("config", "settings.yml")
else
CmdlineOpts.config = File.join("/etc/setec", "setec.conf")
end
end
begin
confighash = YAML.load_file(CmdlineOpts.config)
ConfigOptions = OpenStruct.new(confighash)
require 'lib/db'
if CmdlineOpts.createdb
create_database()
exit
end
# rescue
# puts "Cannot load config: " + ConfigOptions.config
end
# options should be all stripped
# attrib_pairs = {}
# case ARGV.first
# when /add/, /[remove|delete]/, /[show|list]/
# action = ARGV.shift
# subaction = ARGV.shift
# ARGV.each do |pair|
# if pair =~ /=/
# (a, b) = pair.split('=')
# attrib_pairs[a] = b
# else
# puts "data pair missing value..." + pair
# end
# end
# else
# puts ARGV.first
# end
#
User.create(
:username => "cmacleod",
:firstname => "Chris",
:lastname => "MacLeod",
:email => "cmacleod@airdat.com",
:public_key => "public key"
)
pp User.all
dbconfig = File.join(ConfigOptions.config_dir, ConfigOptions.database_config)
ActiveRecord::Base.configurations = YAML::load(File.open(dbconfig))
ActiveRecord::Base.establish_connection(ENVIRONMENT)
# logging
logfile = File.join(ConfigOptions.log_dir, ConfigOptions.log_file)
ActiveRecord::Base.logger = Logger.new(File.open(logfile, 'a'))
ActiveRecord::Base.colorize_logging = false
def create_database()
puts "Initializing database"
require 'db/schema'
end
class User < ActiveRecord::Base
has_and_belongs_to_many :group
has_many :password, :through => :group
validates_uniqueness_of :name
end
class Group < ActiveRecord::Base
has_and_belongs_to_many :user
has_many :password
validates_uniqueness_of :name
end
class Password < ActiveRecord::Base
belongs_to :group
validates_presence_of :title, :password, :user_name
validates_confirmation_of :password, :on => :create or :update
end
# create schema
ActiveRecord::Schema.define(:version => 1) do
create_table :users, :force => true do |t|
t.string :name, :null => false
t.string :first_name
t.string :last_name
t.string :email, :null => false
t.text :public_key, :null => false
t.text :private_key
t.timestamps
end
add_index :users, :name, :unique
create_table :groups, :force => true do |t|
t.string :name, :null => false
t.timestamps
end
add_index :groups, :name, :unique
create_table :passwords, :force => true do |t|
t.string :title, :null => false
t.text :password, :null => false
t.string :user_name
t.datetime :expires
t.timestamps
end
add_index :passwords, :title, :unique
create_table :users_groups, :id => false, :force => true do |t|
t.integer :user_id, :null => false
t.integer :group_id, :null => false
end
create_table :passwords_groups, :id => false, :force => true do |t|
t.integer :password_id, :null => false
t.integer :group_id, :null => false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment