sesh 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 74b3f87282f5252856e6cd89895a40144e9f0ea3
4
- data.tar.gz: 14c8da3d3a2e8ca8ae0719818ea839ff2393f6cb
3
+ metadata.gz: 874c4775437a219acbe216d2f96876483e81f38d
4
+ data.tar.gz: 292433cee503cfe8458341acf001b0c87b3c39c3
5
5
  SHA512:
6
- metadata.gz: 358f35e691af07194d7759c90791fff24810f0ebae616b0450f01191bd9421efd4d34517ed79a2c69d8b95d8b8fd85dc04dada85ccde28cc5493fa48f8d5dea4
7
- data.tar.gz: c730cb2453dc3225f834507b9c2e13bb2c740833af18f393965738175b909948c16e3170c7029480bbf1bf44b88c26d63fe10a843f86758c0a75d11e7fb4d29a
6
+ metadata.gz: 0e5f7543613803df60a7acb83f2802fd6791caf9c7e994a2190b1a65207702abd13ba1aadefb74e53fcbd916d908d1280d9de472edbae59f5e01bfa1deead3aa
7
+ data.tar.gz: f317cfcdace6ddc0f0f7dda8911a49cff671a7a43ff0f87e850b41a01059b4428b702bb61a1e30ec9e25f77ed89b2b576bed7633a2ef9a534a26a22b8a5fd727
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sesh (0.2.0)
4
+ sesh (0.3.0)
5
5
  awesome_print
6
6
  colorize
7
7
  deep_merge
data/lib/sesh/cli.rb CHANGED
@@ -183,7 +183,19 @@ module Sesh
183
183
  pcount = running_projects.count
184
184
  if pcount > 0
185
185
  Logger.success "#{pcount} project#{pcount>1 ? 's':''} currently running:"
186
- puts; running_projects.each {|rp| Logger.info rp, 1 }
186
+ running_projects.each do |rp|
187
+ puts; Logger.info "Project: #{rp}", 1
188
+ tc = TmuxControl.new rp, socket_file: "/tmp/#{rp}.sock"
189
+ tc_clients = tc.connected_client_devices
190
+ if tc_clients.any?
191
+ Logger.success "Connected Client Devices:", 2
192
+ tc_clients.each_with_index do |c, i|
193
+ Logger.info "#{i+1}: #{c}: #{tc.get_ip_from_device(c)}", 3
194
+ end
195
+ else
196
+ Logger.warn 'No clients connected.', 2
197
+ end
198
+ end
187
199
  puts
188
200
  else Logger.fatal "There are no Sesh projects currently running." end
189
201
  when 'connect'
@@ -205,6 +217,7 @@ module Sesh
205
217
  @tmux_control.do_shell_operation! @options[:shell]
206
218
  when 'rspec'
207
219
  puts "Spec: #{@options[:shell][:spec]}"
220
+ when 'detach' then @tmux_control.disconnect_client! ARGV.join(' ')
208
221
  else
209
222
  Logger.fatal "Command not recognized!"
210
223
  end
@@ -4,7 +4,8 @@ module Sesh
4
4
  class TmuxControl
5
5
  def initialize(project, options={})
6
6
  @project = project || Inferences::infer_project_from_current_directory
7
- @options = DEFAULT_OPTIONS[:tmux].merge(options)
7
+ @options = {}.merge(DEFAULT_OPTIONS[:tmux]).merge(options)
8
+ @socket_file = @options[:socket_file]
8
9
  end
9
10
 
10
11
  def self.get_running_projects
@@ -26,7 +27,7 @@ module Sesh
26
27
  def issue_start_command!
27
28
  # Add bundle exec to the sesh begin command for dev purposes.
28
29
  cmd = Sesh.format_command <<-BASH
29
- tmux -S "#{@options[:socket_file]}" new-session -d "eval \\"\$SHELL -l -c 'rvm use default; sesh begin #{@project}'\\"" 2>&1
30
+ tmux -S "#{@socket_file}" new-session -d "eval \\"\$SHELL -l -c 'rvm use default; sesh begin #{@project}'\\"" 2>&1
30
31
  BASH
31
32
  output = `#{cmd}`.strip
32
33
  return true if $? && output.length == 0
@@ -37,12 +38,12 @@ module Sesh
37
38
  `ps -ef | grep "[t]mux -u attach-session -t #{Regexp.escape(@project)}\\$" | grep -v grep | awk '{print $2}' | xargs kill -9`
38
39
  end
39
40
 
40
- def connection_command; "tmux -S #{@options[:socket_file]} a" end
41
+ def connection_command; "tmux -S #{@socket_file} a" end
41
42
 
42
43
  def obtain_pids_from_session
43
44
  tmux_processes =
44
45
  `tmux list-panes -s -F "\#{pane_pid} \#{pane_current_command}" -t "#{@project}" 2> /dev/null | grep -v tmux | awk '{print $1}'`.strip.lines +
45
- `tmux -S "#{@options[:socket_file]}" list-panes -s -F "\#{pane_pid} \#{pane_current_command}" 2> /dev/null | grep -v tmux | awk '{print $1}'`.strip.lines
46
+ `tmux -S "#{@socket_file}" list-panes -s -F "\#{pane_pid} \#{pane_current_command}" 2> /dev/null | grep -v tmux | awk '{print $1}'`.strip.lines
46
47
  return [] unless tmux_processes.any?
47
48
  spring_processes = []
48
49
  spring_app_pid = `ps -ef | grep "[s]pring app .*| #{@project} |" | grep -v grep | awk '{print $2}'`.strip
@@ -95,8 +96,55 @@ module Sesh
95
96
  end
96
97
  def restart_project!; stop_project!; sleep 0.5; start_project! end
97
98
 
99
+ def connected_client_devices
100
+ `tmux -S "#{@socket_file}" list-clients | cut -d : -f 1 | cut -d / -f 3`.strip.lines.map(&:strip)
101
+ end
102
+ def get_ip_from_device(devname)
103
+ ip_line = `who -a 2> /dev/null | grep " #{devname} "`.strip
104
+ return '127.0.0.1' unless ip_line.length > 0 && ip_line =~ /\)$/
105
+ ip_line.split('(')[-1][0..-2]
106
+ end
107
+ def get_device_from_ip(ip)
108
+ return if ( connected_devs = connected_client_devices ).length == 0
109
+ who_lines =
110
+ `who -a 2> /dev/null | grep #{ip == '127.0.0.1' ? ' -v "\\([.*]\\)\\$' :
111
+ '"' + Regexp.escape("(#{ip})") }"`.strip.lines
112
+ return if who_lines.length == 0
113
+ connected_devs.find{|d| who_lines.find{|l| l =~ / #{d} / } }
114
+ end
115
+ def connected_clients
116
+ connected_client_devices.map{|devname| get_ip_from_device(devname) } end
117
+ def disconnect_client_by_device!(devname)
118
+ `tmux -S "#{@socket_file}" detach-client -t "/dev/#{devname}"`.strip.length == 0 end
119
+ def disconnect_client_by_ip!(ip)
120
+ device = get_device_from_ip(ip)
121
+ Logger.fatal("#{ip} is not connected to project \"#{@project}\".") if device.nil?
122
+ disconnect_client_by_device! device end
123
+ def disconnect_client_by_index!(index)
124
+ disconnect_client_by_device! connected_client_devices[index] end
125
+ def disconnect_client!(identifier)
126
+ if identifier.to_i.to_s == identifier.to_s # It's an integer
127
+ disconnect_client_by_index! identifier.to_i - 1
128
+ elsif identifier =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ # It's an ip
129
+ disconnect_client_by_ip! identifier
130
+ elsif identifier =~ /.*@.*/ || identifier =~ /\.local$/
131
+ ping_output = `ping -a -c 1 #{identifier} 2>&1`.strip.lines
132
+ if $? && ping_output.length > 1 && ping_output[1] =~ / from /
133
+ resolved_ip = ping_output[1].split(' from ')[1].split(': ')[0]
134
+ disconnect_client_by_ip! resolved_ip
135
+ else
136
+ puts ping_output
137
+ end
138
+ else
139
+ ssh_identifier =
140
+ `awk '/Host #{identifier}/ {getline; print $2}' ~/.ssh/config`.strip
141
+ return disconnect_client!(ssh_identifier) if ssh_identifier.length > 0
142
+ fatal("Client")
143
+ end
144
+ end
145
+
98
146
  def send_keys_to_project!(keys)
99
- `tmux -S "#{@options[:socket_file]}" send-keys #{keys}`.strip.length == 0
147
+ `tmux -S "#{@socket_file}" send-keys #{keys}`.strip.length == 0
100
148
  end
101
149
  def send_interrupt!; send_keys_to_project! 'C-c' end
102
150
  def interrupt_and_send_keys_to_project!(keys)
data/lib/sesh/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Sesh
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sesh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - MacKinley Smith