am 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0ac8121440319603d9c34a172b5bf701aa71f7e7
4
- data.tar.gz: 40a62d1010f7798019f672535fc55a5451688a1c
3
+ metadata.gz: 3ed54a2838657b508701ab2bfeb4761cc46008e9
4
+ data.tar.gz: 7e18590971c2c43c4c39246b43a72d8e0f037e74
5
5
  SHA512:
6
- metadata.gz: 6f6ac069e16b5ad8024520b63b6efa164eab40981ad6872d86abe4b94bc51a04324374a386a369bc289afd6a18f09470c70de7ffc62fd2e1e946f74ef771e916
7
- data.tar.gz: ca6af85e41a5f392d4e7f693e6a4360880ba2f9a4ea77571cd39147ec5b41a3925d8e3ade6de0b1468fcef1f92f0171e7d499a88868345c57ad7cc0a5995f34e
6
+ metadata.gz: 13168b80a8a7d7348d92ce8e50db49b13c614c1925484d7d4120076e3ff988f7b8a2861a4373a2833f06be69d8de8a58cf1dd4e890d6420874e3f7c3507e2994
7
+ data.tar.gz: e400ea2db125f6c3d17db65be2d632f0fe17ab2fb0abcd593e7758d56dbca3c16607bc29f56da6790e9a11815a2417bf222e49d39dbfdc34b0520c956ebfbd3d
data/.travis.yml CHANGED
@@ -6,4 +6,6 @@ script: bundle exec rspec
6
6
  before_script:
7
7
  - "touch ~/.zsh_history"
8
8
  - "touch ~/.bash_history"
9
-
9
+ addons:
10
+ code_climate:
11
+ repo_token:12696de76803f797a3add017d83ea158a13fd6f3bde7fdffbc3218ccaf473a2a
data/Gemfile CHANGED
@@ -2,3 +2,4 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in am.gemspec
4
4
  gemspec
5
+ gem "codeclimate-test-reporter", group: :test, require: nil
data/README.md CHANGED
@@ -55,6 +55,10 @@ echo 'source ~/.am_config' >> ~/.bash_profile
55
55
  delete alias select from current config
56
56
 
57
57
  ### configure
58
+ * defoult alias
59
+ ```
60
+ aml = source ~/.am_config
61
+ ```
58
62
 
59
63
  * history file location(optional)
60
64
 
data/lib/am/cli.rb CHANGED
@@ -1,12 +1,18 @@
1
- # encoding: utf-8
1
+ # encoding: utf-8
2
2
  require 'am'
3
3
  require 'tail'
4
4
  require 'config'
5
5
  require 'ui'
6
+ require 'validate'
7
+ require 'message_control'
6
8
  require 'thor'
7
9
 
8
10
  module AM
9
11
  class CLI < Thor
12
+
13
+ include MessageControl
14
+ include Validate
15
+
10
16
  default_command :show
11
17
  def initialize(*args)
12
18
  super
@@ -17,7 +23,7 @@ module AM
17
23
  desc "show", "show current alias"
18
24
  def show
19
25
  if @config.al.empty?
20
- puts 'a blank config'
26
+ notice(:config_empty)
21
27
  else
22
28
  @ui.print_current_config(@config)
23
29
  end
@@ -32,20 +38,16 @@ module AM
32
38
  if options[:list]
33
39
  commands = tail.get_last_five_command
34
40
  @ui.print_last_commands(commands)
35
- add_record = @ui.add_command_with_number(commands)
41
+ new_alias = @ui.add_command_with_number(commands)
36
42
 
37
43
  # registeration from last history
38
44
  else
39
45
  last_command = tail.get_last_command
40
- add_record = @ui.add_command_with_last_history(last_command)
46
+ new_alias = @ui.add_command_with_last_history(last_command)
41
47
  end
42
48
 
43
- if uniq?(add_record) && valid?(add_record)
44
- @config.al.merge!(add_record)
45
- add_config(add_record)
46
- else
47
- AM.p1("")
48
- show
49
+ if uniq?(new_alias) && valid?(new_alias)
50
+ @config.add_config(new_alias)
49
51
  end
