Class: Speedflow::Commands::Flow

Inherits:
Speedflow::Command show all
Extended by:
Message
Defined in:
lib/speedflow/commands/flow.rb

Overview

Flow command

Class Method Summary collapse

Methods included from Message

error, info, message, notice, success

Methods inherited from Speedflow::Command

config_from_fresh_options, config_from_options, execute_from_option, inherited, invalid_command, subclasses

Class Method Details

.add_options_from_flow(program, command) ⇒ Object

Add options from flow arguents.

program - the program. command - the command.

Returns nothing.



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/speedflow/commands/flow.rb', line 72

def add_options_from_flow(program, command)
  config = config_from_fresh_options(program, command)
  flow = Speedflow::Flow.new(config)
  flow.flat_arguments.each do |argument|
    program.option(
      argument,
      '',
      "--#{argument} #{argument.capitalize}",
      String,
      'Option form flow.')
  end
end

.init_with_program(program) ⇒ Object

Init command with program.

prog - the program.

Returns nothing.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/speedflow/commands/flow.rb', line 13

def init_with_program(program)
  program.command(:flow) do |command|
    command.syntax 'flow [trigger]'
    command.description 'Play with the flow, trigger some actions.'

    add_options_from_flow(program, command)

    command.action do |args, options|
      config = config_from_options(options)
      trigger = (args.first || '').to_sym
      process(trigger, config, args, options)
    end
  end
end

.invalid_trigger(trigger) ⇒ Object

Display invalid trigger message.

trigger - the trigger name.

Returns nothing.



90
91
92
# File 'lib/speedflow/commands/flow.rb', line 90

def invalid_trigger(trigger)
  error "Trigger not found '#{trigger}'."
end

.process(trigger, config, args, options) ⇒ Object

Process command.

trigger - Trigger name. config - Configuration. args - Arguments. options - Options.

Returns nothing.



36
37
38
39
40
41
42
43
44
45
# File 'lib/speedflow/commands/flow.rb', line 36

def process(trigger, config, args, options)
  if args.empty? || !config.flow_triggers?
    valid_triggers(config['flow'].keys)
  elsif !config.flow_trigger?(trigger)
    invalid_trigger(trigger)
    valid_triggers(config['flow'].keys)
  else
    process_trigger(trigger, config, options)
  end
end

.process_trigger(trigger, config, options) ⇒ Object

Process trigger.

trigger - Trigger name. config - Configuration. options - Options.

Returns nothing.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/speedflow/commands/flow.rb', line 54

def process_trigger(trigger, config, options)
  flow = Speedflow::Flow.new(config)
  flow.trigger(trigger, options)
  success "Trigger '#{trigger}' successful"
rescue FlowTriggerNotFound => exception
  error "Trigger '#{trigger}' error: #{exception.message}"
rescue Plugin::PluginNotFound => exception
  error "Plugin error: #{exception.message}"
rescue Plugin::PluginActionNotFound => exception
  error exception.message
end

.valid_triggers(triggers) ⇒ Object

Display valid trigger message.

triggers - Array of triggers.

Returns nothing.



99
100
101
102
# File 'lib/speedflow/commands/flow.rb', line 99

def valid_triggers(triggers)
  info 'List of available triggers:'
  message triggers.join(', ')
end