pit 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (8) hide show
  1. data/ChangeLog +4 -0
  2. data/README +47 -0
  3. data/Rakefile +136 -0
  4. data/bin/pit +123 -0
  5. data/lib/pit.rb +72 -0
  6. data/test/pit_test.rb +60 -0
  7. data/test/test_helper.rb +3 -0
  8. metadata +71 -0
@@ -0,0 +1,4 @@
1
+ == 0.0.1 / 2008-01-02
2
+
3
+ * initial release
4
+
data/README ADDED
@@ -0,0 +1,47 @@
1
+
2
+ = pit
3
+
4
+
5
+ == Description
6
+
7
+ pit is account management tool.
8
+
9
+
10
+ == Installation
11
+
12
+ === Archive Installation
13
+
14
+ rake install
15
+
16
+ === Gem Installation
17
+
18
+ gem install pit
19
+
20
+
21
+ == Features/Problems
22
+
23
+
24
+ == Synopsis
25
+
26
+ command:
27
+
28
+ $ pit set twitter.com
29
+ open 'twitter.com' config with $EDITOR"
30
+
31
+ $ pit get twitter.com | lv
32
+ get config of 'twitter.com' by YAML.
33
+
34
+ $ pit switch dev
35
+ switch profile to 'dev'
36
+
37
+ ruby lib.
38
+
39
+ require "pit"
40
+
41
+ Pit.get("foo")
42
+
43
+ == Copyright
44
+
45
+ Author:: cho45 <cho45@lowreal.net>
46
+ Copyright:: Copyright (c) 2008 cho45
47
+ License:: Creative Commons by
@@ -0,0 +1,136 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'rake/testtask'
5
+ require 'rake/packagetask'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/rdoctask'
8
+ require 'rake/contrib/rubyforgepublisher'
9
+ require 'rake/contrib/sshpublisher'
10
+ require 'fileutils'
11
+ include FileUtils
12
+
13
+ NAME = "pit"
14
+ AUTHOR = "cho45"
15
+ EMAIL = "cho45@lowreal.net"
16
+ DESCRIPTION = "pit: account management tool"
17
+ RUBYFORGE_PROJECT = "lowreal"
18
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
19
+ BIN_FILES = %w( pit )
20
+ VERS = "0.0.1"
21
+
22
+
23
+ REV = File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
24
+ CLEAN.include ['**/.*.sw?', '*.gem', '.config']
25
+ RDOC_OPTS = [
26
+ '--title', "#{NAME} documentation",
27
+ "--charset", "utf-8",
28
+ "--opname", "index.html",
29
+ "--line-numbers",
30
+ "--main", "README",
31
+ "--inline-source",
32
+ ]
33
+
34
+ task :default => [:test]
35
+ task :package => [:clean]
36
+
37
+ Rake::TestTask.new("test") do |t|
38
+ t.libs << "test"
39
+ t.pattern = "test/**/*_test.rb"
40
+ t.verbose = true
41
+ end
42
+
43
+ spec = Gem::Specification.new do |s|
44
+ s.name = NAME
45
+ s.version = VERS
46
+ s.platform = Gem::Platform::RUBY
47
+ s.has_rdoc = true
48
+ s.extra_rdoc_files = ["README", "ChangeLog"]
49
+ s.rdoc_options += RDOC_OPTS + ['--exclude', '^(examples|extras)/']
50
+ s.summary = DESCRIPTION
51
+ s.description = DESCRIPTION
52
+ s.author = AUTHOR
53
+ s.email = EMAIL
54
+ s.homepage = HOMEPATH
55
+ s.executables = BIN_FILES
56
+ s.rubyforge_project = RUBYFORGE_PROJECT
57
+ s.bindir = "bin"
58
+ s.require_path = "lib"
59
+ s.autorequire = "pit"
60
+ s.test_files = Dir["test/test_*.rb"]
61
+
62
+ #s.add_dependency('activesupport', '>=1.3.1')
63
+ #s.required_ruby_version = '>= 1.8.2'
64
+
65
+ s.files = %w(README ChangeLog Rakefile) +
66
+ Dir.glob("{bin,doc,test,lib,templates,generator,extras,website,script}/**/*") +
67
+ Dir.glob("ext/**/*.{h,c,rb}") +
68
+ Dir.glob("examples/**/*.rb") +
69
+ Dir.glob("tools/*.rb")
70
+
71
+ s.extensions = FileList["ext/**/extconf.rb"].to_a
72
+ end
73
+
74
+ Rake::GemPackageTask.new(spec) do |p|
75
+ p.need_tar = true
76
+ p.gem_spec = spec
77
+ end
78
+
79
+ task :install do
80
+ name = "#{NAME}-#{VERS}.gem"
81
+ sh %{rake package}
82
+ sh %{sudo gem install pkg/#{name}}
83
+ end
84
+
85
+ task :uninstall => [:clean] do
86
+ sh %{sudo gem uninstall #{NAME}}
87
+ end
88
+
89
+
90
+ Rake::RDocTask.new do |rdoc|
91
+ rdoc.rdoc_dir = 'html'
92
+ rdoc.options += RDOC_OPTS
93
+ rdoc.template = "resh"
94
+ #rdoc.template = "#{ENV['template']}.rb" if ENV['template']
95
+ if ENV['DOC_FILES']
96
+ rdoc.rdoc_files.include(ENV['DOC_FILES'].split(/,\s*/))
97
+ else
98
+ rdoc.rdoc_files.include('README', 'ChangeLog')
99
+ rdoc.rdoc_files.include('lib/**/*.rb')
100
+ rdoc.rdoc_files.include('ext/**/*.c')
101
+ end
102
+ end
103
+
104
+ desc "Publish to RubyForge"
105
+ task :rubyforge => [:rdoc, :package] do
106
+ require 'rubyforge'
107
+ @local_dir = "html"
108
+ @host = "cho45@rubyforge.org"
109
+ @remote_dir = "/var/www/gforge-projects/#{RUBYFORGE_PROJECT}/#{NAME}"
110
+ sh %{rsync -r --delete --verbose #{@local_dir}/ #{@host}:#{@remote_dir}}
111
+ end
112
+
113
+ desc 'Package and upload the release to rubyforge.'
114
+ task :release => [:clean, :package] do |t|
115
+ require 'rubyforge'
116
+ v = ENV["VERSION"] or abort "Must supply VERSION=x.y.z"
117
+ abort "Versions don't match #{v} vs #{VERS}" unless v == VERS
118
+ pkg = "pkg/#{NAME}-#{VERS}"
119
+
120
+ rf = RubyForge.new
121
+ puts "Logging in"
122
+ rf.login
123
+
124
+ c = rf.userconfig
125
+ # c["release_notes"] = description if description
126
+ # c["release_changes"] = changes if changes
127
+ c["preformatted"] = true
128
+
129
+ files = [
130
+ "#{pkg}.tgz",
131
+ "#{pkg}.gem"
132
+ ].compact
133
+
134
+ puts "Releasing #{NAME} v. #{VERS}"
135
+ rf.add_release RUBYFORGE_PROJECT, NAME, VERS, *files
136
+ end
data/bin/pit ADDED
@@ -0,0 +1,123 @@
1
+ #!/usr/bin/env ruby
2
+ # vim:ft=ruby:
3
+ # ruby -Ilib bin/pit
4
+
5
+ require "optparse"
6
+ require "pathname"
7
+ require "pit"
8
+
9
+ class PitCommand
10
+ VERSION = "0.0.1"
11
+ NAME = "pit"
12
+
13
+ def self.run(argv)
14
+ new(argv.dup).run
15
+ end
16
+
17
+ def initialize(argv)
18
+ @argv = argv
19
+
20
+ @subparsers = {
21
+ "help" => OptionParser.new { |opts|
22
+ opts.banner = <<-EOB.gsub(/^\t+/, "")
23
+ Usage: #{NAME} help <subcommand>
24
+
25
+ Show help of subcommand.
26
+ EOB
27
+ },
28
+
29
+ "set" => OptionParser.new { |opts|
30
+ opts.banner = <<-EOB.gsub(/^\t+/, "")
31
+ Usage: #{NAME} set <name>
32
+
33
+ Config values of name with $EDITOR.
34
+ EOB
35
+ },
36
+
37
+ "get" => OptionParser.new { |opts|
38
+ opts.banner = <<-EOB.gsub(/^\t+/, "")
39
+ Usage: #{NAME} get <name>
40
+
41
+ Get values of <name>.
42
+ EOB
43
+ },
44
+
45
+ "switch" => OptionParser.new { |opts|
46
+ opts.banner = <<-EOB.gsub(/^\t+/, "")
47
+ Usage: #{NAME} switch <profile>
48
+
49
+ Switch profile to <profile>.
50
+ EOB
51
+ },
52
+ }
53
+
54
+ @parser = OptionParser.new do |parser|
55
+ parser.banner = <<-EOB.gsub(/^\t+/, "")
56
+ Usage: #{NAME} <subcommand> <args>
57
+
58
+ EOB
59
+
60
+ parser.separator ""
61
+
62
+ parser.separator "Subcommands:"
63
+ @subparsers.keys.sort.each do |k|
64
+ parser.separator "#{parser.summary_indent} #{k}"
65
+ end
66
+
67
+ parser.separator ""
68
+
69
+ parser.separator "Options:"
70
+ parser.on('--version', "Show version string `#{VERSION}'") do
71
+ puts VERSION
72
+ exit
73
+ end
74
+ end
75
+ end
76
+
77
+ def run
78
+ @parser.order!(@argv)
79
+ if @argv.empty?
80
+ puts @parser.help
81
+ exit
82
+ else
83
+ @subcommand = @argv.shift
84
+ method_name = "cmd_#{@subcommand}"
85
+ if self.respond_to?(method_name)
86
+ @subparsers[@subcommand].parse!(@argv)
87
+ self.send(method_name)
88
+ else
89
+ puts "Not implemented subcommand: `#{@subcommand}'."
90
+ puts
91
+ puts @parser.help
92
+ end
93
+ end
94
+ end
95
+
96
+ def cmd_get
97
+ name, = @argv
98
+ exit if name.nil?
99
+ if $stdout.tty?
100
+ $stderr.puts "do not output to tty."
101
+ else
102
+ puts Pit.get(name).to_yaml
103
+ end
104
+ end
105
+
106
+ def cmd_set
107
+ name, = @argv
108
+ exit if name.nil?
109
+ Pit.set(name)
110
+ end
111
+
112
+ def cmd_switch
113
+ profile, = @argv
114
+ profile = "default" if profile.nil?
115
+ profile.gsub(/[^a-z0-9.-]/i, "")
116
+ Pit.switch(profile)
117
+ $stderr.puts "Switch profile to #{profile}"
118
+ end
119
+ end
120
+
121
+ PitCommand.run(ARGV)
122
+
123
+
@@ -0,0 +1,72 @@
1
+
2
+ require "yaml"
3
+ require "pathname"
4
+ require "tempfile"
5
+
6
+ module Pit
7
+ Directory = Pathname.new("~/.pit").expand_path
8
+ @@config = Directory + "pit.yaml"
9
+ @@profile = Directory + "default.yaml"
10
+
11
+ def self.set(name, opts={})
12
+ if opts.key?(:data)
13
+ result = opts[:data]
14
+ else
15
+ c = self.get(name).to_yaml
16
+ t = Tempfile.new("pit")
17
+ t << c
18
+ t.close
19
+ system(ENV["EDITOR"], t.path)
20
+ t.open
21
+ result = t.read
22
+ if result == c
23
+ $stderr.puts "No Changes"
24
+ return
25
+ end
26
+ result = YAML.load(result)
27
+ end
28
+ config = self.load
29
+ config[name] = result
30
+ @@profile.open("w") {|f| YAML.dump(config, f) }
31
+ nil
32
+ end
33
+
34
+ def self.get(name, opts={})
35
+ self.load[name] || {"username"=>"", "password"=>""}
36
+ end
37
+
38
+ def self.switch(name, opts={})
39
+ @@profile = Directory + "#{name}.yaml"
40
+ config = self.config
41
+ config["profile"] = name
42
+ @@config.open("w") {|f| f << config.to_yaml }
43
+ end
44
+
45
+ def self.load
46
+ Directory.mkpath unless Directory.exist?
47
+ unless @@config.exist?
48
+ @@config.open("w") {|f| f << {"profile"=>"default"}.to_yaml }
49
+ @@config.chmod(0600)
50
+ end
51
+ self.switch(self.config["profile"])
52
+ unless @@profile.exist?
53
+ @@profile.open("w") {|f| f << {}.to_yaml }
54
+ @@profile.chmod(0600)
55
+ end
56
+ YAML.load(@@profile.read) || {}
57
+ end
58
+
59
+ def self.config
60
+ YAML.load(@@config.read)
61
+ end
62
+ end
63
+
64
+
65
+ __END__
66
+ p Pit.set("test")
67
+ p Pit.get("test")
68
+
69
+ config = Pit.get("twitter")
70
+ p config["password"]
71
+ p config["username"]
72
+
@@ -0,0 +1,60 @@
1
+ require "tmpdir"
2
+ require "pathname"
3
+
4
+ class Pathname
5
+ @@tempname_number = 0
6
+ def self.tempname(base=$0, dir=Dir.tmpdir)
7
+ @@tempname_number += 1
8
+ name = "#{dir}/#{base}.#{$$}.#{@@tempname_number}"
9
+ path = new(name)
10
+ at_exit do
11
+ path.rmtree if path.exist?
12
+ end
13
+ path
14
+ end
15
+ end
16
+
17
+ dir = Pathname.tempname
18
+ dir.mkpath
19
+ ENV["HOME"] = dir
20
+
21
+ require File.dirname(__FILE__) + '/test_helper.rb'
22
+
23
+ require "test/unit"
24
+ class PitTest < Test::Unit::TestCase
25
+ def test_load
26
+ assert Pit
27
+ end
28
+
29
+ def test_set_get
30
+ Pit.set("test", :data => {"username"=>"foo","password"=>"bar"})
31
+ assert_equal "foo", Pit.get("test")["username"]
32
+ assert_equal "bar", Pit.get("test")["password"]
33
+
34
+ Pit.set("test2", :data => {"username"=>"foo2","password"=>"bar2"})
35
+ assert_equal "foo2", Pit.get("test2")["username"]
36
+ assert_equal "bar2", Pit.get("test2")["password"]
37
+
38
+ Pit.set("test", :data => {"username"=>"foo","password"=>"bar"})
39
+ assert_equal "foo", Pit.get("test")["username"]
40
+ assert_equal "bar", Pit.get("test")["password"]
41
+ end
42
+
43
+ def test_switch
44
+ Pit.switch("default")
45
+ Pit.set("test", :data => {"username"=>"foo","password"=>"bar"})
46
+ assert_equal "foo", Pit.get("test")["username"]
47
+ assert_equal "bar", Pit.get("test")["password"]
48
+
49
+ Pit.switch("profile2")
50
+ assert_equal "profile2", Pit.config["profile"]
51
+ Pit.set("test", :data => {"username"=>"foo2","password"=>"bar2"})
52
+ assert_equal "foo2", Pit.get("test")["username"]
53
+ assert_equal "bar2", Pit.get("test")["password"]
54
+
55
+ Pit.switch("default")
56
+ Pit.set("test", :data => {"username"=>"foo","password"=>"bar"})
57
+ assert_equal "foo", Pit.get("test")["username"]
58
+ assert_equal "bar", Pit.get("test")["password"]
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/pit'
3
+
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - cho45
8
+ autorequire: pit
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-01-02 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: "pit: account management tool"
17
+ email: cho45@lowreal.net
18
+ executables:
19
+ - pit
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - ChangeLog
25
+ files:
26
+ - README
27
+ - ChangeLog
28
+ - Rakefile
29
+ - bin/pit
30
+ - test/pit_test.rb
31
+ - test/test_helper.rb
32
+ - lib/pit.rb
33
+ has_rdoc: true
34
+ homepage: http://lowreal.rubyforge.org
35
+ post_install_message:
36
+ rdoc_options:
37
+ - --title
38
+ - pit documentation
39
+ - --charset
40
+ - utf-8
41
+ - --opname
42
+ - index.html
43
+ - --line-numbers
44
+ - --main
45
+ - README
46
+ - --inline-source
47
+ - --exclude
48
+ - ^(examples|extras)/
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project: lowreal
66
+ rubygems_version: 1.0.1
67
+ signing_key:
68
+ specification_version: 2
69
+ summary: "pit: account management tool"
70
+ test_files:
71
+ - test/test_helper.rb