50
52
  end
51
53
 
@@ -53,7 +55,7 @@ module AM
53
55
  option :list, :type => :boolean, :aliases => '-l'
54
56
  def del(delete_alias=nil)
55
57
  if @config.al.empty?
56
- puts 'a blank config'
58
+ notice(:config_empty)
57
59
  exit
58
60
  end
59
61
 
@@ -62,56 +64,16 @@ module AM
62
64
  arr = @ui.print_current_config(@config)
63
65
  delete_alias = @ui.del_command_with_number(arr)
64
66
  end
65
- @config.al.delete(delete_alias)
66
- delete_config(delete_alias)
67
+ if delete_alias && @config.al.key?(delete_alias)
68
+ @config.al.delete(delete_alias)
69
+ else
70
+ warning(:empty_config_number)
71
+ self.del
72
+ end
73
+ @config.delete_config(delete_alias)
67
74
  end
68
75
 
69
76
  no_commands do
70
- # todo merge config
71
- def add_config(add_record)
72
- ak,av = add_record.first
73
- if @config.save_config
74
- AM.p1("[success] #{ak} / #{av} added command")
75
- AM.p2("please run: [ source #{CONFIG_FILE} ]")
76
-
77
- else
78
- puts "[error] #{ak} / #{av} couldn't add command"
79
- end
80
- end
81
-
82
- def delete_config(exclude)
83
- if @config.save_config
84
- AM.p1("[success] delete alias #{exclude}")
85
- AM.p2("please run: [ source #{CONFIG_FILE} ]")
86
-
87
- else
88
- AM.p2("[error] failue delete alias #{exclude}}")
89
- end
90
- end
91
- # todo end
92
- # todo move config class
93
- def uniq?(add_record)
94
- ak,av = add_record.first
95
- @config.al.each do |k,v|
96
- if ak == k
97
- AM.p1("[error] not written as duplecate alias is '#{ak}'")
98
- return false
99
- elsif av == v
100
- AM.p1("[error] not written as duplecate command is #{av}")
101
- return false
102
- end
103
- end
104
- true
105
- end
106
- # end
107
- def valid?(add_record)
108
- ak,av = add_record.first
109
- unless ak.length > 0 || av.length > 0
110
- puts "[error] #{ak} / #{av} length equal 0"
111
- return false
112
- end
113
- true
114
- end
115
77
  end
116
78
  end
117
79
  end
data/lib/am/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Am
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/config.rb CHANGED
@@ -1,23 +1,44 @@
1
- # encoding: utf-8
1
+ # encoding: utf-8
2
2
  require 'am'
3
+ require 'message_control'
3
4
 
4
5
  module AM
5
6
  class Config
7
+ include MessageControl
6
8
  attr_accessor :al, :pg
7
9
 
8
10
  def initialize
9
- @al = {}
10
- @pg = {}
11
11
  load_config
12
12
  end
13
13
 
14
14
  def load_config
15
15
  @al = file_load(CONFIG_FILE)
16
16
  @pg = file_load(LOCAL_FILE)
17
+ @al.delete('aml')
18
+ end
19
+
20
+ def add_config(new_alias)
21
+ @al.merge!(new_alias)
22
+ if save_config
23
+ notice(:success_add_command, [new_alias.first.to_a, CONFIG_FILE].flatten)
24
+ else
25
+ error(add_command,[ak, av])
26
+ end
27
+ end
28
+
29
+ def delete_config(del_alias)
30
+ @al.delete(del_alias)
31
+ if save_config
32
+ notice(:success_delete_command, [del_alias, CONFIG_FILE])
33
+ else
34
+ error(:fail_delete, del_alias)
35
+ end
17
36
  end
18
37
 
19
38
  def save_config
20
- (file_write(CONFIG_FILE, @al, 'alias ') && file_write(LOCAL_FILE, @pg))
39
+ (
40
+ file_write(CONFIG_FILE, @al.merge({"aml" => 'source ~/.am_config'}))
41
+ )
21
42
  end
22
43
 
