sugoi_aliases_updator 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: f022789faf53fa6a431bb0501d170606be5abb3b
4
+ data.tar.gz: 2a64a3f176fa325ee6395c95e79bb97ed5f28775
5
+ SHA512:
6
+ metadata.gz: fda2dd6648ac1c19a41372dea4a26acaabe7170a1a98fb93445da06e574b0838f5ed86d89aa7e87c2182a825c4d87292e066bc6c5a76bdb7e4709217844d771e
7
+ data.tar.gz: cc878a77f4af31b96c407c659eeed23f3d5730ac2a2c01afdf2867a3057c224d5e43f484f788aa185a2d8888cb0b47bbba29a273207da91a1d877bd24fa53dd8
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *~
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sugoi_aliases_updator.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 yamada
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # SugoiAliasesUpdator
2
+
3
+ /etc/aliasesなファイルを読み込んで追加削除した結果を標準出力に出す。
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ gem 'sugoi_aliases_updator'
9
+ ```
10
+
11
+ ## Requirement
12
+ 'ruby' ~> '2.1'
13
+
14
+ ## Usage
15
+ ```
16
+ $ cat /etc/aliases
17
+ bin: root
18
+ daemon: root
19
+ named: root
20
+ nobody: root
21
+ uucp: root
22
+ www: root, n905i.1214@gmail.com
23
+ ftp-bugs: root
24
+ postfix: root, n905i.1214@gmail.com
25
+
26
+ ```
27
+
28
+ ```
29
+ $ sugoi_aliases_updator /etc/aliases add test@exmple.net TO=www
30
+ bin: root
31
+ daemon: root
32
+ named: root
33
+ nobody: root
34
+ uucp: root
35
+ www: root, n905i.1214@gmail.com, test@exmple.net
36
+ ftp-bugs: root
37
+ postfix: root, n905i.1214@gmail.com
38
+ ```
39
+
40
+ ```
41
+ $ sugoi_aliases_updator /etc/aliases rm n905i.1214@gmail.com TO=www,postfix
42
+ bin: root
43
+ daemon: root
44
+ named: root
45
+ nobody: root
46
+ uucp: root
47
+ www: root
48
+ ftp-bugs: root
49
+ postfix: root
50
+ ```
51
+ ```
52
+ $ sugoi_aliases_updator /etc/aliases list n905i.1214@gmail.com
53
+ www,postfix
54
+ ```
55
+
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it ( https://github.com/[my-github-username]/sugoi_aliases_updator/fork )
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ COMMANDS = %w(add rm list)
4
+
5
+ filepath = ARGV[0] || ''
6
+ command = ARGV[1]
7
+ target_email = ARGV[2]
8
+ direction_labels = ARGV[3]
9
+
10
+ if $*.first == '--version'
11
+ require "sugoi_aliases_updator/version"
12
+ puts SugoiAliasesUpdator::VERSION
13
+ exit 0
14
+ end
15
+
16
+ unless File.exists?(filepath)
17
+ puts "file is not found "
18
+ exit 1
19
+ end
20
+
21
+ unless COMMANDS.include?(command)
22
+ puts "input (#{COMMANDS.join("|")})"
23
+ exit 1
24
+ end
25
+
26
+ unless /\w*@.*/ =~ target_email
27
+ puts "input email"
28
+ exit 1
29
+ end
30
+
31
+ if %w(add rm).include?(command) && direction_labels.nil?
32
+ need = { 'add' => 'TO', 'rm' => 'FROM' }
33
+ puts "Usage: #{need[command]}=label1,label2"
34
+ exit 1
35
+ end
36
+
37
+ require "sugoi_aliases_updator"
38
+ include SugoiAliasesUpdator
39
+ aliases_parser = AliasesParser.new(filepath)
40
+
41
+ case command
42
+ when 'add'
43
+ labels = AliasesParser.parse_direction_labels(direction_labels)
44
+ puts aliases_parser.add(target_email, to: labels)
45
+ when 'rm'
46
+ labels = AliasesParser.parse_direction_labels(direction_labels)
47
+ puts aliases_parser.rm(target_email, from: labels)
48
+ when 'list'
49
+ puts aliases_parser.list(target_email)
50
+ end
@@ -0,0 +1,79 @@
1
+ module SugoiAliasesUpdator
2
+ class AliasesParser
3
+
4
+ attr_accessor :changed_labels
5
+
6
+ def self.parse_direction_labels(string)
7
+ raise("Oops '#{string}'") unless /(TO|FROM)=(.*)$/ =~ string
8
+ $2.split(/,\s?/)
9
+ end
10
+
11
+ def initialize(filepath)
12
+ @native_lines = File.readlines(filepath)
13
+ @changed_labels = []
14
+ end
15
+
16
+ def add(target_email, to: )
17
+ check_labels!(to)
18
+ to.each do |x|
19
+ unless label_mails_hash[x].include?(target_email)
20
+ label_mails_hash[x].push(target_email)
21
+ @changed_labels << x
22
+ end
23
+ end
24
+ render!
25
+ end
26
+
27
+ def rm(target_email, from: )
28
+ check_labels!(from)
29
+ from.each do |x|
30
+ if label_mails_hash[x].include?(target_email)
31
+ label_mails_hash[x].delete(target_email)
32
+ @changed_labels << x
33
+ end
34
+ end
35
+ render!
36
+ end
37
+
38
+ def list(target_email)
39
+ finded = []
40
+ label_mails_hash.each do |key, value|
41
+ finded.push(key) if value.include?(target_email)
42
+ end
43
+ finded.join(',')
44
+ end
45
+
46
+ def render!
47
+ new_lines = []
48
+ @native_lines.each do |line|
49
+ line_paser = LineParser.new(line)
50
+ if line_paser.is_aliaes_line && @changed_labels.include?(line_paser.label)
51
+ line = "#{line_paser.label}:#{line_paser.margin}#{label_mails_hash[line_paser.label].join(", ")}\n"
52
+ end
53
+ new_lines << line
54
+ end
55
+ new_lines.join
56
+ end
57
+
58
+ private
59
+
60
+ def label_mails_hash
61
+ @label_mails_hash ||= {}.tap do |h|
62
+ @native_lines.each do |line|
63
+ line_paser = LineParser.new(line)
64
+ if line_paser.is_aliaes_line
65
+ emails_line = line_paser.emails_line
66
+ h[line_paser.label] = line_paser.emails_line.split(/\,\s?/)
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ def check_labels!(inputed_labels)
73
+ unknown_labels = inputed_labels - label_mails_hash.keys
74
+ if unknown_labels.size > 0
75
+ raise("unknown labels #{unknown_labels.join(', ')}")
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,12 @@
1
+ module SugoiAliasesUpdator
2
+ class LineParser
3
+ attr_accessor :emails_line, :label, :is_aliaes_line, :margin
4
+
5
+ def initialize(line)
6
+ @is_aliaes_line = /^(.*):(\s*)([\w@\., ]*)$/ === line
7
+ @label = $1
8
+ @margin = $2
9
+ @emails_line = $3
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module SugoiAliasesUpdator
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "sugoi_aliases_updator/line_parser"
2
+ require "sugoi_aliases_updator/aliases_parser"
3
+
4
+ module SugoiAliasesUpdator
5
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sugoi_aliases_updator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sugoi_aliases_updator"
8
+ spec.version = SugoiAliasesUpdator::VERSION
9
+ spec.authors = ["jiikko"]
10
+ spec.email = ["n905i.1214@gmail.com"]
11
+
12
+ spec.summary = %q{/etc/aliases uppdator.}
13
+ spec.description = %q{/etc/aliases uppdator.}
14
+ spec.homepage = "https://github.com/jiikko/aliases-updator"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.9"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sugoi_aliases_updator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - jiikko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: "/etc/aliases uppdator."
56
+ email:
57
+ - n905i.1214@gmail.com
58
+ executables:
59
+ - sugoi_aliases_updator
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE
68
+ - README.md
69
+ - Rakefile
70
+ - exe/sugoi_aliases_updator
71
+ - lib/sugoi_aliases_updator.rb
72
+ - lib/sugoi_aliases_updator/aliases_parser.rb
73
+ - lib/sugoi_aliases_updator/line_parser.rb
74
+ - lib/sugoi_aliases_updator/version.rb
75
+ - sugoi_aliases_updator.gemspec
76
+ homepage: https://github.com/jiikko/aliases-updator
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.5
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: "/etc/aliases uppdator."
100
+ test_files: []