Class: WireEdit

Inherits:
Object
  • Object
show all
Defined in:
lib/wire-edit.rb

Overview

cases: user@server w/ password, w/o password. server w/password, w/o password. pass it a file. pass it a directory. file exists, doesn’t exist. directory exists, doesn’t exist. absolute path vs relative path prefixed w home dir ~

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ssh_opt) ⇒ WireEdit

Returns a new instance of WireEdit.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/wire-edit.rb', line 13

def initialize ssh_opt
  @m = /(?<ssh_cmd>((?<user>.*)@)?(?<server_name>.*)):(?<remote_dir>.*)/.match(ssh_opt)
  flunk("require the ssh parameter!") unless @m
  [:user, :server_name, :remote_dir, :ssh_cmd].each { |attr| self.send("#{attr}=", @m[attr]) }

  FileUtils.mkdir_p local_path
  
  download
  edit
  fsevent = FSEvent.new
  puts "Watching #{local_path}"
  fsevent.watch(local_path) do |dirs|
    puts "Upload changes from: #{dirs.join(' ')}"
    upload
  end
  fsevent.run
end

Instance Attribute Details

#remote_dirObject

Returns the value of attribute remote_dir.



11
12
13
# File 'lib/wire-edit.rb', line 11

def remote_dir
  @remote_dir
end

#server_nameObject

Returns the value of attribute server_name.



11
12
13
# File 'lib/wire-edit.rb', line 11

def server_name
  @server_name
end

#ssh_cmdObject

Returns the value of attribute ssh_cmd.



11
12
13
# File 'lib/wire-edit.rb', line 11

def ssh_cmd
  @ssh_cmd
end

#userObject

Returns the value of attribute user.



11
12
13
# File 'lib/wire-edit.rb', line 11

def user
  @user
end

Instance Method Details

#downloadObject



31
32
33
34
35
# File 'lib/wire-edit.rb', line 31

def download
  puts 'Downloading...'
  # dl = @sftp.download!(remote_dir, local_path, :recursive => true)
  puts %x{ rsync -avz #{ssh_cmd}:#{remote_dir} #{File.dirname(local_path)} }
end

#editObject

def read_ssh_config

config = Net::SSH.configuration_for(server_name)
flunk("No user specified and no ~/.ssh/config entry for the server name.") if config.empty?
self.user = config[:user]
self.server_name = config[:host_name]

end



49
50
51
# File 'lib/wire-edit.rb', line 49

def edit
  %x{ mate #{local_path} }
end

#flunk(msg) ⇒ Object



61
62
63
64
# File 'lib/wire-edit.rb', line 61

def flunk(msg)
  STDERR << msg << "\n"
  exit 1
end

#local_pathObject



57
58
59
# File 'lib/wire-edit.rb', line 57

def local_path
  "/tmp/wire-edit/#{servername_dir}/#{remote_dir}"
end

#servername_dirObject



53
54
55
# File 'lib/wire-edit.rb', line 53

def servername_dir
  @local_dir ||= (user ? (user + '-') : '') + server_name
end

#uploadObject



37
38
39
40
# File 'lib/wire-edit.rb', line 37

def upload
  # @sftp.upload!(local_path, File.dirname(remote_dir))
  puts %x{ rsync -avz #{local_path} #{ssh_cmd}:#{File.dirname(remote_dir)} }
end