shellutils 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/bin/shellutils +3 -0
- data/lib/shellutils.rb +135 -0
- data/lib/shellutils/version.rb +3 -0
- data/shellutils.gemspec +22 -0
- data/spec/shellutils_spec.rb +12 -0
- data/spec/spec_helper.rb +8 -0
- metadata +82 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Kazuhiro Yamada
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Shellutils
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'shellutils'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install shellutils
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/shellutils
ADDED
data/lib/shellutils.rb
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'thor'
|
4
|
+
require 'timeout'
|
5
|
+
|
6
|
+
module ShellUtils
|
7
|
+
SUDO_PWD_PATH = ".sudo_pwd"
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
@@color = Thor::Shell::Color.new
|
12
|
+
|
13
|
+
def say(cmd, color=nil)
|
14
|
+
puts @@color.set_color(cmd, color)
|
15
|
+
end
|
16
|
+
|
17
|
+
def sh(cmd)
|
18
|
+
say(cmd, :green)
|
19
|
+
`#{cmd}`
|
20
|
+
end
|
21
|
+
|
22
|
+
def file_write(filepath, str)
|
23
|
+
File.open(filepath, "w") {|f| f.write str}
|
24
|
+
end
|
25
|
+
|
26
|
+
def file_read(filepath)
|
27
|
+
File.open(filepath) {|f| f.read}
|
28
|
+
end
|
29
|
+
|
30
|
+
def is_linux?
|
31
|
+
return true if `uname` =~ /Linux/
|
32
|
+
false
|
33
|
+
end
|
34
|
+
|
35
|
+
def is_mac?
|
36
|
+
return true if `uname` =~ /Darwin/
|
37
|
+
false
|
38
|
+
end
|
39
|
+
|
40
|
+
def get_platform
|
41
|
+
if is_linux?
|
42
|
+
'linux'
|
43
|
+
elsif is_mac?
|
44
|
+
'macos'
|
45
|
+
else
|
46
|
+
'unknown'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def is_installed?(program)
|
51
|
+
`which #{program}`.chomp != ""
|
52
|
+
end
|
53
|
+
|
54
|
+
def get_sudo_pwd
|
55
|
+
if File.exist?(SUDO_PWD_PATH) == false || file_read(SUDO_PWD_PATH) == ""
|
56
|
+
pw = user_input("please input sudo password")
|
57
|
+
set_sudo_pwd(pw)
|
58
|
+
end
|
59
|
+
file_read(SUDO_PWD_PATH)
|
60
|
+
end
|
61
|
+
|
62
|
+
def set_sudo_pwd(pw)
|
63
|
+
file_write(SUDO_PWD_PATH, pw)
|
64
|
+
end
|
65
|
+
|
66
|
+
def user_input(desc, example = nil, choices = nil)
|
67
|
+
if example
|
68
|
+
puts "#{desc}: ex) #{example}"
|
69
|
+
elsif choices
|
70
|
+
puts "#{desc}: [#{choices.join("|")}]"
|
71
|
+
else
|
72
|
+
puts "#{desc}:"
|
73
|
+
end
|
74
|
+
print ">>"
|
75
|
+
input = STDIN.gets.chomp
|
76
|
+
while choices && !choices.include?(input)
|
77
|
+
puts "The value you entered is incorrect(#{input}). Please re-enter"
|
78
|
+
print ">>"
|
79
|
+
input = STDIN.gets.chomp
|
80
|
+
end
|
81
|
+
input
|
82
|
+
end
|
83
|
+
|
84
|
+
def make_file_from_template(template_path, dest_path, hash)
|
85
|
+
puts "create #{dest_path}"
|
86
|
+
template = ERB.new File.read(template_path)
|
87
|
+
open(dest_path, "w") do |f|
|
88
|
+
binded_txt = template.result binding
|
89
|
+
f.puts binded_txt
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def error(msg)
|
94
|
+
STDERR.puts @@color.set_color("### ERROR:#{msg}", :red)
|
95
|
+
raise
|
96
|
+
end
|
97
|
+
|
98
|
+
def current_method(index = 0)
|
99
|
+
caller[index].scan(/`(.*)'/).flatten.to_s
|
100
|
+
end
|
101
|
+
|
102
|
+
def exec_cmd_and_clear_password(cmd)
|
103
|
+
puts cmd
|
104
|
+
PTY.spawn(cmd) do |r, w|
|
105
|
+
w.sync = true
|
106
|
+
expect_result = r.expect(/[Pp]assword.*:.*$/)
|
107
|
+
if expect_result
|
108
|
+
begin
|
109
|
+
timeout(1) {
|
110
|
+
w.puts(get_sudo_pwd)
|
111
|
+
}
|
112
|
+
rescue Exception => err
|
113
|
+
error("the sudo command timed out。password=#{get_sudo_pwd}\nPlease change the password ex) set_sudo_pwd PASSWORD")
|
114
|
+
end
|
115
|
+
end
|
116
|
+
puts "-> ok"
|
117
|
+
w.flush
|
118
|
+
begin
|
119
|
+
puts r.read
|
120
|
+
rescue Errno::EIO # GNU/Linux raises EIO.
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def sudo(cmd)
|
126
|
+
exec_cmd_and_clear_password("sudo #{cmd}")
|
127
|
+
end
|
128
|
+
|
129
|
+
def rvmsudo(cmd)
|
130
|
+
exec_cmd_and_clear_password("rvmsudo #{cmd}")
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
end # ShellUtils
|
135
|
+
|
data/shellutils.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'shellutils/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "shellutils"
|
8
|
+
gem.version = Shellutils::VERSION
|
9
|
+
gem.authors = ["Kazuhiro Yamada"]
|
10
|
+
gem.email = ["kyamada@sonix.asia"]
|
11
|
+
gem.description = %q{Shell utils}
|
12
|
+
gem.summary = %q{Shell utils}
|
13
|
+
gem.homepage = "https://github.com/k-yamada/shellutils"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency 'thor'
|
21
|
+
gem.add_development_dependency 'rspec'
|
22
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shellutils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kazuhiro Yamada
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-12 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: thor
|
16
|
+
requirement: &70276910367540 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70276910367540
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70276910367060 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70276910367060
|
36
|
+
description: Shell utils
|
37
|
+
email:
|
38
|
+
- kyamada@sonix.asia
|
39
|
+
executables:
|
40
|
+
- shellutils
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- LICENSE.txt
|
47
|
+
- README.md
|
48
|
+
- Rakefile
|
49
|
+
- bin/shellutils
|
50
|
+
- lib/shellutils.rb
|
51
|
+
- lib/shellutils/version.rb
|
52
|
+
- shellutils.gemspec
|
53
|
+
- spec/shellutils_spec.rb
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
homepage: https://github.com/k-yamada/shellutils
|
56
|
+
licenses: []
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.8.6
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Shell utils
|
79
|
+
test_files:
|
80
|
+
- spec/shellutils_spec.rb
|
81
|
+
- spec/spec_helper.rb
|
82
|
+
has_rdoc:
|