class Rush::Config

The config class accesses files in ~/.rush to load and save user preferences.

Constants

DefaultPort

Attributes

dir[R]

Public Class Methods

new(location = nil) click to toggle source

By default, reads from the dir ~/.rush, but an optional parameter allows using another location.

# File lib/rush/config.rb, line 9
def initialize(location = nil)
  @dir = Rush::Dir.new(location || "#{ENV['HOME']}/.rush")
  @dir.create
end

Public Instance Methods

commands_file() click to toggle source

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
# File lib/rush/config.rb, line 53
def commands_file
  dir['commands.rb']
end
credentials() click to toggle source
# File lib/rush/config.rb, line 82
def credentials
  credentials_file.lines.first.split(':', 2)
end
credentials_file() click to toggle source

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.

# File lib/rush/config.rb, line 78
def credentials_file
  dir['credentials']
end
credentials_password() click to toggle source
# File lib/rush/config.rb, line 94
def credentials_password
  credentials[1]
end
credentials_user() click to toggle source
# File lib/rush/config.rb, line 90
def credentials_user
  credentials[0]
end
ensure_credentials_exist() click to toggle source
# File lib/rush/config.rb, line 98
def ensure_credentials_exist
  generate_credentials if credentials_file.contents_or_blank == ''
end
env_file() click to toggle source

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('www@my.server')
myproj = home['projects/myproj/']
# File lib/rush/config.rb, line 36
def env_file
  dir['env.rb']
end
generate_credentials() click to toggle source
# File lib/rush/config.rb, line 102
def generate_credentials
  save_credentials(generate_user, generate_password)
end
generate_password() click to toggle source
# File lib/rush/config.rb, line 110
def generate_password
  generate_secret(8, 15)
end
generate_secret(min, max) click to toggle source
# File lib/rush/config.rb, line 114
def generate_secret(min, max)
  chars = self.secret_characters
  len = rand(max - min + 1) + min
  len.times.inject('') { |r| r += chars[rand(chars.length)] }
end
generate_user() click to toggle source
# File lib/rush/config.rb, line 106
def generate_user
  generate_secret(4, 8)
end
history_file() click to toggle source

History is a flat file of past commands in the interactive shell, equivalent to .bash_history.

# File lib/rush/config.rb, line 16
def history_file
  dir['history']
end
load_commands() click to toggle source
# File lib/rush/config.rb, line 57
def load_commands
  commands_file.contents_or_blank
end
load_env() click to toggle source
# File lib/rush/config.rb, line 40
def load_env
  env_file.contents_or_blank
end
load_history() click to toggle source
# File lib/rush/config.rb, line 24
def load_history
  history_file.contents_or_blank.split("\n")
end
passwords() click to toggle source
# File lib/rush/config.rb, line 68
def passwords
  passwords_file.lines_or_empty.inject({}) do |result, line|
    user, password = line.split(':', 2)
    result.merge user => password
  end
end
passwords_file() click to toggle source

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.

# File lib/rush/config.rb, line 64
def passwords_file
  dir['passwords']
end
save_credentials(user, password) click to toggle source
# File lib/rush/config.rb, line 86
def save_credentials(user, password)
  credentials_file.write("#{user}:#{password}\n")
end
save_history(array) click to toggle source
# File lib/rush/config.rb, line 20
def save_history(array)
  history_file.write(array.join("\n") + "\n")
end
save_tunnels(hash) click to toggle source
# File lib/rush/config.rb, line 140
def save_tunnels(hash)
  string = ''
  hash.each do |host, port|
    string += "#{host}:#{port}\n"
  end
  tunnels_file.write string
end
secret_characters() click to toggle source
# File lib/rush/config.rb, line 120
def secret_characters
  [('a'..'z'), ('1'..'9')].inject([]) do |chars, range|
    chars += range.to_a
  end
end
tunnels() click to toggle source
# File lib/rush/config.rb, line 133
def tunnels
  tunnels_file.lines_or_empty.inject({}) do |hash, line|
    host, port = line.split(':', 2)
    hash.merge host => port.to_i
  end
end
tunnels_file() click to toggle source

~/.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.

# File lib/rush/config.rb, line 129
def tunnels_file
  dir['tunnels']
end