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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4fcd5d2b4252f7911cf5cff13822ae8a3fc5fa62
|
4
|
+
data.tar.gz: 10f14973070a5a64af3ffa73e18e405a266e45f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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,
|
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
|
7
|
-
|
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
|
-
|
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
|
-
|
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,
|
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
|
-
|
42
|
-
if input.execute?
|
43
|
-
|
44
|
-
|
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
|
53
|
-
# set_valコマンドの時は標準出力を取りたいのでバッククオート実行
|
54
|
-
# その他は、都度出力されるものを表示したいのでsystemで実行
|
54
|
+
return unless command.command
|
55
55
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
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
|
-
|
63
|
+
ResultDecorator.decoration_stderr stderr: stderr, status:status
|
64
64
|
end
|
65
65
|
end
|
66
|
+
|
66
67
|
sleep 0.5 # 表示がいっきに流れて見失しなうのでsleep
|
67
|
-
|
68
|
+
[stdout, stderr, status]
|
68
69
|
end
|
69
70
|
|
70
|
-
def show_commands(current_index:current_index,
|
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,
|
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.
|
4
|
-
puts head = "--
|
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
|
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.
|
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-
|
11
|
+
date: 2015-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|