aka2 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +32 -0
- data/LICENSE.txt +21 -0
- data/README.md +37 -0
- data/Rakefile +10 -0
- data/aka.gemspec +36 -0
- data/bin/aka +5 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/.config +9 -0
- data/lib/aka.rb +1188 -0
- data/lib/aka/version.rb +3 -0
- metadata +169 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6b42c6d23b25160f7d666276d36d857b4aa047ef
|
4
|
+
data.tar.gz: a5002d7e07128d791043eca594a07c8cfdec78a0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e3c58913ffefbb616c13aeea0c7441044cb4d4255d95f9e0889b00cc4b5e3b54faef0cf02b932997d921b516f7c66266c35e153f6acd61643ada7d7ddd4d33ac
|
7
|
+
data.tar.gz: 9816c945c799fec31e5574941bdd4dd9a3533795c8573facbae5d79f47de2db1d447c15ed3cd7064ab4ea8eff4b1072cdf3db84b7b5cb9de59102c51a67a3d9e
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
aka (0.0.1)
|
5
|
+
colorize (~> 0)
|
6
|
+
net-scp (~> 1.2, >= 1.2.1)
|
7
|
+
safe_yaml (~> 1.0, >= 1.0.4)
|
8
|
+
thor (~> 0.19.1)
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: https://rubygems.org/
|
12
|
+
specs:
|
13
|
+
colorize (0.7.7)
|
14
|
+
minitest (5.7.0)
|
15
|
+
net-scp (1.2.1)
|
16
|
+
net-ssh (>= 2.6.5)
|
17
|
+
net-ssh (2.9.2)
|
18
|
+
rake (10.4.2)
|
19
|
+
safe_yaml (1.0.4)
|
20
|
+
thor (0.19.1)
|
21
|
+
|
22
|
+
PLATFORMS
|
23
|
+
ruby
|
24
|
+
|
25
|
+
DEPENDENCIES
|
26
|
+
aka!
|
27
|
+
bundler (~> 1.10)
|
28
|
+
minitest (~> 5.7)
|
29
|
+
rake (~> 10.0)
|
30
|
+
|
31
|
+
BUNDLED WITH
|
32
|
+
1.10.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Ryan Goh
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# aka - The Missing Alias Manager
|
2
|
+
|
3
|
+
aka generate/edit/destroy/find permanent aliases with a single command.
|
4
|
+
|
5
|
+
aka2 requires ruby and is built for bash and zsh users.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
$ gem install aka2
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
$ aka generate hello="echo helloworld"
|
14
|
+
$ aka destroy hello
|
15
|
+
$ aka edit hello
|
16
|
+
$ aka find hello
|
17
|
+
$ aka usage
|
18
|
+
$ aka
|
19
|
+
|
20
|
+
## Requirement
|
21
|
+
|
22
|
+
Ruby
|
23
|
+
|
24
|
+
## Development
|
25
|
+
|
26
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
27
|
+
|
28
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ryangoh/aka2.
|
33
|
+
|
34
|
+
|
35
|
+
## License
|
36
|
+
|
37
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/aka.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'aka/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "aka2"
|
8
|
+
spec.version = Aka::VERSION
|
9
|
+
spec.authors = ["Bryan Lim","Ryan Goh"]
|
10
|
+
spec.email = ["ytbryan@gmail.com","gohengkeat89@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{The Missing Alias Manager}
|
13
|
+
spec.description = %q{aka generate/edit/destroy/find permanent aliases with a single command. }
|
14
|
+
spec.homepage = "https://github.com/ytbryan/aka-gem"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
#spec.bindir = "exe"
|
19
|
+
#spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.executables = ["aka"]
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.required_ruby_version = ">= 2.2.2"
|
24
|
+
|
25
|
+
|
26
|
+
spec.add_runtime_dependency 'net-scp', '~> 1.2', '>= 1.2.1'
|
27
|
+
# spec.add_runtime_dependency 'open-uri'
|
28
|
+
spec.add_runtime_dependency 'colorize', '~> 0'
|
29
|
+
spec.add_runtime_dependency "safe_yaml", '~> 1.0', '>= 1.0.4'
|
30
|
+
spec.add_runtime_dependency 'thor' , '~> 0.19.1'
|
31
|
+
# spec.add_runtime_dependency 'fileutils'
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "minitest", "~> 5.7"
|
36
|
+
end
|
data/bin/aka
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "aka"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/.config
ADDED
data/lib/aka.rb
ADDED
@@ -0,0 +1,1188 @@
|
|
1
|
+
require "aka/version"
|
2
|
+
require 'net/scp'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'colorize'
|
5
|
+
require "safe_yaml/load"
|
6
|
+
require 'thor'
|
7
|
+
require 'fileutils'
|
8
|
+
|
9
|
+
module Aka
|
10
|
+
class Base < Thor
|
11
|
+
check_unknown_options!
|
12
|
+
package_name "aka"
|
13
|
+
default_task :list
|
14
|
+
map "dl" => "download",
|
15
|
+
"g" => "generate",
|
16
|
+
"d" => "destroy",
|
17
|
+
"f" => "find",
|
18
|
+
"up" => "upload",
|
19
|
+
"u" => "usage",
|
20
|
+
"l" => "list",
|
21
|
+
"e" => "edit",
|
22
|
+
"c" => "clean",
|
23
|
+
"h" => "help"
|
24
|
+
|
25
|
+
#
|
26
|
+
# DOWNLOAD
|
27
|
+
# aka download --to ~/Desktop/ --login admin@162.243.249.154:22000 --from /home/admin/hello desc "download [path]", "download a dot file"
|
28
|
+
desc "download", "download dotfile from server"
|
29
|
+
method_options :from => :string
|
30
|
+
method_options :to => :string
|
31
|
+
method_options :login => :string
|
32
|
+
def download
|
33
|
+
if options.from and options.to and options.login
|
34
|
+
success = true
|
35
|
+
arr = split(options.login)
|
36
|
+
pw = get_password()
|
37
|
+
begin
|
38
|
+
result = Net::SCP.download!(arr[1], #remote
|
39
|
+
arr.first,#username
|
40
|
+
options.from, #remote_path
|
41
|
+
options.to, #local_path
|
42
|
+
:ssh => {:password => pw,
|
43
|
+
:port => arr[2]})
|
44
|
+
rescue Exception => e
|
45
|
+
puts "\n#{e}"
|
46
|
+
success = false
|
47
|
+
end
|
48
|
+
puts "\nDone." if success
|
49
|
+
else
|
50
|
+
puts "Some options are missing."
|
51
|
+
puts "--login: #{options.login}"
|
52
|
+
puts "--to: #{options.to}"
|
53
|
+
puts "--from: #{options.from}"
|
54
|
+
puts "aka dl --login some_login_string --to some_local_path_string --from some_remote_path_string"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
#
|
59
|
+
# UPLOAD
|
60
|
+
#
|
61
|
+
#aka upload --from ~/Desktop/TwitterAPI.rb --login admin@162.243.249.154:22000 --to /home/admin
|
62
|
+
desc "upload", "upload a dot file"
|
63
|
+
method_options :from => :string
|
64
|
+
method_options :to => :string
|
65
|
+
method_options :login => :string
|
66
|
+
def upload
|
67
|
+
if options.from and options.to and options.login
|
68
|
+
password = get_password()
|
69
|
+
|
70
|
+
if File.exists?(options.from)
|
71
|
+
success = true
|
72
|
+
begin
|
73
|
+
arr = split(options.login)
|
74
|
+
result = Net::SCP.upload!(arr[1], #remote
|
75
|
+
arr.first, #username
|
76
|
+
options.from, #local_path
|
77
|
+
options.to, #remote_path
|
78
|
+
:ssh => {:port => arr[2],
|
79
|
+
:password => password})
|
80
|
+
rescue Exception => e
|
81
|
+
puts "\n#{e}"
|
82
|
+
success = false
|
83
|
+
end
|
84
|
+
|
85
|
+
puts "\nDone." if success
|
86
|
+
else
|
87
|
+
puts "Cannot find #{options.from}"
|
88
|
+
end
|
89
|
+
else
|
90
|
+
puts "Some options are missing:"
|
91
|
+
puts "--of -> #{options.of}"
|
92
|
+
puts "--to -> #{options.to}"
|
93
|
+
puts "--from -> #{options.from}"
|
94
|
+
puts "aka up --of some_string --to some_string --from some_string"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
#
|
99
|
+
# GENERATE
|
100
|
+
#
|
101
|
+
desc "generate", "generate an alias (short alias: g)"
|
102
|
+
method_options :last => :boolean
|
103
|
+
def generate args
|
104
|
+
result = false
|
105
|
+
if options.last?
|
106
|
+
result = add(add_last_command(parseARGS(args))) if args
|
107
|
+
else
|
108
|
+
result = add(parseARGS(args)) if args
|
109
|
+
if options.proj? and result == true
|
110
|
+
FileUtils.touch("#{Dir.pwd}/.aka")
|
111
|
+
add_to_proj(args)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
reload_dot_file if result == true and !options.noreload
|
115
|
+
end
|
116
|
+
|
117
|
+
#
|
118
|
+
# DESTROY
|
119
|
+
#
|
120
|
+
desc "destroy", "destroy an alias (short alias: d)"
|
121
|
+
method_options :force => :boolean
|
122
|
+
def destroy(*args)
|
123
|
+
args.each_with_index do |value, index|
|
124
|
+
result = remove(value)
|
125
|
+
unalias_the(value) if !options.nounalias and result == true
|
126
|
+
reload_dot_file if result == true and !options.noreload
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
#
|
131
|
+
# SETUP
|
132
|
+
#
|
133
|
+
desc "setup", "setup aka"
|
134
|
+
method_options :force => :boolean
|
135
|
+
def setup
|
136
|
+
setup_aka
|
137
|
+
end
|
138
|
+
|
139
|
+
#
|
140
|
+
# first step: set config file
|
141
|
+
#
|
142
|
+
desc "setup2", "Gem - Setup aka"
|
143
|
+
method_options :force => :boolean
|
144
|
+
def setupconfig
|
145
|
+
configDir = "#{Dir.home}/.aka"
|
146
|
+
if File.exist?("#{configDir}/.config")
|
147
|
+
# list
|
148
|
+
puts "config file is exist in #{configDir}"
|
149
|
+
else
|
150
|
+
setup_config #create and setup .config file
|
151
|
+
setup_aka2 #setup aka
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
#
|
156
|
+
# FIND
|
157
|
+
#
|
158
|
+
desc "find", "find an alias (short alias: f)"
|
159
|
+
method_options :force => :boolean
|
160
|
+
def find *args
|
161
|
+
args.each_with_index do |value, index|
|
162
|
+
show_alias(value)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
#
|
167
|
+
# EDIT
|
168
|
+
#
|
169
|
+
desc "edit", "edit an alias(short alias: e)"
|
170
|
+
method_options :force => :boolean
|
171
|
+
def edit args
|
172
|
+
if args
|
173
|
+
values = args.split("=")
|
174
|
+
if values.size > 1
|
175
|
+
truth, _alias = show_alias(args)
|
176
|
+
if truth == true
|
177
|
+
if options.name
|
178
|
+
remove(_alias) #remove that alias
|
179
|
+
edit_alias(values[1], _alias) #edit that alias
|
180
|
+
reload_dot_file() if !options.noreload
|
181
|
+
else
|
182
|
+
remove(_alias) #remove that alias
|
183
|
+
edit_this(values[1], _alias) #edit that alias
|
184
|
+
reload_dot_file() if !options.noreload
|
185
|
+
end
|
186
|
+
else
|
187
|
+
puts "Alias '#{args}' cannot be found".red
|
188
|
+
end
|
189
|
+
else
|
190
|
+
truth, _alias, command = show_alias(args)
|
191
|
+
if truth == true
|
192
|
+
if options.name
|
193
|
+
input = ask "Enter a new alias for command '#{command}'?\n"
|
194
|
+
if yes? "Please confirm the new alias? (y/N)"
|
195
|
+
remove(_alias) #remove that alias
|
196
|
+
edit_alias(input, _alias) #edit that alias
|
197
|
+
reload_dot_file() if !options.noreload
|
198
|
+
end
|
199
|
+
else
|
200
|
+
input = ask "Enter a new command for alias '#{args}'?\n"
|
201
|
+
if yes? "Please confirm the new command? (y/N)"
|
202
|
+
remove(_alias) #remove that alias
|
203
|
+
edit_this(input, _alias) #edit that alias
|
204
|
+
reload_dot_file() if !options.noreload
|
205
|
+
end
|
206
|
+
end
|
207
|
+
else
|
208
|
+
puts "Alias '#{args}' cannot be found".red
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
end #if args
|
213
|
+
end
|
214
|
+
|
215
|
+
#
|
216
|
+
# LIST OUT
|
217
|
+
#
|
218
|
+
desc "list", "list alias (short alias: l)"
|
219
|
+
method_options :force => :boolean
|
220
|
+
def list(args=nil)
|
221
|
+
if args != nil
|
222
|
+
showlast(args.to_i)
|
223
|
+
else
|
224
|
+
value = readYML("#{Dir.home}/.aka/.config")["list"]
|
225
|
+
showlast(value.to_i) #this is unsafe
|
226
|
+
end
|
227
|
+
|
228
|
+
#total of #{} exports #functions
|
229
|
+
puts "A total of #{count()} aliases,#{count_export} exports and #{count_function} functions from #{readYML("#{Dir.home}/.aka/.config")["dotfile"]}"
|
230
|
+
reload_dot_file
|
231
|
+
end
|
232
|
+
|
233
|
+
#
|
234
|
+
# LIST OUT - ryan - remove numbering
|
235
|
+
#
|
236
|
+
desc "list2", "list alias (short alias: l)"
|
237
|
+
method_options :force => :boolean
|
238
|
+
def list2(args=nil)
|
239
|
+
if args != nil
|
240
|
+
showlast2(args.to_i)
|
241
|
+
else
|
242
|
+
value = readYML("#{Dir.home}/.aka/.config")["list"]
|
243
|
+
showlast2(value.to_i) #this is unsafe
|
244
|
+
end
|
245
|
+
|
246
|
+
#total of #{} exports #functions
|
247
|
+
puts "A total of #{count()} aliases,#{count_export} exports and #{count_function} functions from #{readYML("#{Dir.home}/.aka/.config")["dotfile"]}"
|
248
|
+
reload_dot_file
|
249
|
+
puts "\nUse 'aka -h' to see all the useful commands.\n\n"
|
250
|
+
end
|
251
|
+
|
252
|
+
#
|
253
|
+
# USAGE
|
254
|
+
#
|
255
|
+
desc "usage [number]", "show commands usage based on history"
|
256
|
+
# method_options :least, :type => :boolean, :aliases => '-l', :desc => 'show the least used commands'
|
257
|
+
# method_options :clear, :type => :boolean, :aliases => '-c', :desc => 'clear the dot history file'
|
258
|
+
def usage(args=nil)
|
259
|
+
if args
|
260
|
+
if options.least
|
261
|
+
showUsage(args.to_i, true) if args
|
262
|
+
else
|
263
|
+
showUsage(args.to_i) if args
|
264
|
+
end
|
265
|
+
else
|
266
|
+
if options.least
|
267
|
+
value = readYML("#{Dir.home}/.aka/.config")["usage"]
|
268
|
+
showlast(value.to_i, true) #this is unsafe
|
269
|
+
else
|
270
|
+
value = readYML("#{Dir.home}/.aka/.config")["usage"]
|
271
|
+
showlast(value.to_i) #this is unsafe
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
if options[:clear]
|
276
|
+
puts "clear the dot history file"
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
#
|
281
|
+
# USAGE2 - ryan - remove numbering in front
|
282
|
+
#
|
283
|
+
desc "usage2 [number]", "show commands usage based on history"
|
284
|
+
# method_options :least, :type => :boolean, :aliases => '-l', :desc => 'show the least used commands'
|
285
|
+
# method_options :clear, :type => :boolean, :aliases => '-c', :desc => 'clear the dot history file'
|
286
|
+
def usage2(args=nil)
|
287
|
+
if args
|
288
|
+
if options.least
|
289
|
+
showUsage(args.to_i, true) if args
|
290
|
+
else
|
291
|
+
showUsage(args.to_i) if args
|
292
|
+
end
|
293
|
+
else
|
294
|
+
if options.least
|
295
|
+
value = readYML("#{Dir.home}/.aka/.config")["usage"]
|
296
|
+
showlast2(value.to_i, true) #this is unsafe
|
297
|
+
else
|
298
|
+
value = readYML("#{Dir.home}/.aka/.config")["usage"]
|
299
|
+
showlast2(value.to_i) #this is unsafe
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
if options[:clear]
|
304
|
+
puts "clear the dot history file"
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
|
309
|
+
#
|
310
|
+
# INSTALL
|
311
|
+
#
|
312
|
+
desc "install [name]", "install aka"
|
313
|
+
method_options :force => :boolean
|
314
|
+
def install
|
315
|
+
if File.exist? "#{Dir.pwd}/aka"
|
316
|
+
if File.exist? "/usr/local/bin/aka"
|
317
|
+
if yes? "aka exists. Do you want to replace it? (yN)"
|
318
|
+
FileUtils.rm("/usr/local/bin/aka")
|
319
|
+
system("ln -s #{Dir.pwd}/aka /usr/local/bin/aka")
|
320
|
+
puts "aka replaced."
|
321
|
+
end
|
322
|
+
else
|
323
|
+
result = system("ln -s #{Dir.pwd}/aka /usr/local/bin/aka")
|
324
|
+
puts "aka installed."
|
325
|
+
end
|
326
|
+
else
|
327
|
+
puts "Cannot find aka.".red
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
#
|
332
|
+
# INIT
|
333
|
+
#
|
334
|
+
desc "init", "setup aka"
|
335
|
+
method_options :dotfile => :string
|
336
|
+
method_options :history => :string
|
337
|
+
method_options :home => :string
|
338
|
+
method_options :install => :string
|
339
|
+
method_options :profile => :string
|
340
|
+
method_options :list => :numeric
|
341
|
+
method_options :usage => :numeric
|
342
|
+
method_options :remote => :string
|
343
|
+
method_options :config => :boolean
|
344
|
+
method_options :zshrc => :boolean
|
345
|
+
method_options :bashrc => :boolean
|
346
|
+
method_options :bash => :boolean
|
347
|
+
|
348
|
+
def init
|
349
|
+
if options.count < 1
|
350
|
+
setup
|
351
|
+
else
|
352
|
+
setZSHRC if options[:zshrc]
|
353
|
+
setBASHRC if options[:bashrc]
|
354
|
+
setBASH if options[:bash]
|
355
|
+
|
356
|
+
showConfig if options[:config]
|
357
|
+
setPath(options[:dotfile],"dotfile") if options[:dotfile]
|
358
|
+
setPath(options[:history],"history") if options[:history]
|
359
|
+
setPath(options[:home],"home") if options[:home]
|
360
|
+
setPath(options[:install],"install") if options[:install]
|
361
|
+
setPath(options[:profile],"profile") if options[:profile]
|
362
|
+
setPath(options[:list],"list") if options[:list]
|
363
|
+
setPath(options[:usage],"usage") if options[:usage]
|
364
|
+
setPath(options[:remote],"remote") if options[:remote]
|
365
|
+
end
|
366
|
+
end
|
367
|
+
|
368
|
+
#
|
369
|
+
# CLEAN
|
370
|
+
#
|
371
|
+
desc "clean", "perform cleanup"
|
372
|
+
def clean
|
373
|
+
cleanup
|
374
|
+
end
|
375
|
+
|
376
|
+
#
|
377
|
+
# PRIVATE METHODS
|
378
|
+
#
|
379
|
+
|
380
|
+
private
|
381
|
+
|
382
|
+
# set path
|
383
|
+
def setPath(path, value)
|
384
|
+
data = readYML("#{Dir.home}/.aka/.config")
|
385
|
+
if data.has_key?(value) == true
|
386
|
+
old_path = data[value]
|
387
|
+
data[value] = path
|
388
|
+
writeYML("#{Dir.home}/.aka/.config", data)
|
389
|
+
puts "#{value} -> #{path}"
|
390
|
+
else
|
391
|
+
puts "error: --#{value} does not exist in #{Dir.home}/.aka/.config "
|
392
|
+
end
|
393
|
+
end
|
394
|
+
|
395
|
+
# reload
|
396
|
+
def reload
|
397
|
+
system "source #{readYML("#{Dir.home}/.aka/.config")["dotfile"]}"
|
398
|
+
end
|
399
|
+
|
400
|
+
# read YML
|
401
|
+
def readYML path
|
402
|
+
if File.exists? path
|
403
|
+
return YAML.load_file(path)
|
404
|
+
else
|
405
|
+
puts "#{Dir.home}/.aka/.config does not exist. You need to create one, type `touch ~/.aka/.config` and copy https://github.com/ytbryan/aka/blob/master/.config".red
|
406
|
+
end
|
407
|
+
end
|
408
|
+
|
409
|
+
# write YML
|
410
|
+
def writeYML path, theyml
|
411
|
+
File.open(path, 'w') {|f| f.write theyml.to_yaml } #Store
|
412
|
+
end
|
413
|
+
|
414
|
+
# write_with
|
415
|
+
def write_with array
|
416
|
+
str = checkConfigFile(readYML("#{Dir.home}/.aka/.config")["dotfile"])
|
417
|
+
|
418
|
+
File.open(str, 'w') { |file|
|
419
|
+
array.each do |line|
|
420
|
+
file.write(line)
|
421
|
+
end
|
422
|
+
}
|
423
|
+
end
|
424
|
+
|
425
|
+
# write_with_newline
|
426
|
+
def write_with_newline array
|
427
|
+
str = checkConfigFile(readYML("#{Dir.home}/.aka/.config")["dotfile"])
|
428
|
+
|
429
|
+
File.open(str, 'w') { |file|
|
430
|
+
array.each do |line|
|
431
|
+
file.write(line + "\n")
|
432
|
+
end
|
433
|
+
}
|
434
|
+
end
|
435
|
+
|
436
|
+
# write
|
437
|
+
def write str, path
|
438
|
+
File.open(path, 'w') { |file| file.write(str) }
|
439
|
+
end
|
440
|
+
|
441
|
+
# append
|
442
|
+
def append str, path
|
443
|
+
File.open(path, 'a') { |file| file.write(str) }
|
444
|
+
end
|
445
|
+
|
446
|
+
#append_with_newline
|
447
|
+
def append_with_newline str, path
|
448
|
+
File.open(path, 'a') { |file| file.write(str + "\n") }
|
449
|
+
end
|
450
|
+
|
451
|
+
# reload_dot_file
|
452
|
+
def reload_dot_file
|
453
|
+
if isOhMyZsh == true
|
454
|
+
system("exec zsh")
|
455
|
+
else
|
456
|
+
system("kill -SIGUSR1 #{Process.ppid}")
|
457
|
+
end
|
458
|
+
end
|
459
|
+
|
460
|
+
# history write
|
461
|
+
def historywrite
|
462
|
+
if isOhMyZsh == true
|
463
|
+
system("exec zsh")
|
464
|
+
else
|
465
|
+
system "kill -SIGUSR2 #{Process.ppid}"
|
466
|
+
end
|
467
|
+
end
|
468
|
+
|
469
|
+
# unalias
|
470
|
+
def unalias_the value
|
471
|
+
if isOhMyZsh == true
|
472
|
+
system("exec zsh")
|
473
|
+
else
|
474
|
+
system "echo '#{value}' > ~/sigusr1-args;"
|
475
|
+
system "kill -SIGUSR2 #{Process.ppid}"
|
476
|
+
end
|
477
|
+
end
|
478
|
+
|
479
|
+
#split domain user
|
480
|
+
def split_domain_user fulldomain
|
481
|
+
username = fulldomain.split("@").first
|
482
|
+
domain = fulldomain.split("@")[1]
|
483
|
+
return [username, domain]
|
484
|
+
end
|
485
|
+
|
486
|
+
# split
|
487
|
+
def split fulldomain
|
488
|
+
username = fulldomain.split("@").first
|
489
|
+
domain = fulldomain.split("@")[1].split(":").first
|
490
|
+
port = fulldomain.split("@")[1].split(":")[1]
|
491
|
+
return [username, domain, port]
|
492
|
+
end
|
493
|
+
|
494
|
+
# add
|
495
|
+
def add input
|
496
|
+
if input and show_alias(input).first == false and not_empty_alias(input) == false
|
497
|
+
array = input.split("=")
|
498
|
+
full_command = "alias #{array.first}='#{array[1]}'".gsub("\n","") #remove new line in command
|
499
|
+
print_out_command = "aka g #{array.first}='#{array[1]}'"
|
500
|
+
|
501
|
+
str = checkConfigFile(readYML("#{Dir.home}/.aka/.config")["dotfile"])
|
502
|
+
|
503
|
+
File.open(str, 'a') { |file| file.write("\n" +full_command) }
|
504
|
+
puts "#{print_out_command} is added to #{readYML("#{Dir.home}/.aka/.config")["dotfile"]}"
|
505
|
+
return true
|
506
|
+
else
|
507
|
+
puts "The alias is already present."
|
508
|
+
return false
|
509
|
+
end
|
510
|
+
end
|
511
|
+
|
512
|
+
# not empty alias
|
513
|
+
def not_empty_alias input
|
514
|
+
array = input.split("=")
|
515
|
+
return true if array.count < 2
|
516
|
+
return array[1].strip == ""
|
517
|
+
end
|
518
|
+
|
519
|
+
# show alias
|
520
|
+
def show_alias argument
|
521
|
+
str = checkConfigFile(readYML("#{Dir.home}/.aka/.config")["dotfile"])
|
522
|
+
|
523
|
+
if content = File.open(str).read
|
524
|
+
content.gsub!(/\r\n?/, "\n")
|
525
|
+
content_array = content.split("\n")
|
526
|
+
content_array.each_with_index { |line, index|
|
527
|
+
value = line.split(" ")
|
528
|
+
if value.length > 1 and value.first == "alias"
|
529
|
+
answer = value[1].split("=")
|
530
|
+
if found?(answer.first, argument.split("=").first, line) == true
|
531
|
+
return [true, answer.first, answer[1]]
|
532
|
+
end
|
533
|
+
end
|
534
|
+
}
|
535
|
+
else
|
536
|
+
puts "#{@pwd} cannot be found.".red
|
537
|
+
return [false, nil, nil]
|
538
|
+
end
|
539
|
+
return [false, nil, nil]
|
540
|
+
|
541
|
+
end
|
542
|
+
|
543
|
+
# remove
|
544
|
+
def remove input
|
545
|
+
|
546
|
+
str = checkConfigFile(readYML("#{Dir.home}/.aka/.config")["dotfile"])
|
547
|
+
|
548
|
+
if content=File.open(str).read
|
549
|
+
content.gsub!(/\r\n?/, "\n")
|
550
|
+
content_array= content.split("\n")
|
551
|
+
content_array.each_with_index { |line, index|
|
552
|
+
value = line.split(" ")
|
553
|
+
if value.length > 1 and value.first == "alias"
|
554
|
+
answer = value[1].split("=")
|
555
|
+
if answer.first == input
|
556
|
+
content_array.delete_at(index) and write_with_newline(content_array)
|
557
|
+
print_out_command = "aka g #{input}=#{line.split("=")[1]}"
|
558
|
+
puts "removed: #{print_out_command} is removed from #{str}".red
|
559
|
+
return true
|
560
|
+
end
|
561
|
+
end
|
562
|
+
}
|
563
|
+
|
564
|
+
puts "#{input} cannot be found.".red
|
565
|
+
else
|
566
|
+
puts "#{@pwd} cannot be found.".red
|
567
|
+
return false
|
568
|
+
end
|
569
|
+
end
|
570
|
+
|
571
|
+
# history
|
572
|
+
def history
|
573
|
+
|
574
|
+
str = checkConfigFile(readYML("#{Dir.home}/.aka/.config")["history"])
|
575
|
+
|
576
|
+
if content = File.open(str).read
|
577
|
+
puts ".bash_history is available"
|
578
|
+
count=0
|
579
|
+
content.gsub!(/\r\n?/, "\n")
|
580
|
+
content_array = content.split("\n")
|
581
|
+
content_array.each_with_index { |line, index|
|
582
|
+
array = line.split(" ")
|
583
|
+
if array.first == "alias"
|
584
|
+
count += 1
|
585
|
+
end
|
586
|
+
puts "#{index+1} #{line}"
|
587
|
+
}
|
588
|
+
puts "There are #{count} lines of history."
|
589
|
+
else
|
590
|
+
puts ".bash_history is not available".red
|
591
|
+
end
|
592
|
+
end
|
593
|
+
|
594
|
+
# check version
|
595
|
+
def version
|
596
|
+
puts ""
|
597
|
+
puts "aka #{program(:version)} - #{program(:last_update)}"
|
598
|
+
puts "#{program(:author)} - #{program(:contact)}"
|
599
|
+
puts "https://github.com/ytbryan/aka"
|
600
|
+
end
|
601
|
+
|
602
|
+
# check found
|
603
|
+
def found? answer, argument, line
|
604
|
+
if answer == argument
|
605
|
+
# puts line.red + " - aka add #{argument}"
|
606
|
+
# puts line.red
|
607
|
+
puts "found: aka g #{argument}=#{line.split('=')[1]}".red
|
608
|
+
return true
|
609
|
+
else
|
610
|
+
return false
|
611
|
+
end
|
612
|
+
end
|
613
|
+
|
614
|
+
# edit_this
|
615
|
+
def edit_this newcommand, this_alias
|
616
|
+
puts "new: aka g #{this_alias}='#{newcommand}'"
|
617
|
+
return append("alias " + this_alias + "='" + newcommand + "'", readYML("#{Dir.home}/.aka/.config")["dotfile"] )
|
618
|
+
end
|
619
|
+
|
620
|
+
# edit alias
|
621
|
+
def edit_alias newalias, thiscommand
|
622
|
+
puts "new: aka g #{this_alias}='#{newcommand}'"
|
623
|
+
return append("alias " + newalias + "='" + thiscommand + "'", readYML("#{Dir.home}/.aka/.config")["dotfile"] )
|
624
|
+
end
|
625
|
+
|
626
|
+
# count function
|
627
|
+
def count_function
|
628
|
+
function_count = 0
|
629
|
+
str = checkConfigFile(readYML("#{Dir.home}/.aka/.config")["dotfile"])
|
630
|
+
if content=File.open(str).read
|
631
|
+
content.gsub!(/\r\n?/, "\n")
|
632
|
+
content_array= content.split("\n")
|
633
|
+
content_array.each_with_index { |line, index|
|
634
|
+
value = line.split(" ")
|
635
|
+
if value.length > 1 and value.first == "function"
|
636
|
+
answer = value[1].split("=")
|
637
|
+
function_count += 1
|
638
|
+
end
|
639
|
+
}
|
640
|
+
return function_count
|
641
|
+
end
|
642
|
+
end
|
643
|
+
|
644
|
+
#count export
|
645
|
+
def count_export
|
646
|
+
export_count = 0
|
647
|
+
str = checkConfigFile(readYML("#{Dir.home}/.aka/.config")["dotfile"])
|
648
|
+
if content=File.open(str).read
|
649
|
+
content.gsub!(/\r\n?/, "\n")
|
650
|
+
content_array= content.split("\n")
|
651
|
+
content_array.each_with_index { |line, index|
|
652
|
+
value = line.split(" ")
|
653
|
+
if value.length > 1 and value.first == "export"
|
654
|
+
answer = value[1].split("=")
|
655
|
+
export_count += 1
|
656
|
+
end
|
657
|
+
}
|
658
|
+
return export_count
|
659
|
+
end
|
660
|
+
|
661
|
+
end
|
662
|
+
|
663
|
+
# count
|
664
|
+
def count
|
665
|
+
alias_count = 0
|
666
|
+
|
667
|
+
str = checkConfigFile(readYML("#{Dir.home}/.aka/.config")["dotfile"])
|
668
|
+
|
669
|
+
if content=File.open(str).read
|
670
|
+
content.gsub!(/\r\n?/, "\n")
|
671
|
+
content_array= content.split("\n")
|
672
|
+
content_array.each_with_index { |line, index|
|
673
|
+
value = line.split(" ")
|
674
|
+
if value.length > 1 and value.first == "alias"
|
675
|
+
answer = value[1].split("=")
|
676
|
+
alias_count += 1
|
677
|
+
end
|
678
|
+
}
|
679
|
+
return alias_count
|
680
|
+
end
|
681
|
+
end
|
682
|
+
|
683
|
+
# setup_aka
|
684
|
+
def setup_aka
|
685
|
+
append_with_newline("export HISTSIZE=10000","/etc/profile")
|
686
|
+
trap = "sigusr2() { unalias $1;}
|
687
|
+
sigusr1() { source #{readYML("#{Dir.home}/.aka/.config")["dotfile"]}; history -a; echo 'reloaded dot file'; }
|
688
|
+
trap sigusr1 SIGUSR1
|
689
|
+
trap 'sigusr2 $(cat ~/sigusr1-args)' SIGUSR2\n".pretty
|
690
|
+
append(trap, readYML("#{Dir.home}/.aka/.config")['profile'])
|
691
|
+
puts "Done. Please restart this shell.".red
|
692
|
+
end
|
693
|
+
|
694
|
+
# setup_aka2 by ryan - check bash file first
|
695
|
+
def setup_aka2
|
696
|
+
if File.exist?("#{Dir.home}/.zshrc") #if zshec exist
|
697
|
+
setZSHRC2
|
698
|
+
append_with_newline("export HISTSIZE=10000","#{Dir.home}/.zshrc")
|
699
|
+
elsif
|
700
|
+
File.exist?("#{Dir.home}/.bashrc") #if bashrc exist
|
701
|
+
setBASHRC2
|
702
|
+
append_with_newline("export HISTSIZE=10000","#{Dir.home}/.bashrc")
|
703
|
+
elsif File.exist?("#{Dir.home}/.bash_profile") #if bash_profile exist
|
704
|
+
setBASH2
|
705
|
+
append_with_newline("export HISTSIZE=10000","#{Dir.home}/.bash_profile")
|
706
|
+
else
|
707
|
+
puts "Currently aka2 just support zshrc, bashrc and bash_profile"
|
708
|
+
puts "Pleaes contact aka2 creator for more info."
|
709
|
+
end
|
710
|
+
|
711
|
+
trap = "sigusr2() { unalias $1;}
|
712
|
+
sigusr1() { source #{readYML("#{Dir.home}/.aka/.config")["dotfile"]}; history -a; echo 'reloaded dot file'; }
|
713
|
+
trap sigusr1 SIGUSR1
|
714
|
+
trap 'sigusr2 $(cat ~/sigusr1-args)' SIGUSR2\n".pretty
|
715
|
+
append(trap, readYML("#{Dir.home}/.aka/.config")['profile'])
|
716
|
+
puts "Done. Please restart this shell.".red
|
717
|
+
end
|
718
|
+
|
719
|
+
# create and setup config file
|
720
|
+
def setup_config
|
721
|
+
configDir = "#{Dir.home}/.aka"
|
722
|
+
if File.exist?("#{configDir}/.config")
|
723
|
+
puts "Directory #{configDir}/.config exist"
|
724
|
+
else
|
725
|
+
FileUtils::mkdir_p("#{configDir}")
|
726
|
+
FileUtils.cp("./lib/.config", "#{configDir}")
|
727
|
+
end
|
728
|
+
end
|
729
|
+
|
730
|
+
# write to location
|
731
|
+
def write_to_location location, address
|
732
|
+
if aka_directory_exists?
|
733
|
+
write(location, address)
|
734
|
+
else
|
735
|
+
puts ".aka not found.".red
|
736
|
+
end
|
737
|
+
end
|
738
|
+
|
739
|
+
# read location
|
740
|
+
def read location
|
741
|
+
answer = dot_location_exists?(location)
|
742
|
+
if answer == true and content = File.open(location).read
|
743
|
+
return content
|
744
|
+
end
|
745
|
+
return ""
|
746
|
+
end
|
747
|
+
|
748
|
+
# dot location exist
|
749
|
+
def dot_location_exists? address
|
750
|
+
return File.exist? address
|
751
|
+
end
|
752
|
+
|
753
|
+
# aka directory exist ?
|
754
|
+
def aka_directory_exists?
|
755
|
+
return File.directory?("#{Dir.home}/.aka")
|
756
|
+
end
|
757
|
+
|
758
|
+
# check config file
|
759
|
+
def checkConfigFile str
|
760
|
+
path = "#{Dir.home}/.bash_profile"
|
761
|
+
if str == ""
|
762
|
+
puts "Error: Type `aka init --dotfile #{path}` to set the path to your dotfile. \nReplace .bash_profile with .bashrc or .zshrc if you are not using bash.".red
|
763
|
+
exit()
|
764
|
+
end
|
765
|
+
|
766
|
+
if !File.exists?(str)
|
767
|
+
puts "Error: Type `aka init --dotfile #{path}` to set the path of your dotfile. \nReplace .bash_profile with .bashrc or .zshrc if you are not using bash.".red
|
768
|
+
exit()
|
769
|
+
end
|
770
|
+
|
771
|
+
return str
|
772
|
+
end
|
773
|
+
|
774
|
+
# show last
|
775
|
+
def showlast howmany=10
|
776
|
+
str = checkConfigFile(readYML("#{Dir.home}/.aka/.config")["dotfile"])
|
777
|
+
|
778
|
+
if content = File.open(str).read
|
779
|
+
content.gsub!(/\r\n?/, "\n")
|
780
|
+
content_array = content.split("\n")
|
781
|
+
#why not just call the last five lines? Because i can't be sure that they are aliases
|
782
|
+
total_aliases = []
|
783
|
+
content_array.each_with_index { |line, index|
|
784
|
+
value = line.split(" ")
|
785
|
+
if value.length > 1 and value.first == "alias"
|
786
|
+
total_aliases.push(line)
|
787
|
+
end
|
788
|
+
}
|
789
|
+
puts ""
|
790
|
+
if total_aliases.count > howmany
|
791
|
+
total_aliases.last(howmany).each_with_index do |line, index|
|
792
|
+
splitted= line.split('=')
|
793
|
+
puts "#{total_aliases.count - howmany + index+1}. aka g " + splitted[0].split(" ")[1] + "=" + splitted[1].red
|
794
|
+
# puts "#{total_aliases.count - howmany + index+1}. " + splitted[0] + "=" + splitted[1].red
|
795
|
+
end
|
796
|
+
else
|
797
|
+
total_aliases.last(howmany).each_with_index do |line, index|
|
798
|
+
splitted= line.split('=')
|
799
|
+
# puts "#{index+1}. " + splitted[0] + "=" + splitted[1].red
|
800
|
+
puts "#{index+1}. aka g " + splitted[0].split(" ")[1] + "=" + splitted[1].red
|
801
|
+
end
|
802
|
+
end
|
803
|
+
puts ""
|
804
|
+
end
|
805
|
+
end
|
806
|
+
|
807
|
+
# show last2 - ryan - remove number
|
808
|
+
def showlast2 howmany=10
|
809
|
+
str = checkConfigFile(readYML("#{Dir.home}/.aka/.config")["dotfile"])
|
810
|
+
|
811
|
+
if content = File.open(str).read
|
812
|
+
content.gsub!(/\r\n?/, "\n")
|
813
|
+
content_array = content.split("\n")
|
814
|
+
#why not just call the last five lines? Because i can't be sure that they are aliases
|
815
|
+
total_aliases = []
|
816
|
+
content_array.each_with_index { |line, index|
|
817
|
+
value = line.split(" ")
|
818
|
+
if value.length > 1 and value.first == "alias"
|
819
|
+
total_aliases.push(line)
|
820
|
+
end
|
821
|
+
}
|
822
|
+
puts ""
|
823
|
+
if total_aliases.count > howmany
|
824
|
+
total_aliases.last(howmany).each_with_index do |line, index|
|
825
|
+
splitted= line.split('=')
|
826
|
+
puts "aka g " + splitted[0].split(" ")[1] + "=" + splitted[1].red
|
827
|
+
# puts "#{total_aliases.count - howmany + index+1}. " + splitted[0] + "=" + splitted[1].red
|
828
|
+
end
|
829
|
+
else
|
830
|
+
total_aliases.last(howmany).each_with_index do |line, index|
|
831
|
+
splitted= line.split('=')
|
832
|
+
# puts "#{index+1}. " + splitted[0] + "=" + splitted[1].red
|
833
|
+
puts "aka g " + splitted[0].split(" ")[1] + "=" + splitted[1].red
|
834
|
+
end
|
835
|
+
end
|
836
|
+
puts ""
|
837
|
+
end
|
838
|
+
end
|
839
|
+
|
840
|
+
# show usage
|
841
|
+
def showUsage howmany=10, least=false
|
842
|
+
|
843
|
+
str = checkConfigFile(readYML("#{Dir.home}/.aka/.config")["history"])
|
844
|
+
|
845
|
+
value = reload_dot_file()
|
846
|
+
#get all aliases
|
847
|
+
if content = File.open(str).read
|
848
|
+
content.gsub!(/\r\n?/, "\n")
|
849
|
+
content_array = content.split("\n")
|
850
|
+
total_aliases = []
|
851
|
+
content_array.each_with_index { |line, index|
|
852
|
+
value = line.split(" ")
|
853
|
+
total_aliases.push(value.first)
|
854
|
+
}
|
855
|
+
count_aliases(total_aliases, howmany, least)
|
856
|
+
end
|
857
|
+
end
|
858
|
+
|
859
|
+
# count aliases
|
860
|
+
def count_aliases array, howmany, least=false
|
861
|
+
name_array,count_array = [], []
|
862
|
+
#find the unique value
|
863
|
+
array.each_with_index { |value, index|
|
864
|
+
if name_array.include?(value) == false
|
865
|
+
name_array.push(value)
|
866
|
+
end
|
867
|
+
}
|
868
|
+
#count the value
|
869
|
+
name_array.each { |unique_value|
|
870
|
+
count = 0
|
871
|
+
array.each { |value|
|
872
|
+
if (unique_value == value)
|
873
|
+
count+=1
|
874
|
+
end
|
875
|
+
}
|
876
|
+
count_array.push(count)
|
877
|
+
}
|
878
|
+
|
879
|
+
sorted_count_array, sorted_name_array = sort(count_array, name_array)
|
880
|
+
|
881
|
+
#display the least used aliases
|
882
|
+
if least == true
|
883
|
+
if sorted_count_array.count == sorted_name_array.count
|
884
|
+
puts ""
|
885
|
+
sorted_name_array.last(howmany).each_with_index { |value, index|
|
886
|
+
percent = ((sorted_count_array[sorted_name_array.last(howmany).size + index]).round(2)/array.size.round(2))*100
|
887
|
+
str = "#{sorted_name_array.size-sorted_name_array.last(howmany).size+index+1}. #{value}"
|
888
|
+
puts "#{str} #{showSpace(str)} #{showBar(percent)}"
|
889
|
+
}
|
890
|
+
puts ""
|
891
|
+
else
|
892
|
+
puts "Something went wrong: count_array.count = #{sorted_count_array.count}\n
|
893
|
+
name_array.count = #{sorted_name_array.count}. Please check your .bash_history.".pretty
|
894
|
+
end
|
895
|
+
else
|
896
|
+
# #print out
|
897
|
+
if sorted_count_array.count == sorted_name_array.count
|
898
|
+
puts ""
|
899
|
+
sorted_name_array.first(howmany).each_with_index { |value, index|
|
900
|
+
percent = ((sorted_count_array[index]).round(2)/array.size.round(2))*100
|
901
|
+
str = "#{index+1}. #{value}"
|
902
|
+
puts "#{str} #{showSpace(str)} #{showBar(percent)}"
|
903
|
+
}
|
904
|
+
puts ""
|
905
|
+
else
|
906
|
+
puts "Something went wrong: count_array.count = #{sorted_count_array.count}\n
|
907
|
+
name_array.count = #{sorted_name_array.count}. Please check your .bash_history.".pretty
|
908
|
+
end
|
909
|
+
end
|
910
|
+
puts "There's a total of #{array.size} lines in #{readYML("#{Dir.home}/.aka/.config")["history"]}."
|
911
|
+
end
|
912
|
+
|
913
|
+
# sort
|
914
|
+
def sort(countarray, namearray) #highest first. decscending.
|
915
|
+
temp = 0, temp2 = ""
|
916
|
+
countarray.each_with_index { |count, index|
|
917
|
+
countarray[0..countarray.size-index].each_with_index { |x, thisindex| #always one less than total
|
918
|
+
|
919
|
+
if index < countarray.size-1 and thisindex < countarray.size-1
|
920
|
+
if countarray[thisindex] < countarray[thisindex+1] #if this count is less than next count
|
921
|
+
temp = countarray[thisindex]
|
922
|
+
countarray[thisindex] = countarray[thisindex+1]
|
923
|
+
countarray[thisindex+1] = temp
|
924
|
+
|
925
|
+
temp2 = namearray[thisindex]
|
926
|
+
namearray[thisindex] = namearray[thisindex+1]
|
927
|
+
namearray[thisindex+1] = temp2
|
928
|
+
end
|
929
|
+
end
|
930
|
+
|
931
|
+
}
|
932
|
+
}#outer loop
|
933
|
+
return countarray, namearray
|
934
|
+
end
|
935
|
+
|
936
|
+
# get history file
|
937
|
+
def get_latest_history_file
|
938
|
+
system("history -a")
|
939
|
+
end
|
940
|
+
|
941
|
+
# clean up
|
942
|
+
def cleanup
|
943
|
+
|
944
|
+
str = checkConfigFile(readYML("#{Dir.home}/.aka/.config")["dotfile"])
|
945
|
+
|
946
|
+
if content = File.open(str).read
|
947
|
+
content.gsub!(/\r\n?/, "\n")
|
948
|
+
content_array = content.split("\n")
|
949
|
+
check = false
|
950
|
+
while check == false
|
951
|
+
check = true
|
952
|
+
content_array.each_with_index { |line, index|
|
953
|
+
if line == "" or line == "\n"
|
954
|
+
content_array.delete_at(index)
|
955
|
+
check = false
|
956
|
+
end
|
957
|
+
}
|
958
|
+
end
|
959
|
+
write_with_newline(content_array)
|
960
|
+
end
|
961
|
+
end
|
962
|
+
|
963
|
+
################################################
|
964
|
+
## Getting these babies ready for beauty contest
|
965
|
+
################################################
|
966
|
+
|
967
|
+
def showSpace word
|
968
|
+
space = ""
|
969
|
+
val = 20 - word.size
|
970
|
+
val = 20 if val < 0
|
971
|
+
val.times do
|
972
|
+
space += " "
|
973
|
+
end
|
974
|
+
return space
|
975
|
+
end
|
976
|
+
|
977
|
+
def showBar percent
|
978
|
+
result = ""
|
979
|
+
val = percent/100 * 50
|
980
|
+
val = 2 if val > 1 and val < 2
|
981
|
+
val = 1 if val.round <= 1 #for visibiity, show two bars if it's just one
|
982
|
+
val.round.times do
|
983
|
+
result += "+"
|
984
|
+
end
|
985
|
+
|
986
|
+
remaining = 50 - val.round
|
987
|
+
remaining.times do
|
988
|
+
result += "-".red
|
989
|
+
end
|
990
|
+
|
991
|
+
return result + " #{percent.round(2)}%"
|
992
|
+
end
|
993
|
+
|
994
|
+
|
995
|
+
# command :config do |c|
|
996
|
+
# c.action do |args, options|
|
997
|
+
# # theyml = Hash.new
|
998
|
+
# # theyml["location"] = "~/.aka/.location"
|
999
|
+
# # theyml["global"] = "/Users/ytbryan/.bash_profile"
|
1000
|
+
# # theyml["groups"] = "~/.aka/groups"
|
1001
|
+
# # theyml["remote"] = "255, admin, 12.12.12.233"
|
1002
|
+
# # theyml["path"] = "~/.aka"
|
1003
|
+
# # writeYML("#{Dir.home}/.aka/.config", theyml)
|
1004
|
+
# FileUtils.touch(readYML("#{Dir.home}/.aka/.config")["location"])
|
1005
|
+
# end
|
1006
|
+
# end
|
1007
|
+
#
|
1008
|
+
# command :readconfig do |c|
|
1009
|
+
# c.action do |args, options|
|
1010
|
+
# puts "readYML('#{Dir.home}/.aka/.config')['location'] ->#{readYML("#{Dir.home}/.aka/.config")["location"].class} -> #{readYML("#{Dir.home}/.aka/.config")["location"]}"
|
1011
|
+
# puts "readYML("#{Dir.home}/.aka/.config")["location"] -> #{ readYML("#{Dir.home}/.aka/.config")["location"].class} -> #{ readYML("#{Dir.home}/.aka/.config")["location"]}"
|
1012
|
+
# puts "#{program(:path_to_location).class} -> #{program(:path_to_location)}"
|
1013
|
+
# end
|
1014
|
+
# end
|
1015
|
+
#
|
1016
|
+
|
1017
|
+
###########################
|
1018
|
+
### DEVELOPMENT
|
1019
|
+
###########################
|
1020
|
+
|
1021
|
+
# command :build do |c|
|
1022
|
+
# c.syntax = 'aka build [options]'
|
1023
|
+
# c.summary = 'build the VERSION file and run tests'
|
1024
|
+
# c.action do |args, options|
|
1025
|
+
# write(program(:version), './VERSION')
|
1026
|
+
# puts "VERSION #{program(:version)} created at #{Time.now.strftime("%I:%M%p, %A, %d %b %Y")}"
|
1027
|
+
# end
|
1028
|
+
# end
|
1029
|
+
|
1030
|
+
# command :install do |c|
|
1031
|
+
# c.syntax = 'aka copy [options]'
|
1032
|
+
# c.summary = 'copy a local copy of aka to /usr/local/bin'
|
1033
|
+
# c.action do |args, options|
|
1034
|
+
# result = system("sudo cp aka /usr/local/bin")
|
1035
|
+
# puts "Installed aka #{program(:version)} into /usr/local/bin (#{Time.now.strftime("%I:%M%p,%a,%d %b %Y")}).".red if result == true
|
1036
|
+
# puts "" if result == true
|
1037
|
+
# end
|
1038
|
+
# end
|
1039
|
+
#
|
1040
|
+
# command :uninstall do |c|
|
1041
|
+
# c.syntax = 'aka uninstall [options]'
|
1042
|
+
# c.summary = 'uninstall aka'
|
1043
|
+
# c.action do |args, options|
|
1044
|
+
# input = ask "Confirm that you want to uninstall aka? (y/N)"
|
1045
|
+
# system("sudo rm -rf ~/.aka; sudo rm -rf /usr/local/bin/aka;") if input == "y"
|
1046
|
+
# end
|
1047
|
+
# end
|
1048
|
+
|
1049
|
+
def add_to_proj fullalias
|
1050
|
+
values = fullalias.split("=")
|
1051
|
+
yml = readYML("#{Dir.pwd}/.aka")
|
1052
|
+
if yml == false
|
1053
|
+
write_new_proj_aka_file fullalias
|
1054
|
+
else
|
1055
|
+
yml["proj"]["title"] = "this is title"
|
1056
|
+
yml["proj"]["summary"] = "this is summary"
|
1057
|
+
yml["proj"]["aka"][values.first] = values[1]
|
1058
|
+
writeYML("#{Dir.pwd}/.aka", yml)
|
1059
|
+
end
|
1060
|
+
end
|
1061
|
+
|
1062
|
+
def write_new_proj_aka_file fullalias
|
1063
|
+
values = fullalias.split("=")
|
1064
|
+
|
1065
|
+
theyml = {"proj" => {
|
1066
|
+
"title" => "",
|
1067
|
+
"summary" => "",
|
1068
|
+
"aliases" => {
|
1069
|
+
"firstvalue" => ""
|
1070
|
+
}}}
|
1071
|
+
|
1072
|
+
writeYML("#{Dir.pwd}/.aka", theyml)
|
1073
|
+
end
|
1074
|
+
|
1075
|
+
def createShortcut(proj)
|
1076
|
+
answer = ""
|
1077
|
+
proj["shortcuts"].to_a.each_with_index do |each,index|
|
1078
|
+
answer += "#{each["name"]}
|
1079
|
+
- #{each["command"]}
|
1080
|
+
".pretty
|
1081
|
+
answer += "\n"
|
1082
|
+
end
|
1083
|
+
return answer
|
1084
|
+
end
|
1085
|
+
|
1086
|
+
def add_last_command name
|
1087
|
+
command= find_last_command()
|
1088
|
+
return str = name + "=" + "#{command}"
|
1089
|
+
end
|
1090
|
+
|
1091
|
+
def find_last_command
|
1092
|
+
str = checkConfigFile(readYML("#{Dir.home}/.aka/.config")["history"])
|
1093
|
+
#i think if you do history -w, you can retrieve the latest command
|
1094
|
+
if content = File.open(str).read
|
1095
|
+
count=0
|
1096
|
+
content.gsub!(/\r\n?/, "\n")
|
1097
|
+
content_array = content.split("\n")
|
1098
|
+
return content_array[content_array.count - 1]
|
1099
|
+
end
|
1100
|
+
end
|
1101
|
+
|
1102
|
+
def parseARGS str
|
1103
|
+
array = str.split(" ")
|
1104
|
+
array.each_with_index do |line, value|
|
1105
|
+
array[value] = line.gsub('#{pwd}', Shellwords.escape(Dir.pwd))
|
1106
|
+
end
|
1107
|
+
return array.join(" ")
|
1108
|
+
end
|
1109
|
+
|
1110
|
+
def add_pwd_to_the_command
|
1111
|
+
#detect #{pwd}
|
1112
|
+
#get the pwd and replace the the pwd
|
1113
|
+
end
|
1114
|
+
|
1115
|
+
def showConfig
|
1116
|
+
thing = YAML.load_file("#{Dir.home}/.aka/.config")
|
1117
|
+
puts ""
|
1118
|
+
thing.each do |company,details|
|
1119
|
+
puts "#{company} -> " + "#{details}".red
|
1120
|
+
end
|
1121
|
+
end
|
1122
|
+
|
1123
|
+
def setZSHRC
|
1124
|
+
setPath("#{Dir.home}/.zshrc","dotfile")
|
1125
|
+
setPath("#{Dir.home}/.zsh_history","history")
|
1126
|
+
setPath("/etc/zprofile","profile")
|
1127
|
+
end
|
1128
|
+
|
1129
|
+
def setBASHRC
|
1130
|
+
setPath("#{Dir.home}/.bashrc","dotfile")
|
1131
|
+
setPath("#{Dir.home}/.bash_history","history")
|
1132
|
+
setPath("/etc/profile","profile")
|
1133
|
+
end
|
1134
|
+
|
1135
|
+
def setBASH
|
1136
|
+
setPath("#{Dir.home}/.bash_profile","dotfile")
|
1137
|
+
setPath("#{Dir.home}/.bash_history","history")
|
1138
|
+
setPath("/etc/profile","profile")
|
1139
|
+
end
|
1140
|
+
|
1141
|
+
def setZSHRC2 #ryan - set the right dotfile and profile
|
1142
|
+
setPath("#{Dir.home}/.zshrc","dotfile")
|
1143
|
+
setPath("#{Dir.home}/.zsh_history","history")
|
1144
|
+
setPath("#{Dir.home}/.zshrc","profile")
|
1145
|
+
setPath("#{Dir.home}/.aka","home")
|
1146
|
+
end
|
1147
|
+
|
1148
|
+
def setBASHRC2 #ryan - set the right dotfile and profile
|
1149
|
+
setPath("#{Dir.home}/.bashrc","dotfile")
|
1150
|
+
setPath("#{Dir.home}/.bash_history","history")
|
1151
|
+
setPath("#{Dir.home}/.bashrc","profile")
|
1152
|
+
setPath("#{Dir.home}/.aka","home")
|
1153
|
+
end
|
1154
|
+
|
1155
|
+
def setBASH2 #ryan - set the right dotfile and profile
|
1156
|
+
setPath("#{Dir.home}/.bash_profile","dotfile")
|
1157
|
+
setPath("#{Dir.home}/.bash_history","history")
|
1158
|
+
setPath("/etc/profile","profile")
|
1159
|
+
setPath("#{Dir.home}/.aka","home")
|
1160
|
+
end
|
1161
|
+
|
1162
|
+
def get_password
|
1163
|
+
ask("Enter Password: ", :echo => false)
|
1164
|
+
end
|
1165
|
+
|
1166
|
+
def isOhMyZsh
|
1167
|
+
if readYML("#{Dir.home}/.aka/.config")["dotfile"] == "#{Dir.home}/.zshrc"
|
1168
|
+
return true
|
1169
|
+
else
|
1170
|
+
return false
|
1171
|
+
end
|
1172
|
+
end
|
1173
|
+
|
1174
|
+
|
1175
|
+
end
|
1176
|
+
|
1177
|
+
|
1178
|
+
end
|
1179
|
+
|
1180
|
+
class String
|
1181
|
+
def pretty
|
1182
|
+
return self.gsub("\s\t\r\f", ' ').squeeze(' ')
|
1183
|
+
end
|
1184
|
+
|
1185
|
+
def is_i?
|
1186
|
+
!!(self =~ /\A[-+]?[0-9]+\z/)
|
1187
|
+
end
|
1188
|
+
end
|