command_butler 0.0.1 → 0.0.2

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: 0eeab5518d79fe43f062c86c4d4482d6bd906032
4
- data.tar.gz: 2b1dc6526ef7c67e8513a2bd13f3a67965dcd961
3
+ metadata.gz: 4fcd5d2b4252f7911cf5cff13822ae8a3fc5fa62
4
+ data.tar.gz: 10f14973070a5a64af3ffa73e18e405a266e45f0
5
5
  SHA512:
6
- metadata.gz: aab346606ee81ea6d5ae597fb076b3ea8a1792df31fd846c9a1f6d60638e84d9a40d3fb248d411ea1dd53669975cb2a16fbda498498d3c301a7c7715f7589b52
7
- data.tar.gz: 985c1405eae4832f159eec2ac4d778ac9c6ced951b46152b775d5115fdf6e98075fe4c9c24b85d405558bb497d60b97fec6907154679f9040ee7828b201faa3e
6
+ metadata.gz: 8acbfdda96a8755884df3a438942f851a35f3dc388981d7f64edfe4701b4b5eb900169bba703add2629c4002f8c88625176dd4d736681475d9a228e5a270243c
7
+ data.tar.gz: 4c0026498457483612f3611146ee42435ee4f0ce687bcfd9412db822a2cc7e34d87c8f158b83458d304372434d7d00ac8308350d428041e9633b93b67bec1370
@@ -21,6 +21,25 @@ module CommandButler
21
21
  end
22
22
  end
23
23
 
24
+ def execute
25
+ stdout = ""
26
+ stderr = ""
27
+ status = nil
28
+ options = nil
29
+ if set_val_command?
30
+ stdout, stderr, status = Open3.capture3(command)
31
+ else
32
+ # その他は、都度出力されるものを表示したい
33
+ begin
34
+ system command
35
+ rescue => e
36
+ stderr = e.message
37
+ end
38
+ status = $?
39
+ end
40
+ [stdout, stderr, status]
41
+ end
42
+
24
43
  # こういうのを文字列で返したい
25
44
  # {"original_command"=>"date",
26
45
  # "command"=>"date",
@@ -1,10 +1,14 @@
1
1
  module CommandButler
2
2
  class LineDecorator
3
- def self.decoration(command:command, index:index, current_index: current_index, input: input)
3
+ def self.decoration(command:command, index:index, current_index: current_index, history: history)
4
4
  mark = if current_index == index
5
5
  "\e[33m" + " > "
6
- elsif input
7
- "\e[32m" + (input.execute?? " o " : " - ")
6
+ elsif history
7
+ if history[:result]
8
+ history[:result][:status].success?? "\e[32m" + " o " : "\e[31m" + " x "
9
+ else
10
+ "\e[32m" + (history[:input].execute?? " o " : " - ")
11
+ end
8
12
  else
9
13
  "\e[37m" + " "
10
14
  end
@@ -6,10 +6,12 @@ require 'command_butler/val_decorator'
6
6
  require 'command_butler/result_decorator'
7
7
  require 'command_butler/title_decorator'
8
8
  require 'command_butler/input'
9
+ require 'open3'
9
10
  module CommandButler
10
11
  class Mediator
11
12
  def execute(file_name, options)
12
- inputs = {}
13
+ # resultクラスも作る
14
+ histories = []
13
15
  @commands = CommandParser.parse(file_name:file_name)
14
16
 
15
17
  @vals = {}
@@ -28,48 +30,47 @@ module CommandButler
28
30
 
29
31
  # jumpが設定されていたらskipしたことにする
30
32
  if (index < jump_index)
31
- inputs[index] = Input.skip_instance
33
+ histories[index] = {input: Input.skip_instance}
32
34
  next
33
35
  end
34
36
 
35
37
  # show all comamnds
