Class: Legion::CLI::Workflow

Inherits:
Thor
  • Object
show all
Defined in:
lib/legion/cli/workflow_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/legion/cli/workflow_command.rb', line 8

def self.exit_on_failure?
  true
end

Instance Method Details

#install(file) ⇒ Object



17
18
19
20
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
46
47
48
# File 'lib/legion/cli/workflow_command.rb', line 17

def install(file)
  out = formatter
  with_data do
    require 'legion/workflow/manifest'
    require 'legion/workflow/loader'

    unless File.exist?(file)
      out.error("File not found: #{file}")
      raise SystemExit, 1
    end

    manifest = Legion::Workflow::Manifest.new(path: file)
    unless manifest.valid?
      manifest.errors.each { |e| out.error(e) }
      raise SystemExit, 1
    end

    result = Legion::Workflow::Loader.new.install(manifest)

    if result[:success]
      if options[:json]
        out.json(result)
      else
        out.success("Workflow '#{manifest.name}' installed " \
                    "(chain_id=#{result[:chain_id]}, #{result[:relationship_ids].size} relationships)")
      end
    else
      out.error("Install failed: #{result[:error]}")
      raise SystemExit, 1
    end
  end
end

#listObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/legion/cli/workflow_command.rb', line 51

def list
  out = formatter
  with_data do
    require 'legion/workflow/loader'

    workflows = Legion::Workflow::Loader.new.list
    if options[:json]
      out.json(workflows)
    else
      rows = workflows.map { |w| [w[:id].to_s, w[:name].to_s, w[:relationships].to_s] }
      out.table(%w[chain_id name relationships], rows)
    end
  end
end

#status(name) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/legion/cli/workflow_command.rb', line 96

def status(name)
  out = formatter
  with_data do
    require 'legion/workflow/loader'

    result = Legion::Workflow::Loader.new.status(name)

    if result[:success]
      if options[:json]
        out.json(result)
      else
        puts "Workflow: #{result[:name]} (chain_id=#{result[:chain_id]})"
        rows = result[:relationships].map do |r|
          [r[:id].to_s, r[:name].to_s, r[:trigger].to_s, r[:action].to_s,
           r[:conditions] ? 'yes' : 'no', r[:active] ? 'active' : 'inactive']
        end
        out.table(%w[id name trigger action conditions active], rows)
      end
    else
      out.error("Workflow '#{name}' not found")
      raise SystemExit, 1
    end
  end
end

#uninstall(name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/legion/cli/workflow_command.rb', line 69

def uninstall(name)
  out = formatter
  with_data do
    require 'legion/workflow/loader'

    unless options[:confirm]
      out.warn("This will delete workflow '#{name}' and all its relationships")
      print '  Continue? [y/N] '
      response = $stdin.gets&.chomp
      unless response&.downcase == 'y'
        out.warn('Aborted')
        return
      end
    end

    result = Legion::Workflow::Loader.new.uninstall(name)

    if result[:success]
      out.success("Workflow '#{name}' uninstalled (#{result[:deleted_relationships]} relationships removed)")
    else
      out.error("Workflow '#{name}' not found")
      raise SystemExit, 1
    end
  end
end