23
44
  def file_write(file_name, config, prefix=nil)
@@ -38,7 +59,7 @@ module AM
38
59
  File.open(file_name, 'r') do |file|
39
60
  file.each_line do |line|
40
61
  line = line.strip
41
- buf << line.gsub(/^alias /, '').split('=', 2) if line !~ /^$/ && line =~ /.+=.+/ && line !~ /^#.*/
62
+ buf << line.gsub(/^alias /, '').split('=', 2) if line !~ /^$/ && line =~ /.+=.+/ && line !~ /^#.*/
42
63
  end
43
64
  end if File.exists?(file_name)
44
65
  Hash[buf]
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+ require 'am'
3
+
4
+ module AM
5
+ module MessageControl
6
+ NOTICE_MESSAGE = {
7
+ config_empty: 'config is empty',
8
+ success_add_command: "success! %s / %s added command\n please run: [ source %s ]",
9
+ success_delete_command: "success! %s delete alias\n please run: [ source %s ]"
10
+ }
11
+ WARNING_MESSAGE = {
12
+ add_command: "%s / %s couldn't add command",
13
+ empty_config_number: "selected number missing in current config",
14
+ duplecate_alias: "not written as duplecate alias is '%s'",
15
+ duplecate_command: "not written as duplecate command is %s",
16
+ }
17
+ ERROR_MESSAGE = {
18
+ add_command: "%s / %s couldn't add command",
19
+ validate_length_zero: "%s / %s length equal 0",
20
+ faile_delete: "failue delete alias %s",
21
+ }
22
+
23
+ def notice(code, val=nil)
24
+ puts "\n" + '-'*60
25
+ print(NOTICE_MESSAGE, 'info', code, val)
26
+ puts '-'*60
27
+ end
28
+
29
+ def warning(code, val=nil)
30
+ puts "\n" + '-'*60
31
+ print(WARNING_MESSAGE, 'warning', code, val)
32
+ puts '-'*60
33
+ end
34
+ def error(code, val=nil)
35
+ puts "\n" + '-'*60
36
+ print(ERROR_MESSAGE, 'error', code, val)
37
+ puts '-'*60
38
+ exit
39
+ end
40
+
41
+ def print(template, prefix, code, val=nil)
42
+ puts "[#{prefix}] #{template[code]}"%val
43
+ end
44
+ end
45
+ end
data/lib/tail.rb CHANGED
@@ -5,72 +5,62 @@ module AM
5
5
  class Tail
6
6
  attr_accessor :profile
7
7
  def initialize(config)
8
- set_profile(config)
8
+ @profile = get_profile(config)
9
9
  end
10
10
 
11
- def set_profile(config)
11
+ def get_profile(config)
12
12
  shell = ENV['SHELL']
13
- @profile = {}
14
-
15
- if shell =~ /zsh/
16
- @profile = {
17
- margin: 0,
18
- max_line: 5,
19
- file: '~/.zsh_history'
20
- }
21
- elsif shell =~ /bash/
22
- @profile = {
23
- margin: 1,
24
- max_line: 5,
25
- file: '~/.bash_history'
26
- }
27
- else
28
- puts "does not support is #{shell}"
29
- exit
13
+ h = case shell
14
+ when /zsh/ then set_hash(0, 5, '~/.zsh_history')
15
+ when /bash/ then set_hash(1, 5, '~/.bash_history')
16
+ else # todo raise
17
+ puts "does not support is #{shell}"
18
+ exit
30
19
  end
31
20
 
32
- @profile[:file] = config.pg['history_file'] unless config.pg['history_file'].nil?
33
- @profile[:file] = File.expand_path(@profile[:file])
21
+ h[:file] = f if f = config.pg['history_file']
22
+ h[:file] = File.expand_path(h[:file])
34
23
 
35
- unless File.exists?(@profile[:file])
36
- puts "history file not found #{@profile[:file]}"
24
+ unless File.exists?(h[:file])
25
+ #todo raise
26
+ puts "history file not found #{h[:file]}"
37
27
  exit
38
28
  end
