Class: Rush::Config
- Inherits:
-
Object
- Object
- Rush::Config
- Defined in:
- lib/rush/config.rb
Overview
The config class accesses files in ~/.rush to load and save user preferences.
Constant Summary collapse
- DefaultPort =
7770
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
Instance Method Summary collapse
-
#commands_file ⇒ Object
Commands are mixed in to Array and Rush::Entry, alongside the default commands from Rush::Commands.
- #credentials ⇒ Object
-
#credentials_file ⇒ Object
Credentials is the client-side equivalent of passwords.
- #credentials_password ⇒ Object
- #credentials_user ⇒ Object
- #ensure_credentials_exist ⇒ Object
-
#env_file ⇒ Object
The environment file is executed when the interactive shell starts up.
- #generate_credentials ⇒ Object
- #generate_password ⇒ Object
- #generate_secret(min, max) ⇒ Object
- #generate_user ⇒ Object
-
#history_file ⇒ Object
History is a flat file of past commands in the interactive shell, equivalent to .bash_history.
-
#initialize(location = nil) ⇒ Config
constructor
By default, reads from the dir ~/.rush, but an optional parameter allows using another location.
- #load_commands ⇒ Object
- #load_env ⇒ Object
- #load_history ⇒ Object
- #passwords ⇒ Object
-
#passwords_file ⇒ Object
Passwords contains a list of username:password combinations used for remote access via rushd.
- #save_credentials(user, password) ⇒ Object
- #save_history(array) ⇒ Object
- #save_tunnels(hash) ⇒ Object
- #secret_characters ⇒ Object
- #tunnels ⇒ Object
-
#tunnels_file ⇒ Object
~/.rush/tunnels contains a list of previously created ssh tunnels.
Constructor Details
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
5 6 7 |
# File 'lib/rush/config.rb', line 5 def dir @dir end |
Instance Method Details
#commands_file ⇒ Object
Commands are mixed in to Array and Rush::Entry, alongside the default commands from Rush::Commands. Any methods here should reference “entries” to get the list of entries to operate on.
Example ~/.rush/commands.rb:
def destroy_svn(*args)
entries.select { |e| e.name == '.svn' }.destroy
end
53 54 55 |
# File 'lib/rush/config.rb', line 53 def commands_file dir['commands.rb'] end |
#credentials ⇒ Object
84 85 86 |
# File 'lib/rush/config.rb', line 84 def credentials credentials_file.lines.first.split(":", 2) end |
#credentials_file ⇒ Object
Credentials is the client-side equivalent of passwords. It contains only one username:password combination that is transmitted to the server when connecting. This is also autogenerated if it does not exist.
80 81 82 |
# File 'lib/rush/config.rb', line 80 def credentials_file dir['credentials'] end |
#credentials_password ⇒ Object
96 97 98 |
# File 'lib/rush/config.rb', line 96 def credentials_password credentials[1] end |
#credentials_user ⇒ Object
92 93 94 |
# File 'lib/rush/config.rb', line 92 def credentials_user credentials[0] end |
#ensure_credentials_exist ⇒ Object
100 101 102 |
# File 'lib/rush/config.rb', line 100 def ensure_credentials_exist generate_credentials if credentials_file.contents_or_blank == "" end |
#env_file ⇒ Object
The environment file is executed when the interactive shell starts up. Put aliases and your own functions here; it is the equivalent of .bashrc or .profile.
Example ~/.rush/env.rb:
server = Rush::Box.new('[email protected]')
myproj = home['projects/myproj/']
36 37 38 |
# File 'lib/rush/config.rb', line 36 def env_file dir['env.rb'] end |
#generate_credentials ⇒ Object
104 105 106 |
# File 'lib/rush/config.rb', line 104 def generate_credentials save_credentials(generate_user, generate_password) end |
#generate_password ⇒ Object
112 113 114 |
# File 'lib/rush/config.rb', line 112 def generate_password generate_secret(8, 15) end |
#generate_secret(min, max) ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/rush/config.rb', line 116 def generate_secret(min, max) chars = self.secret_characters len = rand(max - min + 1) + min password = "" len.times do |index| password += chars[rand(chars.length)] end password end |
#generate_user ⇒ Object
108 109 110 |
# File 'lib/rush/config.rb', line 108 def generate_user generate_secret(4, 8) end |
#history_file ⇒ Object
History is a flat file of past commands in the interactive shell, equivalent to .bash_history.
16 17 18 |
# File 'lib/rush/config.rb', line 16 def history_file dir['history'] end |
#load_commands ⇒ Object
57 58 59 |
# File 'lib/rush/config.rb', line 57 def load_commands commands_file.contents_or_blank end |
#load_env ⇒ Object
40 41 42 |
# File 'lib/rush/config.rb', line 40 def load_env env_file.contents_or_blank end |
#load_history ⇒ Object
24 25 26 |
# File 'lib/rush/config.rb', line 24 def load_history history_file.contents_or_blank.split("\n") end |
#passwords ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/rush/config.rb', line 68 def passwords hash = {} passwords_file.lines_or_empty.each do |line| user, password = line.split(":", 2) hash[user] = password end hash end |
#passwords_file ⇒ Object
Passwords contains a list of username:password combinations used for remote access via rushd. You can fill this in manually, or let the remote connection publish it automatically.
64 65 66 |
# File 'lib/rush/config.rb', line 64 def passwords_file dir['passwords'] end |
#save_credentials(user, password) ⇒ Object
88 89 90 |
# File 'lib/rush/config.rb', line 88 def save_credentials(user, password) credentials_file.write("#{user}:#{password}\n") end |
#save_history(array) ⇒ Object
20 21 22 |
# File 'lib/rush/config.rb', line 20 def save_history(array) history_file.write(array.join("\n") + "\n") end |
#save_tunnels(hash) ⇒ Object
147 148 149 150 151 152 153 |
# File 'lib/rush/config.rb', line 147 def save_tunnels(hash) string = "" hash.each do |host, port| string += "#{host}:#{port}\n" end tunnels_file.write string end |
#secret_characters ⇒ Object
126 127 128 129 130 |
# File 'lib/rush/config.rb', line 126 def secret_characters [ ('a'..'z'), ('1'..'9') ].inject([]) do |chars, range| chars += range.to_a end end |
#tunnels ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/rush/config.rb', line 139 def tunnels tunnels_file.lines_or_empty.inject({}) do |hash, line| host, port = line.split(':', 2) hash[host] = port.to_i hash end end |
#tunnels_file ⇒ Object
~/.rush/tunnels contains a list of previously created ssh tunnels. The format is host:port, where port is the local port that the tunnel is listening on.
135 136 137 |
# File 'lib/rush/config.rb', line 135 def tunnels_file dir['tunnels'] end |