pitcgi 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3b3ae4bb6f13fd8b301344ee8e0c0576ed85b710
4
+ data.tar.gz: 7cbf3477c880f507cf35f87c880eaa2db5da91b5
5
+ SHA512:
6
+ metadata.gz: a8713bcdad19604cf3045de698f23ade2bc7e876cf1b9aa95f8dd9bf2ac980b5af244e3d5ce92189682c6afbd4cd680062e989bd6928c706e2ac7e3e2767ceac
7
+ data.tar.gz: 8eaa7118a37a8f7ee1eb7648b94b0ac6b43e36b9733c707f4caf5cbddbb8dde8f638ef9048c9c204b58ebd28c11bc83874481de469eeb33b61f4e208e8b228ba
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/ChangeLog ADDED
@@ -0,0 +1,6 @@
1
+ 2014-12-18 sanadan <jecy00@gmail.com>
2
+
3
+ * [release]:
4
+ pit 0.0.7を改造してcgi専用に修正。
5
+ コマンド名もpitcgiに修正。
6
+
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in text_bracket_expander.gemspec
4
+ gemspec
5
+
6
+ gem 'rake'
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # pitcgi
2
+
3
+
4
+ ## Description
5
+
6
+ pitcgi is account management tool for cgi.
7
+
8
+
9
+ ## Installation
10
+
11
+ ### Archive Installation
12
+
13
+ rake install
14
+
15
+
16
+ ### Gem Installation
17
+
18
+ gem install pitcgi
19
+
20
+
21
+ ### Setup
22
+
23
+ sudo mkdir /etc/pitcgi
24
+ sudo chgrp www-data /etc/pitcgi
25
+ sudo chmod 770 /etc/pitcgi
26
+
27
+
28
+ ## Features/Problems
29
+
30
+
31
+ ## Synopsis
32
+
33
+ command:
34
+
35
+ $ pitcgi set twitter.com
36
+ open 'twitter.com' config with $EDITOR.
37
+
38
+ $ pitcgi get twitter.com | lv
39
+ get config of 'twitter.com' by YAML.
40
+
41
+ $ pitcgi switch dev
42
+ switch profile to 'dev'
43
+
44
+ ruby lib.
45
+ ```
46
+ require "pitcgi"
47
+
48
+ config # Pitcgi.get("twitter.com", :require #> {
49
+ "username" #> "default value",
50
+ "password" #> "default value"
51
+ })
52
+
53
+ Pitcgi.get("vox.com", :require #> {
54
+ "username" #> "default value",
55
+ "password" #> "default value"
56
+ "nickname" #> "default value"
57
+ })
58
+ ```
59
+ Pitcgi.get open $EDITOR with `require` hash if the setting does not have
60
+ required keys.
61
+
62
+
63
+ ## Copyright
64
+
65
+ Author: sanadan <jecy00@gmail.com>
66
+ Copyright: Copyright (c) 2008 cho45
67
+ Copyright (c) 2014 sanadan
68
+ License: Ruby's
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "test"
8
+ t.test_files = FileList['test/*_test.rb']
9
+ end
data/bin/pitcgi ADDED
@@ -0,0 +1,137 @@
1
+ #!/usr/bin/env ruby
2
+ # vim:ft=ruby:
3
+ # ruby -Ilib bin/pitcgi
4
+
5
+ require "optparse"
6
+ require "pathname"
7
+ require "pitcgi"
8
+
9
+ class PitcgiCommand
10
+ VERSION = Pitcgi::VERSION
11
+ NAME = "pitcgi"
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
+ warn "do not output to tty."
101
+ else
102
+ puts Pitcgi.get(name).to_yaml
103
+ end
104
+ end
105
+
106
+ def cmd_set
107
+ name, = @argv
108
+ exit if name.nil?
109
+ Pitcgi.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
+ Pitcgi.switch(profile)
117
+ warn "Switch profile to #{profile}"
118
+ end
119
+
120
+ def cmd_help
121
+ subcommand, = @argv
122
+ if subcommand
123
+ if @subparsers.key? subcommand
124
+ puts @subparsers[subcommand].help
125
+ else
126
+ puts "No such subcommand `#{subcommand}'"
127
+ puts @parser.help
128
+ end
129
+ else
130
+ puts @parser.help
131
+ end
132
+ end
133
+ end
134
+
135
+ PitcgiCommand.run(ARGV)
136
+
137
+
data/lib/pitcgi.rb ADDED
@@ -0,0 +1,102 @@
1
+
2
+ require 'pitcgi/version'
3
+ require "yaml"
4
+ require "pathname"
5
+ require "tempfile"
6
+
7
+ module Pitcgi
8
+ Directory = Pathname.new("/etc/pitcgi").expand_path
9
+ @@config = Directory + "pitcgi.yaml"
10
+ @@profile = Directory + "default.yaml"
11
+
12
+ # Set _name_ setting to current profile.
13
+ # If not _opts_ specified, this opens $EDITOR with current profile setting.
14
+ # If `data` specified, this just sets it to current profile.
15
+ # If `config` specified, this opens $EDITOR with merged hash with specified hash and
16
+ # current profile.
17
+ def self.set(name, opts={})
18
+ profile = self.load
19
+ if opts.key?(:data)
20
+ result = opts[:data]
21
+ else
22
+ if ENV["EDITOR"].nil? || !$stdout.tty?
23
+ return {}
24
+ end
25
+ c = (opts[:config] || self.get(name)).to_yaml
26
+ t = Tempfile.new("pitcgi")
27
+ t << c
28
+ t.close
29
+ system(ENV["EDITOR"], t.path)
30
+ t.open
31
+ result = t.read
32
+ if result == c
33
+ warn "No Changes"
34
+ return profile[name]
35
+ end
36
+ result = YAML.load(result)
37
+ end
38
+ profile[name] = result
39
+ @@profile.open("w") {|f| YAML.dump(profile, f) }
40
+ result
41
+ end
42
+
43
+ # Get _name_ setting from current profile.
44
+ # If not _opts_ specified, this just returns setting from current profile.
45
+ # If _require_ specified, check keys on setting and open $EDITOR.
46
+ def self.get(name, opts={})
47
+ ret = self.load[name] || {}
48
+ if opts[:require]
49
+ unless opts[:require].keys.all? {|k| ret[k] }
50
+ ret = opts[:require].update(ret)
51
+ ret = self.set(name, :config => ret)
52
+ end
53
+ end
54
+ ret || {"username"=>"", "password"=>""}
55
+ end
56
+
57
+ # Switch to current profile to _name_.
58
+ # Profile is set of settings. You can switch some settings using profile.
59
+ def self.switch(name, opts={})
60
+ @@profile = Directory + "#{name}.yaml"
61
+ config = self.config
62
+ ret = config["profile"]
63
+ config["profile"] = name
64
+ @@config.open("w") {|f| f << config.to_yaml }
65
+ ret
66
+ end
67
+
68
+ protected
69
+ def self.load
70
+ unless Directory.exist?
71
+ Directory.mkpath
72
+ Directory.chmod 0770
73
+ Directory.chown(nil, 33) # www-data
74
+ end
75
+ unless @@config.exist?
76
+ @@config.open("w") {|f| f << {"profile"=>"default"}.to_yaml }
77
+ @@config.chmod(0660)
78
+ @@config.chown(nil, 33) # www-data
79
+ end
80
+ self.switch(self.config["profile"])
81
+ unless @@profile.exist?
82
+ @@profile.open("w") {|f| f << {}.to_yaml }
83
+ @@profile.chmod(0660)
84
+ @@profile.chown(nil, 33) # www-data
85
+ end
86
+ YAML.load(@@profile.read) || {}
87
+ end
88
+
89
+ def self.config
90
+ YAML.load(@@config.read)
91
+ end
92
+ end
93
+
94
+
95
+ __END__
96
+ p Pitcgi.set("test")
97
+ p Pitcgi.get("test")
98
+
99
+ config = Pitcgi.get("twitter")
100
+ p config["password"]
101
+ p config["username"]
102
+
@@ -0,0 +1,4 @@
1
+
2
+ module Pitcgi
3
+ VERSION = '0.0.1'
4
+ end
data/pitcgi.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pitcgi/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "pitcgi"
8
+ gem.version = Pitcgi::VERSION
9
+ gem.authors = ["sanadan"]
10
+ gem.email = ["jecy00@gmail.com"]
11
+ gem.description = %q|pitcgi: account management tool for cgi|
12
+ gem.summary = %q|pitcgi: account management tool for cgi|
13
+ gem.homepage = "https://github.com/sanadan/pitcgi"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,122 @@
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
+ path = new(dir) + "#{File.basename(base)}.#{$$}.#{@@tempname_number}"
9
+ at_exit do
10
+ path.rmtree if path.exist?
11
+ end
12
+ path
13
+ end
14
+ end
15
+
16
+ dir = Pathname.tempname
17
+ dir.mkpath
18
+ #ENV["HOME"] = dir.to_s
19
+
20
+ require File.dirname(__FILE__) + '/test_helper.rb'
21
+
22
+ require "test/unit"
23
+ class PitcgiTest < Test::Unit::TestCase
24
+ def test_load
25
+ assert Pitcgi
26
+ end
27
+
28
+ def test_set_get
29
+ Pitcgi.set("test", :data => {"username"=>"foo","password"=>"bar"})
30
+ assert_equal "foo", Pitcgi.get("test")["username"]
31
+ assert_equal "bar", Pitcgi.get("test")["password"]
32
+
33
+ Pitcgi.set("test2", :data => {"username"=>"foo2","password"=>"bar2"})
34
+ assert_equal "foo2", Pitcgi.get("test2")["username"]
35
+ assert_equal "bar2", Pitcgi.get("test2")["password"]
36
+
37
+ Pitcgi.set("test", :data => {"username"=>"foo3","password"=>"bar3"})
38
+ assert_equal "foo3", Pitcgi.get("test")["username"]
39
+ assert_equal "bar3", Pitcgi.get("test")["password"]
40
+
41
+ assert_equal "foo4", Pitcgi.set("test", :data => {"username"=>"foo4","password"=>"bar4"})["username"]
42
+
43
+ # Clear
44
+ Pitcgi.set("test", :data => {})
45
+ Pitcgi.set("test2", :data => {})
46
+ end
47
+
48
+ def test_editor
49
+ ENV["EDITOR"] = nil
50
+ assert_nothing_raised("When editor is not set.") do
51
+ Pitcgi.set("test")
52
+ end
53
+
54
+ tst = Pathname.tempname
55
+ exe = Pathname.tempname
56
+ exe.open("w") do |f|
57
+ f.puts <<-EOF.gsub(/^\t+/, "")
58
+ #!/usr/bin/env ruby
59
+
60
+ File.open(ENV["TEST_FILE"], "w") do |f|
61
+ f.puts ARGF.read
62
+ end
63
+ EOF
64
+ end
65
+ exe.chmod(0700)
66
+
67
+ ENV["TEST_FILE"] = tst.to_s
68
+ ENV["EDITOR"] = exe.to_s
69
+ Pitcgi.set("test")
70
+
71
+ assert_nothing_raised do
72
+ assert_equal({}, YAML.load_file(tst.to_s))
73
+ end
74
+
75
+ data = {
76
+ "foo" => "0101",
77
+ "bar" => "0202",
78
+ }
79
+
80
+ Pitcgi.set("test", :data => data)
81
+ Pitcgi.set("test")
82
+
83
+ assert_nothing_raised do
84
+ assert_equal(data, YAML.load_file(tst.to_s))
85
+ end
86
+
87
+ # clear
88
+ Pitcgi.set("test", :data => {})
89
+ tst.open("w") {|f| }
90
+
91
+ Pitcgi.get("test", :require => data)
92
+
93
+ assert_nothing_raised do
94
+ assert_equal(data, YAML.load_file(tst.to_s))
95
+ end
96
+ end
97
+
98
+ def test_switch
99
+ Pitcgi.switch("default")
100
+ Pitcgi.set("test", :data => {"username"=>"foo5","password"=>"bar5"})
101
+ assert_equal "foo5", Pitcgi.get("test")["username"]
102
+ assert_equal "bar5", Pitcgi.get("test")["password"]
103
+
104
+ r = Pitcgi.switch("profile2")
105
+ assert_equal "default", r
106
+ assert_equal "profile2", Pitcgi.config["profile"]
107
+ Pitcgi.set("test", :data => {"username"=>"foo2","password"=>"bar2"})
108
+ assert_equal "foo2", Pitcgi.get("test")["username"]
109
+ assert_equal "bar2", Pitcgi.get("test")["password"]
110
+
111
+ # Clear
112
+ Pitcgi.set("test", :data => {})
113
+
114
+ Pitcgi.switch("default")
115
+ Pitcgi.set("test", :data => {"username"=>"foo6","password"=>"bar6"})
116
+ assert_equal "foo6", Pitcgi.get("test")["username"]
117
+ assert_equal "bar6", Pitcgi.get("test")["password"]
118
+
119
+ # Clear
120
+ Pitcgi.set("test", :data => {})
121
+ end
122
+ end
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/pitcgi'
3
+
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pitcgi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - sanadan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'pitcgi: account management tool for cgi'
14
+ email:
15
+ - jecy00@gmail.com
16
+ executables:
17
+ - pitcgi
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ChangeLog
23
+ - Gemfile
24
+ - README.md
25
+ - Rakefile
26
+ - bin/pitcgi
27
+ - lib/pitcgi.rb
28
+ - lib/pitcgi/version.rb
29
+ - pitcgi.gemspec
30
+ - test/pitcgi_test.rb
31
+ - test/test_helper.rb
32
+ homepage: https://github.com/sanadan/pitcgi
33
+ licenses: []
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 2.2.2
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: 'pitcgi: account management tool for cgi'
55
+ test_files:
56
+ - test/pitcgi_test.rb
57
+ - test/test_helper.rb