29
+ h
30
+ end
31
+
32
+ def set_hash(margin, max_line, history_file)
33
+ return { margin: margin, max_line: max_line, file: history_file }
39
34
  end
40
35
 
41
36
  def get_last_five_command
42
- exit if @profile.empty?
43
37
  commands = []
38
+ r=""
44
39
  last_commands = `tail -#{@profile[:max_line] + 1 - @profile[:margin]} #{@profile[:file]} | head -#{@profile[:max_line]}`.split("\n")
45
-
46
- last_commands.each_with_index do |c,i|
47
- if r = sampling(c)
48
- commands << r
40
+ unless last_commands.empty?
41
+ last_commands.each_with_index do |c,i|
42
+ commands << r if r = sampling(c)
49
43
  end
50
- end unless last_commands.empty?
51
-
52
- commands
44
+ commands
45
+ end
53
46
  end
54
47
 
55
48
  def get_last_command
56
- exit if @profile[:file].empty?
57
-
49
+ r=""
58
50
  if c = `tail -#{2-@profile[:margin]} #{@profile[:file]} | head -1`
59
- if r = sampling(c)
60
- r
61
- end
51
+ sampling(c)
62
52
  end
63
53
  end
64
54
 
65
55
  def sampling(command)
66
56
  zsh = '[0-9]+:[0-0];(.*)'
67
57
  bash = '(.*)'
