stf-client-neofreko 0.1.7 → 0.1.8

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: ebb281d8ac65af753870dc7cea6bd86be8a3ce3c
4
- data.tar.gz: b4b9783173755f0e0862656badd9126da0718043
3
+ metadata.gz: f2d1547cb516379f9794670327da1e61a4c38dda
4
+ data.tar.gz: c877f7b529fb3fe6dbafb672bc1c09f0448fdc34
5
5
  SHA512:
6
- metadata.gz: c45ed8a17bdaf36d3edb58aa3454f60328fa32ce96396dafff8e8eab6e1a8fb9ded0bb6ac832582f67fc72d8d1c87ec758648bec162cf833f127673a5462a8d3
7
- data.tar.gz: 8c95da0681fde2a4168b8ba1eda52a95a8494c4554cc89f846a4e665957acd9d4236b90feb0eeff7f9be12f2d77be3ea99a1ff57d659818cbd1970d371ecc0d6
6
+ metadata.gz: e94a19593b4a94be969b7dc8ab8f4ad67b5c542989c9413cb921aef9451de5d13b9faf8e42577dc2b1d393cee3ee3d0c46f24d45e8ea496d5bf9d2042123ba7c
7
+ data.tar.gz: 235d8b26d77f34f1c686fc8595fd81c284a5c987617f8838424a8d93fdf14f7b8fbdab7c474c5ddf7f37112ddbbb85472cac281107b99198810b2d766db1454f
data/README.md CHANGED
@@ -50,11 +50,6 @@ COMMANDS
50
50
  ENVIRONMENT VARIABLES
51
51
  STF_TOKEN - Authorization token
52
52
  STF_URL - URL to STF
53
-
54
- COMMAND OPTIONS
55
- connect
56
- -f - Filter devices in the form of "key:value". Use stf-client keys to list keys and stf-client values -k <key from prev command> to get list of applicable values
57
- -d, --adb - Automatically execute adb connect command once device acquired. Default true
58
53
  ```
59
54
 
60
55
  ## Contributing
data/lib/stf/client.rb CHANGED
@@ -17,52 +17,57 @@ module Stf
17
17
 
18
18
  def get_devices
19
19
  response = execute '/api/v1/devices', Net::HTTP::Get
20
- return response.devices
20
+ response.devices
21
21
  end
22
22
 
23
23
  def get_device(serial)
24
24
  response = execute "/api/v1/devices/#{serial}", Net::HTTP::Get
25
- return response.device
25
+ response.device
26
26
  end
27
27
 
28
28
  def get_user
29
29
  response = execute '/api/v1/user', Net::HTTP::Get
30
- return response.user
30
+ response.user
31
31
  end
32
32
 
33
33
  def get_user_devices
34
34
  response = execute '/api/v1/user/devices', Net::HTTP::Get
35
- return response.devices
35
+ response.devices
36
36
  end
37
37
 
38
38
  def add_device(serial)
39
- response = execute '/api/v1/user/devices', Net::HTTP::Post, {serial: serial}.to_json
40
- return response.success
39
+ response = execute '/api/v1/user/devices', Net::HTTP::Post, { serial: serial }.to_json
40
+ response.success
41
+ end
42
+
43
+ def add_adb_public_key(adbkeypub)
44
+ response = execute '/api/v1/user/adbPublicKeys', Net::HTTP::Post, { publickey: adbkeypub }.to_json
45
+ response.success
41
46
  end
42
47
 
43
48
  def remove_device(serial)
44
49
  response = execute "/api/v1/user/devices/#{serial}", Net::HTTP::Delete
45
- return response.success
50
+ response.success
46
51
  end
47
52
 
48
53
  def start_debug(serial)
49
54
  response = execute "/api/v1/user/devices/#{serial}/remoteConnect", Net::HTTP::Post
50
- return response
55
+ response
51
56
  end
52
57
 
53
58
  def stop_debug(serial)
54
59
  response = execute "/api/v1/user/devices/#{serial}/remoteConnect", Net::HTTP::Delete
55
- return response.success
60
+ response.success
56
61
  end
57
62
 
58
63
  private
59
64
 
60
- def execute(relative_url, type, body='')
61
- return execute_absolute @base_url + relative_url, type, body
65
+ def execute(relative_url, type, body = '')
66
+ execute_absolute @base_url + relative_url, type, body
62
67
  end
63
68
 
64
- def execute_absolute(url, type, body='', limit = 10)
65
- raise ArgumentError, 'too many HTTP redirects' if limit == 0
69
+ def execute_absolute(url, type, body = '', limit = 10)
70
+ raise ArgumentError, 'too many HTTP redirects' if limit.zero?
66
71
 
67
72
  uri = URI.parse(url)
68
73
  http = Net::HTTP.new(uri.host, uri.port)
@@ -72,19 +77,19 @@ module Stf
72
77
  response = http.request(request)
73
78
 
74
79
  case response
75
- when Net::HTTPSuccess then
76
- json = JSON.parse(response.body, object_class: OpenStruct)
77
-
78
- logger.debug "API returned #{json}"
79
- when Net::HTTPRedirection then
80
- location = response['location']
81
- logger.debug "redirected to #{location}"
82
- return execute_absolute(location, type, body, limit - 1)
83
- else
84
- logger.error "API returned #{response.value}"
80
+ when Net::HTTPSuccess then
81
+ json = JSON.parse(response.body, object_class: OpenStruct)
82
+
83
+ logger.debug "API returned #{json}"
84
+ when Net::HTTPRedirection then
85
+ location = response['location']
86
+ logger.debug "redirected to #{location}"
87
+ return execute_absolute(location, type, body, limit - 1)
88
+ else
89
+ logger.error "API returned #{response.value}"
85
90
  end
86
91
 
87
- return json
92
+ json
88
93
  end
89
94
  end
90
- end
95
+ end
@@ -6,7 +6,6 @@ require 'stf/errors'
6
6
  require 'stf/model/session'
7
7
 
8
8
  class StartDebugSessionInteractor
9
-
10
9
  include Log
11
10
  include ADB
12
11
 
@@ -14,19 +13,19 @@ class StartDebugSessionInteractor
14
13
  @stf = stf
15
14
  end
16
15
 
17
- def execute(wanted, all_flag, filter, auto_adb_connect)
16
+ def execute(wanted, all_flag, filter)
18
17
  wanted = 1 if wanted.nil?
19
18
  wanted = wanted.to_i
20
19
 
21
20
  1..10.times do
22
- wanted -= connect(wanted, all_flag, filter, auto_adb_connect)
21
+ wanted -= connect(wanted, all_flag, filter)
23
22
  return if all_flag || wanted <= 0
24
23
  logger.info 'We are still waiting for ' + wanted.to_s + ' device(s). Retrying'
25
24
  sleep 5
26
25
  end
27
26
  end
28
27
 
29
- def connect(wanted, all_flag, filter, auto_adb_connect)
28
+ def connect(wanted, all_flag, filter)
30
29
  devices = @stf.get_devices
31
30
  if devices.nil? || (devices.is_a?(Array) && devices.empty?)
32
31
  logger.info 'No devices connected to STF'
@@ -34,8 +33,8 @@ class StartDebugSessionInteractor
34
33
  end
35
34
 
36
35
  usable_devices = devices
37
- .map {|d| Device.new(d)}
38
- .select do |d|
36
+ .map { |d| Device.new(d) }
37
+ .select do |d|
39
38
  d.ready == true && d.present == true && d.using == false
40
39
  end
41
40
 
@@ -59,47 +58,36 @@ class StartDebugSessionInteractor
59
58
 
60
59
  n = 0
61
60
  usable_devices.shuffle.each do |d|
62
- n += 1 if connect_device(d, auto_adb_connect)
61
+ n += 1 if connect_device(d)
63
62
  break if !all_flag && n >= wanted
64
63
  end
65
64
 
66
65
  n
67
66
  end
68
67
 
69
- def connect_device(device, auto_adb_connect)
70
- begin
71
- return false if device.nil?
72
-
73
- serial = device.serial
74
- success = @stf.add_device serial
75
- if success
76
- logger.info "Device #{serial} added"
77
- elsif logger.error "Can't add device #{serial}"
78
- return false
79
- end
80
-
81
- result = @stf.start_debug serial
82
- unless result.success
83
- logger.error "Can't start debugging session for device #{serial}"
84
- @stf.remove_device serial
85
- return false
86
- end
87
-
88
- logger.info "remoteConnectUrl: #{result.remoteConnectUrl}"
68
+ def connect_device(device)
69
+ return false if device.nil?
89
70
 
90
- if auto_adb_connect
91
- _execute_adb_with 30, "connect #{result.remoteConnectUrl}"
92
- end
93
- return true
71
+ serial = device.serial
72
+ success = @stf.add_device serial
73
+ if success
74
+ logger.info "Device #{serial} added"
75
+ elsif logger.error "Can't add device #{serial}"
76
+ return false
77
+ end
94
78
 
95
- rescue Net::HTTPFatalError
96
- logger.error 'Failed to start debug session'
79
+ result = @stf.start_debug serial
80
+ unless result.success
81
+ logger.error "Can't start debugging session for device #{serial}"
82
+ @stf.remove_device serial
97
83
  return false
98
84
  end
99
- end
100
85
 
101
- def _execute_adb_with(timeout, cmd)
102
- execute_adb_with timeout, cmd
103
- end
86
+ execute_adb_with 30, "connect #{result.remoteConnectUrl}"
87
+ return true
104
88
 
105
- end
89
+ rescue Net::HTTPFatalError
90
+ logger.error 'Failed to start debug session'
91
+ return false
92
+ end
93
+ end
@@ -0,0 +1,23 @@
1
+ require 'ADB'
2
+
3
+ require 'stf/client'
4
+ require 'stf/log/log'
5
+ require 'stf/errors'
6
+ require 'stf/model/session'
7
+
8
+ class UserInteractor
9
+ include Log
10
+ def initialize(stf)
11
+ @stf = stf
12
+ end
13
+
14
+ def execute(adb_public_key_location)
15
+ public_key = File.open(adb_public_key_location, 'rb', &:read)
16
+ success = @stf.add_adb_public_key public_key
17
+ if success
18
+ logger.info "adb public key from '#{adb_public_key_location}' has been added"
19
+ elsif logger.error "Can't add public key from '#{adb_public_key_location}'"
20
+ return false
21
+ end
22
+ end
23
+ end
data/lib/stf/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Stf
2
- VERSION = '0.1.7'
2
+ VERSION = '0.1.8'
3
3
  end
data/lib/stf/view/cli.rb CHANGED
@@ -9,6 +9,7 @@ module Stf
9
9
  require 'stf/interactor/remove_all_user_devices_interactor'
10
10
  require 'stf/interactor/get_keys_interactor'
11
11
  require 'stf/interactor/get_values_interactor'
12
+ require 'stf/interactor/user_interactor'
12
13
 
13
14
  include GLI::App
14
15
  extend self
@@ -24,15 +25,14 @@ module Stf
24
25
  desc 'URL to STF, can also be set by environment variable STF_URL'
25
26
  flag [:u, :url]
26
27
 
27
- pre do |global_options, command, options, args|
28
-
28
+ pre do |global_options, _command, _options, _args|
29
29
  global_options[:url] = ENV['STF_URL'] if global_options[:url].nil?
30
30
  global_options[:token] = ENV['STF_TOKEN'] if global_options[:token].nil?
31
31
 
32
32
  help_now!('STF url is required') if global_options[:url].nil?
33
33
  help_now!('Authorization token is required') if global_options[:token].nil?
34
34
 
35
- Log::verbose(global_options[:verbose])
35
+ Log.verbose(global_options[:verbose])
36
36
 
37
37
  $stf = Stf::Client.new(global_options[:url], global_options[:token])
38
38
  end
@@ -42,16 +42,15 @@ module Stf
42
42
  c.switch [:all]
43
43
  c.flag [:n, :number]
44
44
  c.flag [:f, :filter]
45
- c.switch :adb, default_value: true, desc: 'automatically execute adb connect'
46
45
 
47
- c.action do |global_options, options, args|
48
- StartDebugSessionInteractor.new($stf).execute(options[:number], options[:all], options[:filter], options[:adb])
46
+ c.action do |_global_options, options, _args|
47
+ StartDebugSessionInteractor.new($stf).execute(options[:number], options[:all], options[:filter])
49
48
  end
50
49
  end
51
50
 
52
- desc 'Show available keys for filtering'
51
+ desc 'Show avaliable keys for filtering'
53
52
  command :keys do |c|
54
- c.action do |global_options, options, args|
53
+ c.action do |_global_options, _options, _args|
55
54
  puts GetKeysInteractor.new($stf).execute
56
55
  end
57
56
  end
@@ -60,7 +59,7 @@ module Stf
60
59
  command :values do |c|
61
60
  c.flag [:k, :key]
62
61
 
63
- c.action do |global_options, options, args|
62
+ c.action do |_global_options, options, _args|
64
63
  if options[:key].nil?
65
64
  help_now!('Please specify the key (--key)')
66
65
  else
@@ -75,7 +74,7 @@ module Stf
75
74
  c.flag [:d, :device]
76
75
  c.switch [:all]
77
76
 
78
- c.action do |global_options, options, args|
77
+ c.action do |_global_options, options, _args|
79
78
  if options[:device].nil? && options[:all] == true
80
79
  StopAllDebugSessionsInteractor.new($stf).execute
81
80
  elsif !options[:device].nil? && options[:all] == false
@@ -87,12 +86,24 @@ module Stf
87
86
 
88
87
  desc 'Frees all devices that are assigned to current user in STF. Doesn\'t modify local adb'
89
88
  command :clean do |c|
90
- c.action do |global_options, options, args|
89
+ c.action do |_global_options, _options, _args|
91
90
  RemoveAllUserDevicesInteractor.new($stf).execute
92
91
  end
93
92
  end
94
93
 
94
+ desc 'Add current adb public key into STF (depends on https://github.com/openstf/stf/pull/770)'
95
+ command :trustme do |c|
96
+ c.flag [:k, :adb_public_key_location], desc: 'Location of adb public key', default_value: '~/.android/adbkey.pub'
97
+ c.action do |_global_options, options, _args|
98
+ options[:adb_public_key_location] = '~/.android/adbkey.pub' if options[:adb_public_key_location].nil?
99
+ filename = File.expand_path(options[:adb_public_key_location])
100
+ unless File.exist? filename
101
+ help_now!("File does not exist: '#{options[:adb_public_key_location]}'")
102
+ end
103
+ UserInteractor.new($stf).execute(options[:adb_public_key_location])
104
+ end
105
+ end
106
+
95
107
  exit run(ARGV)
96
108
  end
97
109
  end
98
-
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stf-client-neofreko
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akhmad Fathonih
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-25 00:00:00.000000000 Z
11
+ date: 2017-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gli
@@ -134,6 +134,7 @@ files:
134
134
  - lib/stf/interactor/start_debug_session_interactor.rb
135
135
  - lib/stf/interactor/stop_all_debug_sessions_interactor.rb
136
136
  - lib/stf/interactor/stop_debug_session_interactor.rb
137
+ - lib/stf/interactor/user_interactor.rb
137
138
  - lib/stf/log/log.rb
138
139
  - lib/stf/model/device.rb
139
140
  - lib/stf/model/session.rb