Class: NliPipeline::SetupPipeline
- Inherits:
-
Object
- Object
- NliPipeline::SetupPipeline
- Defined in:
- lib/nli_pipeline/setup_pipeline.rb
Overview
Class used to parse arguments from bin/setup_pipeline executable
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#dir_error ⇒ Object
readonly
Returns the value of attribute dir_error.
-
#docker_commands ⇒ Object
readonly
Returns the value of attribute docker_commands.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#delegate_to_managers(flags, command) ⇒ Object
handle what manager object to create based on flags / command.
-
#flags_or_help_screen ⇒ Object
handle case where invalid flag is passed show user help screen.
-
#help(opts) ⇒ Object
print help screen to console.
-
#initialize ⇒ SetupPipeline
constructor
Set up class attributes.
-
#main ⇒ Object
Method called by bin/setup_pipeline Parse commands (command line arguments) and pass flags / flags to parse_flags.
-
#parse_args ⇒ Object
Commandline Arguments: $1 (file path) (required) $2 (command to run).
-
#parse_flags ⇒ Object
Commandline Flags: $1 (file path) (required) –version –extension=(string: file extension) –debug (show all system commands when enabled).
-
#show_commands ⇒ Object
simple formatter.
Constructor Details
#initialize ⇒ SetupPipeline
Set up class attributes
14 15 16 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 |
# File 'lib/nli_pipeline/setup_pipeline.rb', line 14 def initialize @version = NliPipeline::VERSION @commands = { undo: proc { |fm| fm.load_from_backup }, show_last_created: proc { |fm| puts(fm.last_created_files.to_s) } } @docker_commands = { docker_build: proc { |dm| dm.build }, docker_build_commit: proc { |dm| dm.build_commit? }, docker_build_and_save: proc { |dm| dm.build_and_save }, docker_deploy_branch: proc { |dm| dm.deploy_branch }, docker_deploy_master: proc { |dm| dm.deploy_master }, docker_exec: proc { |dm| dm.docker_exec }, docker_run: proc { |dm| dm.run }, docker_save: proc { |dm| dm.save } } @dir_error = " The first argument to setup_pipeline must be a valid directory. For example, setting up a pipeline in the current directory: setup_pipeline $PWD The exceptions to this are:\n setup_pipeline --version setup_pipeline --help setup_pipeline --show-commands setup_pipeline --show-flags " end |
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
11 12 13 |
# File 'lib/nli_pipeline/setup_pipeline.rb', line 11 def commands @commands end |
#dir_error ⇒ Object (readonly)
Returns the value of attribute dir_error.
11 12 13 |
# File 'lib/nli_pipeline/setup_pipeline.rb', line 11 def dir_error @dir_error end |
#docker_commands ⇒ Object (readonly)
Returns the value of attribute docker_commands.
11 12 13 |
# File 'lib/nli_pipeline/setup_pipeline.rb', line 11 def docker_commands @docker_commands end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/nli_pipeline/setup_pipeline.rb', line 11 def @options end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
11 12 13 |
# File 'lib/nli_pipeline/setup_pipeline.rb', line 11 def version @version end |
Instance Method Details
#delegate_to_managers(flags, command) ⇒ Object
handle what manager object to create based on flags / command
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/nli_pipeline/setup_pipeline.rb', line 176 def delegate_to_managers(flags, command) if @docker_commands.key?(command) # always fail on error if anything goes wrong with a deploy flags[:fail_on_error] = true if command.to_s.include?('deploy') dm = NliPipeline::DockerManager.new(**flags) @docker_commands[command].call(dm) # if not DockerManager, use FileManager else fm = NliPipeline::FileManager.new(**flags) if @commands.key?(command) @commands[command].call(fm) # is no command is specified, copy example files else fm.copy_example_files end end end |
#flags_or_help_screen ⇒ Object
handle case where invalid flag is passed show user help screen
158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/nli_pipeline/setup_pipeline.rb', line 158 def flags_or_help_screen begin flags = parse_flags rescue OptionParser::InvalidOption => e puts(e.to_s.red) # inject --help as first arg ARGV.insert(0, '--show-flags') # call flags again to display help dialogue parse_flags # re-throw error raise e end flags end |
#help(opts) ⇒ Object
print help screen to console
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/nli_pipeline/setup_pipeline.rb', line 57 def help(opts) how_to_use = "HOW TO USE:\n setup_pipeline $PWD #{opts} FileManager commands: #{@commands.keys} DockerManager commands: #{@docker_commands.keys} " errors = "COMMON ERRORS:\n #{@dir_error} " docs = "FOR MORE INFORMATION PLEASE READ THE DOCS:\n on bitbucket:\t\thttps://bitbucket.org/nlireland/nli-pipeline-gem\n or on ruby gems:\t\thttps://rubygems.org/gems/nli_pipeline\n " puts("#{how_to_use}\n#{errors}\n#{docs}") end |
#main ⇒ Object
Method called by bin/setup_pipeline Parse commands (command line arguments) and pass flags / flags to parse_flags
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/nli_pipeline/setup_pipeline.rb', line 200 def main # byebug flags = flags_or_help_screen # flags will be false if a flag that exits early is passed e.g. false # otherwise flags will be some kind of hash, all of which are truthy, even {} return false unless flags # TODO # document this better or # get rid of commands? use separate executables in bin for FileManager vs DockerManager? # command is the second argument # e.g. setup_pipeline $PWD COMMAND_GOES_HERE --flags --go --here args = parse_args flags[:path] = args[:path] command = args[:command] delegate_to_managers(flags, command) end |
#parse_args ⇒ Object
Commandline Arguments:
$1 (file path) (required)
$2 (command to run)
147 148 149 150 151 152 153 154 |
# File 'lib/nli_pipeline/setup_pipeline.rb', line 147 def parse_args raise ArgumentError, "\n#{@dir_error}\n" unless ARGV[0] && Dir.exist?(ARGV[0]) args = {} args[:path] = ARGV[0] args[:command] = ARGV[1].to_sym unless ARGV[1].nil? || ARGV[1].empty? args end |
#parse_flags ⇒ Object
Commandline Flags:
$1 (file path) (required)
--version
--extension=(string: file extension)
--debug (show all system commands when enabled)
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/nli_pipeline/setup_pipeline.rb', line 86 def parse_flags = {} OptionParser.new do |opts| @options = opts # break out of function, don't throw error about passing directory path opts.on('-h', '--help') do |_v| help(opts) return false end opts.on('--show-commands') do |_v| show_commands return false end opts.on('--show-flags') do |_v| puts(self.) return false end opts.on('-v', '--version') do |_v| puts(self.version) return false end opts.on('-cmds=', '--commands=a,b,c', Array, 'Array of bash commands') do |v| [:bash_commands] = v end opts.on('--debug') do |v| [:debug] = v end opts.on("--extension=[[\w\.]+]", String, 'String file extension') do |v| [:extension] = v end # override git branch (for docker deploy) opts.on("--git-branch=[[\w\.]+]", String, 'String git branch') do |v| [:git_branch] = v end opts.on('--fail-on-error') do |v| [:fail_on_error] = v end opts.on("--image=[([\w\-\.\:]+)]", String, 'String docker image') do |v| [:image_name] = v end opts.on("--mount=[([\w\-\.\:]+)]", String, 'String directory to mount') do |v| [:mount] = v end opts.on('--ports=a,b,c', Array, 'Array of ports') do |v| [:ports] = v end opts.on("--proxy=[([\w\-\.\:]+)]", String, 'String proxy') do |v| [:proxy] = v end opts.on("--upstream-image=[([\w\-\.\:]+)]", String, 'String dockerhub image') do |v| [:upstream_image_name] = v end end.parse! end |
#show_commands ⇒ Object
simple formatter
48 49 50 51 52 53 |
# File 'lib/nli_pipeline/setup_pipeline.rb', line 48 def show_commands puts('FileManager commands') puts(@commands.keys) puts('DockerManager commands') puts(@docker_commands.keys) end |