36
- show_commands(current_index: index, inputs: inputs) # 1件実行ごとにコマンドを表示する
38
+ show_commands(current_index: index, histories: histories) # 1件実行ごとにコマンドを表示する
37
39
  # execute
38
40
  input = command.need_confirm?? Input.start : Input.execute_instance
39
41
  exit 1 if input.abort?
40
42
  jump_index = (input.input_value - 1) if input.jump?
41
- inputs[index] = input
42
- if input.execute?
43
- res = execute_command(command:command, index:index)
44
- @commands.each {|c| c.replace_command(val:res[:set_val])} if res[:set_val]
43
+ histories[index] = {input: input}
44
+ if input.execute? && command.command
45
+ stdout, stderr, status = execute_command(command:command, index:index)
46
+ histories[index][:result] = {stdout:stdout, stderr: stderr, status: status}
47
+ @commands.each {|c| c.replace_command(val:{command.set_val => stdout})} if command.set_val_command?
45
48
  end
46
49
  end
47
50
  end
48
51
 
49
52
  def execute_command(command:command, index:index)
50
- res = {set_val:nil}
51
53
  Dir.chdir(command.chdir) if command.chdir
52
- return res unless command.command
53
- # set_valコマンドの時は標準出力を取りたいのでバッククオート実行
54
- # その他は、都度出力されるものを表示したいのでsystemで実行
54
+ return unless command.command
55
55
 
56
- ResultDecorator.decoration(command: command, index: index) do
57
- if command.set_val_command?
58
- val = `#{command.command}`.chomp
59
- puts val
60
- res[:set_val] = {command.set_val => val}
61
- puts " (set_val : #{command.set_val})"
56
+ # set_valコマンドの時は標準出力を取りたいのでopen3で実行
57
+ stdout, stderr, status = "", "", nil
58
+ ResultDecorator.decoration_frame(command: command, index: index) do
59
+ stdout, stderr, status = command.execute
60
+ if status.success?
61
+ ResultDecorator.decoration_stdout stdout: stdout, status:status
62
62
  else
63
- system command.command
63
+ ResultDecorator.decoration_stderr stderr: stderr, status:status
64
64
  end
65
65
  end
66
+
66
67
  sleep 0.5 # 表示がいっきに流れて見失しなうのでsleep
67
- res
68
+ [stdout, stderr, status]
68
69
  end
69
70
 
70
- def show_commands(current_index:current_index, inputs:inputs)
71
+ def show_commands(current_index:current_index, histories:histories)
71
72
  @commands.each_with_index do |command, index|
72
- puts LineDecorator.decoration command: command, index: index, current_index: current_index, input: inputs[index]
73
+ puts LineDecorator.decoration command: command, index: index, current_index: current_index, history: histories[index]
73
74
  detail = LineDetailDecorator.decoration command: command
74
75
  puts detail if 0 < detail.size
75
76
  end
@@ -1,10 +1,20 @@
1
1
  module CommandButler
2
2
  class ResultDecorator
3
- def self.decoration(command:command,index:index, &block)
4
- puts head = "-- result #{index+1}" + "-" * 40
3
+ def self.decoration_frame(command:command,index:index, &block)
4
+ puts head = "-- [#{index+1}] #{command.command || command.chdir} " + "-" * 30
5
5
  yield
6
6
  puts "-" * head.length
7
7
  end
8
- end
9
8
 
9
+ def self.decoration_stdout(stdout: stdout, status:status)
10
+ puts stdout
11
+ puts "\e[32m success status:" + status.to_i.to_s + "\e[0m"
12
+ end
13
+
14
+ def self.decoration_stderr(stderr: stderr, status:status)
15
+ puts stderr
16
+ puts "\e[31m error status:" + status.to_i.to_s+ "\e[0m"
17
+ end
18
+
19
+ end
10
20
  end
@@ -1,3 +1,3 @@
1
1
  module CommandButler
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: command_butler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - motsat
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-04 00:00:00.000000000 Z
11
+ date: 2015-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler