Module: Wavesync::Commands

Defined in:
lib/wavesync/commands.rb,
lib/wavesync/commands/help.rb,
lib/wavesync/commands/pull.rb,
lib/wavesync/commands/sync.rb,
lib/wavesync/commands/analyze.rb,
lib/wavesync/commands/command.rb,
lib/wavesync/commands/setlist.rb,
lib/wavesync/commands/clear_cache.rb

Defined Under Namespace

Classes: Analyze, ClearCache, Command, Help, Option, Pull, Setlist, Subcommand, Sync

Constant Summary collapse

CONFIG_OPTION =
Option.new(short: '-c', long: '--config PATH', description: 'Path to wavesync config YAML file')
GLOBAL_OPTIONS =
[CONFIG_OPTION].freeze
ALL =
[Sync, Pull, Analyze, Setlist, ClearCache, Help].freeze

Class Method Summary collapse

Class Method Details

.load_config(path) ⇒ Object

: (String path) -> Config



13
14
15
16
17
18
# File 'lib/wavesync/commands.rb', line 13

def self.load_config(path)
  Wavesync::Config.load(path)
rescue Wavesync::ConfigError => e
  puts "Configuration error: #{e.message}"
  exit 1
end

.resolve_device_pairs(config, device_name: nil) ⇒ Object

: (Config config, ?device_name: String?) -> Array



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/wavesync/commands.rb', line 21

def self.resolve_device_pairs(config, device_name: nil)
  device_configs = config.device_configs
  if device_name
    device_configs = device_configs.select { |device_config| device_config[:name] == device_name }
    if device_configs.empty?
      known = config.device_configs.map { |device_config| device_config[:name] }.join(', ')
      puts "Unknown device \"#{device_name}\". Devices in config: #{known}"
      exit 1
    end
  elsif device_configs.size > 1
    device_names = device_configs.map { |device_config| device_config[:name] }
    selected_name = Wavesync::UI.new.select('Select device', device_names)
    device_configs = device_configs.select { |device_config| device_config[:name] == selected_name }
  end

  device_configs.map do |device_config|
    device = Wavesync::Device.find_by(name: device_config[:model])
    unless device
      supported = Wavesync::Device.all.map(&:name).join(', ')
      puts "Unknown device model \"#{device_config[:model]}\" in config. Supported models: #{supported}"
      exit 1
    end
    [device_config, device]
  end
end

.with_mtp_retry(transport, device_name, &block) ⇒ Object

: ((Wavesync::Transport::Filesystem | Wavesync::Transport::Mtp) transport, String device_name) { () -> void } -> void



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/wavesync/commands.rb', line 48

def self.with_mtp_retry(transport, device_name, &block)
  return block.call unless transport.is_a?(Wavesync::Transport::Mtp)

  begin
    block.call
  rescue Wavesync::Libmtp::Error => e
    puts "Could not reach #{device_name} over MTP (#{e.message}). Put the device in MTP mode, then press Enter to retry (Ctrl+C to abort)."
    $stdin.gets
    retry
  end
end