Class: Factor::Commands::RegistryCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/commands/registry_command.rb

Overview

Workflow is a Command to start the factor runtime from the CLI

Constant Summary

Constants inherited from Command

Command::DEFAULT_FILENAME

Instance Attribute Summary

Attributes inherited from Command

#logger

Instance Method Summary collapse

Methods inherited from Command

#initialize, #load_config, #save_config

Constructor Details

This class inherits a constructor from Factor::Commands::Command

Instance Method Details

#add_connector(args, options) ⇒ Object



31
32
33
34
35
# File 'lib/commands/registry_command.rb', line 31

def add_connector(args, options)
  puts "Workflow ID is required (factor registry connector add --help)".red unless args[0]

  setup_connector args[0].to_s, options if args[0]
end

#add_workflow(args, options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/commands/registry_command.rb', line 37

def add_workflow(args, options)
  begin
    list          = get_yaml_data 'https://raw.githubusercontent.com/factor-io/index/master/workflows.yml'
  rescue
    puts "Couldn't connect to the server to get connector information".red
    exit
  end

  unless args[0]
    puts "Workflow ID is required".red
    exit
  end

  begin
    workflow_id   = args[0].to_s
    workflow_info = list[workflow_id]
    config_url    = workflow_info['config']
    workflow_name = workflow_info['name']
  rescue
    puts "Couldn't find information for #{workflow_id}".red
    exit
  end

  load_config(credentials:options.credentials, connectors:options.connectors)

  begin
    config = get_json_data(config_url)
  rescue
    puts "Couldn't pull up configuration information from #{config_url}".red
    exit
  end

  if !config['required-connectors'] || !config['required-connectors'].is_a?(Array) || !config['variables'] || !config['variables'].is_a?(Hash)
    puts "Configuration information for the workflow is missing information"
    exit
  end

  config['required-connectors'].each do |connector_id|
    if configatron.credentials.to_hash[connector_id.to_sym]
      puts "#{connector_id} already configured".green
    else
      setup_connector(connector_id,options)
    end
  end

  variables = {}
  config['variables'].each do |var_id,var_info|
    values = begin
      JSON.parse(options.values)
    rescue 
      {}
    end
    if values[var_id]
      variables[var_id.to_s] = values[var_id]
    else
      puts var_info['description'] if var_info['description']
      variables[var_id.to_s] = ask("#{var_info['name'].bold}#{' (optional)' if var_info['optional']}: ").to_s
    end
  end

  begin
    template = RestClient.get(config['template'])
  rescue
    puts "Couldn't find a template at #{config['template']}".red
    exit
  end

  begin
    liquid = Liquid::Template.parse(template)
    workflow_content = liquid.render variables
  rescue
    puts "Failed to generate template".red
    exit
  end

  workflow_filename = "workflow-#{workflow_id}.rb"
  begin
    File.write(workflow_filename, workflow_content)
  rescue
    puts "Failed to write the file to disk. Check permissions.".red
    exit
  end


  puts "Created '#{workflow_name}' successfully".green
end

#connectors(args, options) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/commands/registry_command.rb', line 23

def connectors(args, options)
  list = get_yaml_data 'https://raw.githubusercontent.com/factor-io/index/master/connectors.yml'

  list.each do |id, values|
    puts "#{values['name'].bold} (#{id})"
  end
end

#workflows(args, options) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/commands/registry_command.rb', line 15

def workflows(args, options)
  list = get_yaml_data 'https://raw.githubusercontent.com/factor-io/index/master/workflows.yml'

  list.each do |id, values|
    puts "#{values['name'].bold} (#{id}): #{values['description']}"
  end
end