68
- if command =~ /#{zsh}/
69
- p = zsh
70
- else
71
- p = bash
72
- end
73
- command.split(/#{p}/)[COMMAND].strip if command.strip !~ /^$/
58
+ if command =~ /#{zsh}/
59
+ p = zsh
60
+ else
61
+ p = bash
62
+ end
63
+ command.split(/#{p}/)[COMMAND].strip if command.strip !~ /^$/
74
64
  end
75
65
  end
76
66
  end
data/lib/ui.rb CHANGED
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # encoding: utf-8
2
2
  require 'am'
3
3
 
4
4
  module AM
data/lib/validate.rb ADDED
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ require 'am'
3
+
4
+ module AM
5
+ module Validate
6
+ def uniq?(new_alias)
7
+ ak,av = new_alias.first
8
+ @config.al.each do |k,v|
9
+ if ak == k
10
+ warning(:duplecate_alias, ak)
11
+ return false
12
+ elsif av == v
13
+ warning(:duplecate_command, av)
14
+ return false
15
+ end
16
+ end
17
+ true
18
+ end
19
+
20
+ def valid?(new_alias)
21
+ ak,av = new_alias.first
22
+ unless ak.length > 0 || av.length > 0 || ak == 'aml'
23
+ error(:validate_lenght_zero, [ak,av])
24
+ end
25
+ true
26
+ end
27
+
28
+ end
29
+ end
@@ -1,11 +1,11 @@
1
- # encoding: utf-8
1
+ # encoding: utf-8
2
2
  require 'spec_helper.rb'
3
3
 
4
4
  describe AM::CLI do
5
5
  shared_examples_for "shell test" do
6
6
  before do
7
7
  ENV['SHELL'] = shell
8
- add_history(AM::Tail.new(AM::Config.new).profile[:file])
8
+ add_history
9
9
  end
10
10
 
11
11
  before(:each) do
@@ -1,46 +1,69 @@
1
1
  require 'spec_helper.rb'
2
2
 
3
3
  describe AM::Config do
4
- context 'zsh' do
4
+ shared_examples_for 'config test' do
5
5
  before do
6
- `echo "alias test1='test az - AZ 09 _'" > #{AM::CONFIG_FILE}`
7
- `echo "history_file=~/.zsh_history" > #{AM::LOCAL_FILE}`
8
- @config= AM::Config.new
9
- @ak,@av = @config.al.first
6
+ ENV['SHELL'] = shell
10
7
  end
8
+ describe 'alias config' do
9
+ describe 'load config' do
10
+ before do
11
+ file_write(AM::CONFIG_FILE,"alias test1='test az - AZ 09 _", 'w')
12
+ @config= AM::Config.new
13
+ @ak,@av = @config.al.first
14
+ end
15
+ it 'load' do
16
+ expect(@ak).to eq 'test1'
17
+ expect(@av).to eq "'test az - AZ 09 _"
18
+ end
19
+ end
11
20
 
12
- it 'load config' do
13
- expect(@ak).to eq 'test1'
14
- expect(@av).to eq "'test az - AZ 09 _'"
15
- expect(@config.pg['history_file']).to eq "~/.zsh_history"
21
+ describe 'save config'do
22
+ before do
23
+ file_write(AM::CONFIG_FILE,"alias test1='test az - AZ 09 _", 'w')
24
+ @config= AM::Config.new
25
+ @config.al.merge!({'test2' => "'abcdefgeijklmn'"})
26
+ end
27
+ it 'add' do
28
+ expect(@config.save_config).to be true
29
+ expect(@config.al.length).to eql(2)
30
+ expect(@config.al.key?('test2')).to be true
31
+ expect(@config.al['test2']).to eq "'abcdefgeijklmn'"
32
+ end
33
+ it 'delete' do
34
+ @config.al.delete('test1')
35
+ expect(@config.save_config()).to be true
36
+ expect(@config.al.length).to eql(1)
37
+ expect(@config.al.key?('test2')).to be true
38
+ expect(@config.al['test2']).to eq "'abcdefgeijklmn'"
39
+ end
40
+ end
16
41
  end
42
+ end
17
43
 
18
- it 'save config'do
19
- # add
20
- @config.al.merge!({'test2' => "'abcdefgeijklmn'"})
21
- expect(@config.save_config).to be true
22
- expect(@config.al.length).to eql(2)
23
-
24
- # delete
25
- #
26
- @config.al.delete('test1')
27
- expect(@config.save_config()).to be true
28
- expect(@config.al.length).to eql(1)
29
-
30
- # value check
31
- expect(@config.al.key?('test2')).to be true
32
- expect(@config.al['test2']).to eq "'abcdefgeijklmn'"
33
- expect(@config.pg['history_file']).to eq "~/.zsh_history"
34
-
44
+ describe 'pg config' do
45
+ describe 'load config' do
46
+ before do
47
+ file_write(AM::LOCAL_FILE, "history_file=~/.csh_history", 'w')
48
+ @config= AM::Config.new
49
+ @pk,@pv = @config.pg.first
50
+ end
51
+ it 'load config' do
52
+ expect(@pk).to eq 'history_file'
53
+ expect(@pv).to eq "~/.csh_history"
54
+ end
35
55
  end
36
-
37
- it 'pg config' do
38
- # @config.pg_check
39
- `echo "history_file=~/.csh_history" > #{AM::LOCAL_FILE}`
40
- config= AM::Config.new
41
- expect(config.pg['history_file']).to eq "~/.csh_history"
42
- `echo "" > #{AM::LOCAL_FILE}`
56
+ after do
57
+ file_write(AM::LOCAL_FILE, '','w');
43
58
  end
44
59
  end
45
- end
60
+ describe 'zsh' do
61
+ let(:shell) {'/bin/zsh' }
62
+ it_should_behave_like 'config test'
63
+ end
46
64
 
65
+ describe 'bash' do
66
+ let(:shell) {'/bin/bash' }
67
+ it_should_behave_like 'config test'
68
+ end
69
+ end
@@ -1,34 +1,31 @@
1
1
  require 'spec_helper.rb'
2
-
3
2
  describe AM::Tail do
4
- context 'tail commands tests' do
5
- shared_examples_for "shell test" do
6
- before do
7
- ENV['SHELL'] = shell
8
- @tail = AM::Tail.new(AM::Config.new)
9
- add_history(@tail.profile[:file])
10
- end
11
-
12
- it 'get_last_five_commands' do
13
- expect(@tail.get_last_five_command.length).to eq 5
14
- expect(@tail.get_last_five_command[0]).to match(/[a-z]+/)
15
- end
16
-
17
- it 'get_last_command' do
18
- expect(@tail.get_last_command.length).to be >= 1
19
- expect(@tail.get_last_command[0]).to match(/[a-z]+/)
20
- end
3
+ shared_examples_for "tail test" do
4
+ before do
5
+ ENV['SHELL'] = shell
6
+ @tail = AM::Tail.new(AM::Config.new)
7
+ add_history
21
8
  end
22
9
 
23
- describe 'zsh' do
24
- let(:shell) {'/bin/zsh' }
25
- it_should_behave_like 'shell test'
10
+ it 'get_last_five_commands' do
11
+ expect(@tail.get_last_five_command.length).to eq 5
12
+ expect(@tail.get_last_five_command[0]).to match(/[a-z]+/)
26
13
  end
27
14
 
28
- describe 'bash' do
29
- let(:shell) {'/bin/bash' }
30
- it_should_behave_like 'shell test'
15
+ it 'get_last_command' do
16
+ expect(@tail.get_last_command.length).to be >= 1
17
+ expect(@tail.get_last_command[0]).to match(/[a-z]+/)
31
18
  end
32
19
  end
20
+
21
+ describe 'zsh' do
22
+ let(:shell) {'/bin/zsh' }
23
+ it_should_behave_like 'tail test'
24
+ end
25
+
26
+ describe 'bash' do
27
+ let(:shell) {'/bin/bash' }
28
+ it_should_behave_like 'tail test'
29
+ end
33
30
  end
34
31
 
data/spec/spec_helper.rb CHANGED
@@ -1,13 +1,21 @@
1
1
  # encoding: utf-8
2
+ require "codeclimate-test-reporter"
3
+ CodeClimate::TestReporter.start
2
4
  require 'am'
3
5
  require 'am/cli'
4
6
  require 'tail'
5
7
  require 'config'
6
8
 
9
+ def file_write(file_name, records, mode='a')
10
+ file_name = File.expand_path(file_name)
11
+ file = File.open(file_name, mode)
12
+ records.split("\n").each {|r| file.puts(r) }
13
+ file.close
14
+ end
7
15
 
8
- def add_history(hist_file)
16
+ def add_history
9
17
 
10
- record = <<'EOS'
18
+ records = <<'EOS'
11
19
  : 1426499455:0;rake
12
20
  rake
13
21
  : 1426500006:0;rails
@@ -21,15 +29,13 @@ pepabo
21
29
  : 1426500618:0;abcd ABCD 1234 あいうえ
22
30
  abcd ABCD 1234 あいうえ"
23
31
  EOS
24
- hist_file = File.expand_path(hist_file)
32
+ hist_file = File.expand_path(AM::Tail.new(AM::Config.new).profile[:file])
25
33
  type = File.exists?(hist_file)? 'a' : 'w'
26
- file = File.open(hist_file, type)
27
- record.split("\n").each {|r| file.puts(r) }
28
- file.close
34
+ file_write(hist_file, records, type)
29
35
  end
30
36
 
31
37
  def add_alias_config
32
- record = <<'EOS'
38
+ records = <<'EOS'
33
39
  alias hoge='fuga'
34
40
  alias 123='123'
35
41
  alias ABC='ABC'
@@ -38,9 +44,7 @@ alias ほげ='ふがふが'
38
44
  EOS
39
45
 
40
46
  config_file = File.expand_path(AM::CONFIG_FILE)
41
- file = File.open(config_file, "w")
42
- record.split("\n").each {|r| file.puts(r) }
43
- file.close
47
+ file_write(config_file, records, 'w')
44
48
  end
45
49
 
46
50
  def match_current_config
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: am
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - ka-yamashita
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-16 00:00:00.000000000 Z
11
+ date: 2015-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,8 +86,10 @@ files:
86
86
  - lib/am/cli.rb
87
87
  - lib/am/version.rb
88
88
  - lib/config.rb
89
+ - lib/message_control.rb
89
90
  - lib/tail.rb
90
91
  - lib/ui.rb
92
+ - lib/validate.rb
91
93
  - spec/lib/am/cli_spec.rb
92
94
  - spec/lib/config_spec.rb
93
95
  - spec/lib/tail_spec.rb