shellutils 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -18,7 +18,7 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ Take a look at the [spec file](https://github.com/k-yamada/shellutils/blob/master/spec/shellutils_spec.rb)
22
22
 
23
23
  ## Contributing
24
24
 
@@ -6,7 +6,7 @@ require 'pty'
6
6
  require 'expect'
7
7
 
8
8
  module ShellUtils
9
- SUDO_PWD_PATH = ".sudo_pwd"
9
+ SUDO_PWD_PATH = File.expand_path("~/.sudo_pwd")
10
10
 
11
11
  class << self
12
12
 
@@ -33,6 +33,10 @@ module ShellUtils
33
33
  File.open(filepath, "w") {|f| f.write str}
34
34
  end
35
35
 
36
+ def file_add(filepath, str)
37
+ File.open(filepath, "a") {|f| f.write str}
38
+ end
39
+
36
40
  def file_read(filepath)
37
41
  File.open(filepath) {|f| f.read}
38
42
  end
@@ -105,7 +109,7 @@ module ShellUtils
105
109
  raise
106
110
  end
107
111
 
108
- def current_method(index = 0)
112
+ def current_method(index=0)
109
113
  caller[index].scan(/`(.*)'/).flatten.to_s
110
114
  end
111
115
 
@@ -128,8 +132,6 @@ module ShellUtils
128
132
  end
129
133
  end
130
134
  w.flush
131
- puts "-> ok"
132
- w.flush
133
135
  begin
134
136
  puts r.read
135
137
  rescue Errno::EIO # GNU/Linux raises EIO.
@@ -144,7 +146,32 @@ module ShellUtils
144
146
  def rvmsudo(cmd)
145
147
  exec_cmd_and_clear_password("rvmsudo #{cmd}")
146
148
  end
147
-
149
+
150
+ def add_config(file_path, config_title, config_content, comment_char="#")
151
+ text = File.read(file_path)
152
+
153
+ # delete config if already exists
154
+ text.gsub!(/#{get_config_header(config_title, comment_char)}.*#{get_config_footer(comment_char)}/m, "")
155
+
156
+ # add config
157
+ text << get_config_header(config_title, comment_char)
158
+ text << config_content
159
+ text << get_config_footer(comment_char)
160
+ File.open(file_path, "w") {|f| f.write(text)}
161
+ end
162
+
163
+ private
164
+
165
+ def get_config_header(config_title, comment_char)
166
+ "#{comment_char * 2} #{config_title}\n"
167
+ end
168
+
169
+ def get_config_footer(comment_char)
170
+ "#{comment_char * 2} END\n"
171
+ end
172
+
148
173
  end
174
+
175
+
149
176
  end # ShellUtils
150
177
 
@@ -1,3 +1,3 @@
1
1
  module Shellutils
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -1,16 +1,57 @@
1
1
  require 'spec_helper'
2
+ require 'tempfile'
2
3
 
3
4
  module ShellUtils
4
5
  describe ShellUtils do
5
- it "shoudl exec shell command" do
6
- ShellUtils.sh "pwd"
6
+ describe ".sh" do
7
+ it "exec shell command" do
8
+ ShellUtils.sh "pwd"
9
+ end
7
10
  end
8
11
 
9
- it "shoudl exec shell command" do
10
- ShellUtils.sudo "pwd"
12
+ describe ".sudo" do
13
+ #it "exec shell command" do
14
+ # ShellUtils.sudo "pwd"
15
+ #end
11
16
  end
12
-
13
- end
14
17
 
18
+ describe ".add_config_text" do
19
+ it "add config text" do
20
+ config_text = <<-EOF
21
+ config1=value1
22
+ config2=value2
23
+ EOF
24
+ tf = Tempfile.open("tmp")
25
+ ShellUtils.add_config(tf.path, "sample", config_text)
26
+ result = tf.read
27
+ result.should =~ /config1=value1/
28
+ result.should =~ /config2=value2/
29
+ tf.close!
30
+ end
31
+
32
+ it "overwrite config text" do
33
+ config_text = <<-EOF
34
+ config3=value3
35
+ config4=value4
36
+ EOF
37
+ tf = Tempfile.open("tmp")
38
+ initial_text = <<-EOF
39
+ ## sample
40
+ config1=value1
41
+ config2=value2
42
+ ## END
43
+ EOF
44
+ File.open(tf.path, "w") {|f| f.write initial_text}
15
45
 
46
+ ShellUtils.add_config(tf.path, "sample", config_text)
47
+ result = tf.read
48
+ result.should_not =~ /config1=value1/
49
+ result.should_not =~ /config2=value2/
50
+ result.should =~ /config3=value3/
51
+ result.should =~ /config4=value4/
52
+ tf.close!
53
+ end
54
+ end
55
+
56
+ end
16
57
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shellutils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-22 00:00:00.000000000 Z
12
+ date: 2012-10-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor
@@ -89,3 +89,4 @@ summary: Shell utils
89
89
  test_files:
90
90
  - spec/shellutils_spec.rb
91
91
  - spec/spec_helper.rb
92
+ has_rdoc: