view_server 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,24 +1,73 @@
1
- # ViewServer
1
+ # view_server
2
+ ## Description
2
3
 
3
- TODO: Write a gem description
4
+ Sometimes when working on a remote server you want to view data in a local app like Excel. The traditionnal way to acheive this is to save the data into a file then copy it using scp, then opening it with the app of your choice. If you have to do this many times a day it can really slow down your workflow.
5
+ With the view\_server it's very easy to view a file.
6
+
7
+ view\_server also offers you a way to access your local clipboard from a remote server.
4
8
 
5
9
  ## Installation
6
10
 
7
- Add this line to your application's Gemfile:
11
+ gem install view_server
8
12
 
9
- gem 'view_server'
13
+ ## Usage
10
14
 
11
- And then execute:
15
+ ### Show file
12
16
 
13
- $ bundle
17
+ Start the server on your local machine. At the moment only OS X is supported.
14
18
 
15
- Or install it yourself as:
19
+ view_server
16
20
 
17
- $ gem install view_server
21
+ Now open a ssh session to the remote server and create a reverse tunnel to your local machine:
18
22
 
19
- ## Usage
23
+ ssh -R 10021:localhost:10021 username@remoteserver.com
24
+
25
+ Then install the gem on the remote server:
26
+
27
+ gem install view_server
28
+
29
+ Now that your setup is complete. You can view data from the remote server. Open a irb console on the remote server and try this:
30
+
31
+ require 'view_server'
32
+
33
+ ViewServer::Client.new.server.show <<EOF, 'csv'
34
+ 1,2
35
+ 3,4
36
+ EOF
37
+
38
+ This will open Excel with a spreadsheet filled with the data defined in the here document.
39
+
40
+ The last paramenter of the show method represents the file extension of the temporary file that will be created before being opened on your local computer. If you want to open it with your text editor just change this parameter with 'txt'
41
+
42
+ There is also a command line utility to that you can use:
43
+
44
+ echo -e "1,2\n2,3" | show -e csv
45
+
46
+ ### Clipboard support
47
+
48
+ On the remote server you can copy to the local clipboard:
49
+
50
+ ls | cbcopy
51
+
52
+ You can also paste to the remote server:
53
+
54
+ cbpaste > file.txt
55
+
56
+ ## Simplifying the ssh command
57
+
58
+ It can be teadious to add the -R parameter each time that you want to ssh to the remote server.
59
+ In order to create the tunnel automaticaly when you ssh to a server you can add this at the end of your ~/.ssh/config
60
+
61
+ Host * !*github* !*bitbucket*
62
+ RemoteForward 10021 localhost:10021
63
+
64
+ The first line tells ssh to create the tunnel for all ssh connection except for github and bitbucket.
65
+
66
+ Now each time that you connect to a remote server with ssh the tunnel will be made, to need to add the -R option.
67
+
68
+ ## TODO
20
69
 
21
- TODO: Write usage instructions here
70
+ Adding tests
22
71
 
23
72
  ## Contributing
24
73
 
@@ -28,4 +77,4 @@ TODO: Write usage instructions here
28
77
  4. Push to the branch (`git push origin my-new-feature`)
29
78
  5. Create new Pull Request
30
79
  =======
31
- view_server
80
+ view\_server
data/Rakefile CHANGED
@@ -6,9 +6,9 @@ require 'bundler/gem_tasks'
6
6
  require 'rake/testtask'
7
7
 
8
8
  Rake::TestTask.new do |t|
9
- t.pattern = "test/*_test.rb"
9
+ t.pattern = "spec/*_spec.rb"
10
10
  # To avoid requirement of File.expand_path(File.dirname(__FILE__) + 'test_helper') from test files
11
- t.libs << 'test'
11
+ t.libs << 'spec'
12
12
  end
13
13
 
14
14
 
data/bin/cbcopy CHANGED
@@ -3,9 +3,4 @@
3
3
  require 'main'
4
4
  require 'view_server'
5
5
 
6
- Main do
7
- def run
8
- ViewServer::Client.new.server.to_cb($stdin.read)
9
- end
10
- end
11
-
6
+ ViewServer::CbCopyRunner.run(ARGV)
data/bin/cbpaste CHANGED
@@ -3,9 +3,4 @@
3
3
  require 'main'
4
4
  require 'view_server'
5
5
 
6
- Main do
7
- def run
8
- $stdout << ViewServer::Client.new.server.paste
9
- end
10
- end
11
-
6
+ ViewServer::CbPasteRunner.run(ARGV)
data/bin/show CHANGED
@@ -1,48 +1,5 @@
1
1
  #! /usr/bin/env ruby
2
-
3
2
  require 'main'
4
3
  require 'view_server'
5
- require 'optparse'
6
-
7
- options = {}
8
-
9
- class ShowRunner
10
- def self.parse(args)
11
- options = {}
12
- opt_parser = OptionParser.new do |opts|
13
- opts.banner = "Usage: show [options] [file]"
14
-
15
- opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
16
- options[:verbose] = v
17
- end
18
-
19
- opts.on("-e", "--extension EXTENSION", "File extension") do |v|
20
- options[:ext] = v
21
- end
22
-
23
- opts.on("-p", "--port PORT", "Port number") do |v|
24
- options[:ext] = v
25
- end
26
- end
27
-
28
- opt_parser.parse!(args)
29
- filename = ARGV.first
30
- if filename
31
- data = File.read filename
32
- ext = File.extname(filename)[1..-1]
33
- else
34
- data = $stdin.read
35
- ext = options.fetch(:ext) {'txt'}
36
- end
37
-
38
- {:data => data, :ext => ext }
39
- end
40
-
41
- def self.run(args)
42
- opts = self.parse args
43
- ViewServer::Client.new.server.show(opts[:data], opts[:ext])
44
- end
45
- end
46
- options = ShowRunner.run(ARGV)
47
-
48
4
 
5
+ ViewServer::ShowRunner.run(ARGV)
data/lib/view_server.rb CHANGED
@@ -1,8 +1,13 @@
1
1
  require "view_server/version"
2
+ require "view_server/show_runner"
2
3
 
3
4
  module ViewServer
4
- autoload :Server, 'view_server/server'
5
- autoload :Client, 'view_server/client'
6
- autoload :Runner, 'view_server/runner'
7
- autoload :TmpFile, 'view_server/tmp_file'
5
+ autoload :Server, "view_server/server"
6
+ autoload :Client, "view_server/client"
7
+ autoload :Runner, "view_server/runner"
8
+ autoload :TmpFile, "view_server/tmp_file"
9
+ autoload :ShowRunner, "view_server/show_runner"
10
+ autoload :CbCopyRunner, "view_server/cbcopy_runner"
11
+ autoload :CbPasteRunner,"view_server/cbpaste_runner"
12
+ autoload :Launcher, "view_server/launcher"
8
13
  end
@@ -0,0 +1,31 @@
1
+ require 'optparse'
2
+ require 'clipboard'
3
+
4
+ module ViewServer
5
+ class CbCopyRunner
6
+ def self.parse(args)
7
+ options = {:port => 10021}
8
+ opt_parser = OptionParser.new do |opts|
9
+ opts.banner = "Usage: cbcopy [options]"
10
+
11
+ opts.on("-p", "--port PORT", "Port number. Default is 10021") do |v|
12
+ options[:port] = Integer(v)
13
+ end
14
+ end
15
+ opt_parser.parse!(args)
16
+ options
17
+ end
18
+
19
+ def self.run(args = [])
20
+ opts = parse(args)
21
+ server = if opts[:port] == -1
22
+ ViewServer::Server.new(nil)
23
+ else
24
+ ViewServer::Client.new(opts[:port])
25
+ end
26
+ content = $stdin.read
27
+ server.to_cb(content)
28
+ end
29
+ end
30
+ end
31
+
@@ -0,0 +1,29 @@
1
+ require 'optparse'
2
+ require 'clipboard'
3
+
4
+ module ViewServer
5
+ class CbPasteRunner
6
+ def self.parse(args)
7
+ options = {:port => 10021}
8
+ opt_parser = OptionParser.new do |opts|
9
+ opts.banner = "Usage: cbpaste [options]"
10
+
11
+ opts.on("-p", "--port PORT", "Port number. Default is 10021") do |v|
12
+ options[:port] = Integer(v)
13
+ end
14
+ end
15
+ opt_parser.parse!(args)
16
+ options
17
+ end
18
+
19
+ def self.run(args = [])
20
+ opts = parse(args)
21
+ server = if opts[:port] == -1
22
+ ViewServer::Server.new(nil)
23
+ else
24
+ ViewServer::Client.new(opts[:port])
25
+ end
26
+ $stdout << server.paste
27
+ end
28
+ end
29
+ end
@@ -1,12 +1,17 @@
1
1
  require 'drb'
2
+ require 'delegate'
2
3
 
3
4
  module ViewServer
4
- class Client
5
+ class Client < Delegator
5
6
  attr_reader :server
6
7
 
7
8
  def initialize(port = 10021)
8
9
  DRb.start_service
9
10
  @server = DRbObject.new(nil, "druby://localhost:#{port}")
10
11
  end
12
+
13
+ def __getobj__
14
+ @server
15
+ end
11
16
  end
12
17
  end
@@ -0,0 +1,12 @@
1
+ module ViewServer
2
+ class Launcher
3
+ def launch(data, file_ext = 'txt')
4
+ TmpFile.open("", "." + file_ext) do |a|
5
+ a << data
6
+ a.close
7
+ `open #{a.path} `
8
+ sleep 5 #We don't want to delete the file before it's loaded
9
+ end
10
+ end
11
+ end
12
+ end
@@ -2,38 +2,6 @@ require 'main'
2
2
 
3
3
  module ViewServer
4
4
  class Runner
5
- def self.old_run
6
- options = Clip do |p|
7
- p.flag 's', 'server',:desc => 'Is a server', :default => false
8
-
9
- p.optional 'p', 'port', :desc => 'The port', :default => 10021 do |v|
10
- v.to_i # always deal with integers
11
- end
12
- p.flag 'c', 'copy_to_clipboard', :desc => "Send to clipboard"
13
- p.flag 'P', 'paste_from_clipboard', :desc => "Read from clipboard"
14
- p.flag 'v', 'verbose', :desc => 'Make it chatty'
15
- end
16
-
17
- if options.valid?
18
- if options.verbose?
19
- puts options.host
20
- puts options.port
21
- puts 'files:'
22
- options.files.each do |f|
23
- puts "\t#{f}"
24
- end
25
- end
26
- if options.server?
27
- Server.serve(options)
28
- else
29
- Client.new(options.port).send(options)
30
- end
31
- else
32
- # print error message(s) and usage
33
- $stderr.puts options.to_s
34
- end
35
- end
36
-
37
5
  def self.run
38
6
  main.run
39
7
  end
@@ -1,28 +1,30 @@
1
1
  require 'drb'
2
+ require 'clipboard'
2
3
 
3
4
  module ViewServer
4
5
  class Server
6
+ attr_reader :launcher, :clipboard
7
+
8
+ def initialize(launcher = Launcher.new)
9
+ @launcher = launcher
10
+ @clipboard = Clipboard
11
+ end
12
+
5
13
  # I wanted to use the display method but it conflicts
6
14
  # with the Object#display method
7
15
  def show(data, file_ext = 'txt')
8
- TmpFile.open("", "." + file_ext) do |a|
9
- a << data
10
- a.close
11
- `open #{a.path} `
12
- sleep 5 #We don't want to delete the file before it's loaded
13
- end
16
+ launcher.launch(data, file_ext)
14
17
  end
15
18
 
16
19
  def to_cb(data) # To clipboard
17
- File.open("#{ENV['HOME']}/tmp/clipboard", "w"){|a| a << data}
18
- IO.popen('pbcopy', 'w'){|a| a << data}
20
+ clipboard.copy(data)
19
21
  end
20
22
 
21
23
  def paste # From clipboard
22
- `pbpaste`
24
+ clipboard.paste
23
25
  end
24
26
 
25
- def self.serve(port)
27
+ def self.serve(launcher = Launcher.new, port)
26
28
  DRb.start_service "druby://localhost:#{port}", Server.new
27
29
  puts DRb.uri
28
30
  DRb.thread.join
@@ -0,0 +1,43 @@
1
+ require 'optparse'
2
+
3
+ module ViewServer
4
+ class ShowRunner
5
+ def self.parse(args)
6
+ options = {:port => 10021}
7
+ opt_parser = OptionParser.new do |opts|
8
+ opts.banner = "Usage: show [options] [file]"
9
+
10
+ opts.on("-e", "--extension EXTENSION", "File extension") do |v|
11
+ options[:ext] = v
12
+ end
13
+
14
+ opts.on("-p", "--port PORT", "Port number. Default is 10021") do |v|
15
+ options[:port] = Integer(v)
16
+ end
17
+ end
18
+
19
+ opt_parser.parse!(args)
20
+
21
+ filename = ARGV.first
22
+ if filename
23
+ data = File.read filename
24
+ ext = File.extname(filename)[1..-1]
25
+ else
26
+ data = $stdin.read
27
+ ext = options.fetch(:ext) {'txt'}
28
+ end
29
+
30
+ options.merge({:data => data, :ext => ext })
31
+ end
32
+
33
+ def self.run(args, launcher = Launcher.new)
34
+ opts = parse(args)
35
+
36
+ if opts[:port] == -1
37
+ ViewServer::Server.new(launcher).show(opts[:data], opts[:ext])
38
+ else
39
+ ViewServer::Client.new(opts[:port]).show(opts[:data], opts[:ext])
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,3 +1,3 @@
1
1
  module ViewServer
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
File without changes
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'CbCopyRunner' do
4
+ it 'should copy to the clipboard' do
5
+ data = 'This is the clipboard content'
6
+ capture_io(data) do
7
+ ViewServer::CbCopyRunner.run %w[-p -1]
8
+ end
9
+
10
+ pasted = capture_io('') do
11
+ ViewServer::CbPasteRunner.run %w[-p -1]
12
+ end
13
+
14
+ pasted.must_equal data
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'ShowRunner' do
4
+ it 'should call the show server method with data and extension' do
5
+ data = 'This is data'
6
+ launcher = mock('launcher')
7
+ launcher.expects(:launch).with(data, 'csv')
8
+ capture_io data do
9
+ ViewServer::ShowRunner.run %w[-e csv -p -1], launcher
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,60 @@
1
+ $:.unshift File.expand_path('..', __FILE__)
2
+ $:.unshift File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'minitest/spec'
5
+ require 'minitest/autorun'
6
+ require 'mocha/setup'
7
+
8
+ require 'view_server'
9
+ require 'fileutils'
10
+ require 'stringio'
11
+ require 'open3'
12
+ require 'colored'
13
+ require 'pry'
14
+
15
+ class MiniTest::Spec
16
+ BIN_DIR = File.join(File.dirname(File.expand_path(__FILE__)), '../bin' )
17
+ LIB_DIR = File.join(File.dirname(File.expand_path(__FILE__)), '../lib' )
18
+
19
+ def run_exe(executable, stdin_str, cmd_arg = [])
20
+ env = {"RUBYLIB" => "#{LIB_DIR}:#{ENV['RUBYLIB']}"}
21
+ filepath = File.join(BIN_DIR, executable)
22
+ outstream = StringIO.new
23
+
24
+ Open3.popen3(env, filepath, *cmd_arg) do |stdin, stdout, stderr, wait_thr|
25
+ stdin << stdin_str
26
+ stdin.close
27
+ outstream << stdout.read
28
+ raise_on_error(wait_thr.value, stderr, executable, stdout)
29
+ end
30
+ outstream.string
31
+ end
32
+
33
+ def capture_io(in_str)
34
+ old_stdin, old_stdout = $stdin, $stdout
35
+ $stdin = StringIO.new(in_str)
36
+ $stdout = StringIO.new
37
+ yield
38
+ out_str = $stdout.string
39
+ out_str
40
+ ensure
41
+ $stdin, $stdout = old_stdin, old_stdout
42
+ end
43
+
44
+ def raise_on_error(status, stderr, executable, stdout)
45
+ if ! status.success?
46
+ msg = <<EOF
47
+ Error running #{executable}"
48
+ stdout:
49
+ #{stdout.read}
50
+
51
+ stderr:
52
+ #{stderr.read}
53
+
54
+ status:
55
+ #{status}
56
+ EOF
57
+ raise msg
58
+ end
59
+ end
60
+ end
data/view_server.gemspec CHANGED
@@ -6,13 +6,16 @@ require 'view_server/version'
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = "view_server"
8
8
  gem.version = ViewServer::VERSION
9
- gem.add_dependency "clip"
10
9
  gem.add_dependency "main"
11
- gem.add_development_dependency 'rake'
10
+ gem.add_dependency "clipboard"
11
+ gem.add_development_dependency "rake"
12
12
  gem.add_development_dependency "bundler", "~> 1.0"
13
- gem.add_development_dependency 'minitest'
14
- gem.add_development_dependency 'pry'
15
- gem.add_development_dependency 'pry-nav'
13
+ gem.add_development_dependency "minitest"
14
+ gem.add_development_dependency "pry"
15
+ gem.add_development_dependency "pry-nav"
16
+ gem.add_development_dependency "colored"
17
+ gem.add_development_dependency "mocha"
18
+
16
19
  gem.authors = ["Martin Chabot"]
17
20
  gem.email = ["chabotm@gmail.com"]
18
21
  gem.description = %q{Allow local viewing of remote files}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: view_server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,10 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-14 00:00:00.000000000 Z
12
+ date: 2013-11-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: clip
15
+ name: main
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
@@ -28,7 +28,7 @@ dependencies:
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
30
  - !ruby/object:Gem::Dependency
31
- name: main
31
+ name: clipboard
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
@@ -123,6 +123,38 @@ dependencies:
123
123
  - - ! '>='
124
124
  - !ruby/object:Gem::Version
125
125
  version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: colored
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: mocha
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
126
158
  description: Allow local viewing of remote files
127
159
  email:
128
160
  - chabotm@gmail.com
@@ -144,14 +176,19 @@ files:
144
176
  - bin/show
145
177
  - bin/view_server
146
178
  - lib/view_server.rb
179
+ - lib/view_server/cbcopy_runner.rb
180
+ - lib/view_server/cbpaste_runner.rb
147
181
  - lib/view_server/client.rb
182
+ - lib/view_server/launcher.rb
148
183
  - lib/view_server/runner.rb
149
184
  - lib/view_server/server.rb
185
+ - lib/view_server/show_runner.rb
150
186
  - lib/view_server/tmp_file.rb
151
187
  - lib/view_server/version.rb
152
- - test/runner_spec.rb
153
- - test/spec_helper.rb
154
- - test/view_server_test.rb
188
+ - spec/cb_paste_runner_spec.rb
189
+ - spec/cbcopy_spec.rb
190
+ - spec/show_runner_spec.rb
191
+ - spec/spec_helper.rb
155
192
  - view_server.gemspec
156
193
  homepage: ''
157
194
  licenses: []
@@ -167,7 +204,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
167
204
  version: '0'
168
205
  segments:
169
206
  - 0
170
- hash: 4378741166747802808
207
+ hash: -1787566526224458814
171
208
  required_rubygems_version: !ruby/object:Gem::Requirement
172
209
  none: false
173
210
  requirements:
@@ -176,7 +213,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
213
  version: '0'
177
214
  segments:
178
215
  - 0
179
- hash: 4378741166747802808
216
+ hash: -1787566526224458814
180
217
  requirements: []
181
218
  rubyforge_project:
182
219
  rubygems_version: 1.8.23
@@ -184,6 +221,7 @@ signing_key:
184
221
  specification_version: 3
185
222
  summary: Allow local viewing of remote files
186
223
  test_files:
187
- - test/runner_spec.rb
188
- - test/spec_helper.rb
189
- - test/view_server_test.rb
224
+ - spec/cb_paste_runner_spec.rb
225
+ - spec/cbcopy_spec.rb
226
+ - spec/show_runner_spec.rb
227
+ - spec/spec_helper.rb
data/test/runner_spec.rb DELETED
@@ -1,10 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'ViewServer Runner Main' do
4
- before do
5
- @main = ViewServer.main
6
- end
7
-
8
- it 'should start as a sever' do
9
- end
10
- end
data/test/spec_helper.rb DELETED
@@ -1,4 +0,0 @@
1
- $:.unshift File.expand_path('..', __FILE__)
2
- $:.unshift File.expand_path('../../lib', __FILE__)
3
- require 'minitest/spec'
4
- require 'minitest/autorun'
@@ -1,9 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ViewServer
4
- class ViewServerTest < MiniTest::Unit::TestCase
5
- def test_truth
6
- assert true
7
- end
8
- end
9
- end