How to avoid generating content with quotes from an erb template? - ruby

I have written an erb template which I'm using to generate a Dockerfile dynamically. Here is how the whole thing works:
Take the input from command line.
Create an ruby array out of the input.
Use the Dockerfile.erb to generate Dockerfile based of this input.
The problem that I face here is that when the input is copied onto the generated Dockerfile, it has quotes around it which I need to avoid. Is there a way to do so?
Dockerfile.erb
FROM docker.cernerrepos.net/alpine:3.7 AS builder
LABEL bundles=<%= keys[:bundles] %>
generate.rb
require 'optparse'
require 'erb'
DEBUG_PREFIX = '>>> generate_dockerfile.rb ->'
#options = {}
#options[:output]= '.'
optparse = OptionParser.new do |opts|
opts.banner = 'Usage: generate_dockerfile.rb [options]'
opts.on('-b [BUNDLES]', '--bundles [BUNDLES]', "Comma separated list of bundles without extension and version.") do |bundles|
#options[:bundles] = bundles.split(',')
end
opts.on('-h', '--help', 'Display this Help') do
puts opts
exit
end
end
optparse.parse!
keys = #options
File.open('Dockerfile', 'w+') do |f|
f.write(ERB.new(File.read('Dockerfile.erb'), nil, '-').result(binding))
end
Actual Output Dockerfile
FROM docker.cernerrepos.net/alpine:3.7 AS builder
LABEL bundles=["sample_bundle1", "sample_bundle2"]
Expected Output Dockerfile
FROM docker.cernerrepos.net/alpine:3.7 AS builder
LABEL bundles=[sample_bundle1, sample_bundle2]
Command to Execute
ruby generate.rb -b sample_bundle1,sample_bundle2
Edit 1: Modified the question to include the minimum sample code, the expected out and the actual output and the command to run the code.

Right here:
LABEL bundles=<%= keys[:bundles] %>
You are basically calling keys[:bundle].to_s and interpolating that in the result.
What's ["foo", "bar"].to_s? It's ["foo", "bar"] (with quotes!)
That's why you're getting this result.
You can change it to the following, if you want to remove the quotations:
LABEL bundles=[<%= keys[:bundles].join(", ") %>]
Not to say this is the wrong approach, but isn't really a typical thing to do, so I don't think there's another built-in way to do this.

Related

Parse multiple command line options in Ruby using OptionParser

I've just started using OptionParser for Ruby and I wanted to use flags that would use more than just one argument.
For instance, I would like to be able to run:
script --move src dst
Note how src and dst are not separated using a coma.
My initial idea was:
opts.on("-m src dst", "--move src dst ", "move file from SRCto DST") do |src|
# do something
end
But this is not working. I assume that this is not the right approach. But how could this be done?
The example under the "Complete Example" section of the OptionParser details how a list of items can be accepted.
Here is a sample program based on that example. The third parameter Array in opts.on indicates that input src, dst should be used to create an array. To run this sample, you need to do gem install trollop.
# test.rb
require 'optparse'
options = {}
OptionParser.new do |opt|
opt.on("-m src, dst", "--move src, dst", Array, "Move from src to dst") do |list|
options[:src] = list[0]
options[:dst] = list[1]
end
end.parse!
puts options # It's a hash of parsed options
Sample run:
> ruby test.rb -m from,to
{:src=>"src", :dst=>"dst"}
>ruby test.rb -h
Usage: test [options]
-m, --move src, dst Move from src to dst
The above script forces one to separate the options using comma.
As indicated by "Really Cheap Command-Line Option Parsing in Ruby", there seems to be a gem, trollop, that can be quite easy to use for command-line parsing.
A sample program based on Trollop is given below, which allows usage of spaces for specifying options with multiple values
# test.rb
require "trollop"
opts = Trollop::options do
banner "Command line parsing using Trollop"
opt :move, "--move src dst', Move from src to dst", :short => "-m", :long => "--move", :type => :strings
end
# An array of option values
p opts.move
Sample run:
>ruby test.rb -m hello world
["hello", "world"]
>ruby test.rb -h
Command line parsing using Trollop
-m, --move=<s+> '--move src dst', Move from src to dst
-h, --help Show this message
There is a subtle difference between the help output between the two approaches. Trollop produces help text where --move=<s+> does not indicate clearly that it needs accepts two values, so I had to repeat the command syntax description.
OptionParser doesn't support that; It could be patched to do so, but I'm not sure it's worth the trouble.
Consider this code:
require 'optparse'
options = {}
OptionParser.new do |opt|
opt.on('-m', '--move') { |o| options[:move] = o }
end.parse!
from_name, to_name = ARGV
puts "Should move: #{ options.key?(:move) }"
puts "From: #{ from_name }"
puts "To: #{ to_name }"
Saving it and running it with various combinations of the parameters returns:
> ruby test.rb --move from to
Should move: true
From: from
To: to
> ruby test.rb from to
Should move: false
From:
To:
If the code is supposed to move files by default then don't bother with the --move flag, simply use:
test.rb from to
and consider removing the OptionParser block entirely.
If the code is supposed to normally copy with the option to move, then --move becomes more sensible to act as a flag that moving is desired.
ruby test.rb --move from to
I'd have code that tests for options[:move] and run the code to move instead of copy at that point.
In either case, the filenames shouldn't be tied to the flag, they should be supplied separately and retrieved from ARGV after OptionParser has finished parsing the command-line and removing entries it's handled.

Optparse doesn't seem to return ARGV array. Argument Required Error

This is homework and I do not expect you to give me the complete answer.
I'm trying to parse a command line entry such as:
./apacheReport.rb -u testlog.txt
When I enter this:
./apacheReport.rb -u testlog.txt
I get:
Argument required
My code is:
require_relative 'CommonLog'
require 'optparse'
# puts ARGV.inspect
optparser = OptionParser.new
optU = false
optI = false
optS = false
optH = false
optparser.banner = "apacheReport.rb [options] filename"
optparser.parse!
rescue => m
puts m.message
puts optparser
exit
end
if ARGV.length < 1
puts "Argument required"
exit
end
userInputFile = ARGV[0]
userInputFile.to_s
file = CommonLog.new(userInputFile)
It should parse the leftover portion of the command into ARGV[0] then should store it as userInputFile and then create a CommonLog object using the file as the constructor. At that point I call the methods that were specified in the command.
It seems that for some reason my ARGV is not being returned. I'm not sure what the issue is.
Ruby's OptionParser is easy to use, but you have to puzzle through the documentation. Here's a little example that'd be useful for your code:
require 'optparse'
options = {}
OptionParser.new do |opt|
opt.on('-u', '--use_this FILE', 'Use this file') { |o| options[:use_this] = o }
end.parse!
options will contain the flags. In this case, if you pass in -u foo, options[:use_this] will be foo.
Save that and try running it without and with a parameter. Also try running it with just a -h flag.
You can search StackOverflow for more answers where I was dealing with OptionParser.
It's hard to tell what's wrong since you code doesn't seem to be working at the moment. The problem may be that the parse! method removes found options from ARGV. So when you write:
optparser.parse!
It removes your two parameters (-u testlog.txt) and this code always fails:
if ARGV.length < 1
puts "Argument required"
exit
end
Instead of looking at ARGV, you need to set up optparser correctly. Perhaps something like:
optparser = OptionParser.new do |opts|
opts.banner = "apacheReport.rb [options] filename"
opts.on("-u", "--u-short-for-this", "Whatever u stands for") do |u|
optU = u
end
end
Then optU will be true only if the user passed -u and the filename will be in ARGV[0].

Debugging in rails Console

I have this script that i would like to test within the rails console
Gem.find_files("models/*.rb").each do |f|
filename = File.basename(f, '.*')
class_name_symbol = filename.classify.to_sym
autoload class_name_symbol, "models/#{filename}"
end
what i would like to do is print out the results in the console but can only get as far as outputting the array using
Gem.find_files("models/*.rb")
which returns this
["/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/portfolio_sector.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/post.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/image.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/message.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/sector.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/portfolio.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/category.rb"]
tips appreciated
After using answer the output is
models/portfolio_sector
models/post
models/message
models/sector
models/portfolio
models/category
=> ["/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/portfolio_sector.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/post.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/image.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/message.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/sector.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/portfolio.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/category.rb"]
not sure why the array is at the end?
Edit :
The script is supposed to take each item in the array and autoload the file contained in models/#{filename}. I would like to print out all the model/#{filename} paths in the console to ensure they are correct –
Gem.find_files("models/*.rb").each do |f|
filename = File.basename(f, '.*')
# So, instead of passing this filename to autoload, you print it. Or do both.
puts "models/#{filename}"
# class_name_symbol = filename.classify.to_sym
# autoload class_name_symbol, "models/#{filename}"
end && nil # suppress return value from `each`

OptionParser to parse arguments form file instead of command line

I am using Ruby to execute a code that takes command line arguments.
now i trying to use the same program with differnt options so i am putting the options in a file and i want the program to read each line interpret the options and execute the program accordingly.
but i get this error. "C:/Ruby193/lib/ruby/1.9.1/optparse.rb:1348:in block in parse_in_order': undefined methodshift' for "--c execue --query unix --Servername abc123":String (NoMethodError)"
i understand that its reading the file and treating the line as string. but wondering if there is a way to overcome this shift error and treat the line as if it was entered in command prompt. or any better solution.
here is my code.
require 'optparse'
require 'micro-optparse'
# --command execue --query unix command --Servername abc123
f =File.open("list_of_commands.txt", "r")
f.each_line { |line|
line= line.chomp
#line = "--c execue --query unix --Servername abc123"
#line = eval("\"#{line}\"")
puts line
options = {}
OptionParser.new do |opts|
opts.on("-c", "--command result,execue,chart,scpfile", String, "Single command to execute ") do |c|
options[:comd] = c
end
opts.on("-q", "--query remote command, unix command", String, "performs the command on local or remote machine") do |q|
options[:query] = q
end
opts.on("-s", "--servername CHSXEDWDC002 ", String, "server name to execute the command") do |v|
options[:hname] = v
end
opts.on_tail('-h', '--help', 'Show this message') do
puts opts
exit
end
end.parse!(line)
p options
}
the contents of the file are below
--c execue --query unix --Servername abc123
i also tried to use micro-optparse but facing same error. any workaround ?
Update:
as per suggestion from "#mu is too short" i tried below options.
end.parse!("#{Shellwords.shellsplit(line)}") and/or
end.parse!(Shellwords.shellsplit(line)).
but none of them worked.
i also tried to split the line as array using "line = line.split("\t")" and then
end.parse!(line). out put as
--c execue
--query unix
--Servername abc123
but now i get error as block in : invalid option --c execute
Update:#2
looking at the error, the issue is with the wrong parameter(-c. but thanks to user "#mu is too short" for suggesting to use Array.
Update: 3
passing the array only worked for short form of the arguments such as -c but when long form was supplied it failed with invalid argument erorr.
i dont see much documentation on the optparse. i even tried micro-parse but it requres default valuves and its not an option for me :(
The parse! method wants an array as its argument, not a string. You'll probable want to use Shellwords.shellsplit rather than String#split (or similar hand-rolled method) to convert your line to an array just in case you have to deal with quoting and whatnot. Something like this:
OptionParser.new do |opts|
#...
end.parse!(Shellwords.shellsplit(line))
While you can put your command-line arguments into a file, flags and all, there are better ways to remember configuration settings.
Instead of storing the flags, use a YAML file. YAML is a great data format, that translates easily to Ruby hashes and objects. "Yaml Cookbook" is a very useful page for learning the ins and outs of the format with Ruby. There are YAML parsers for a myriad other languages, making it easy to share the settings, which can be useful as a system grows.
With a little creative code you can use your YAML as the base settings, and let your CLI flags override the stored settings.
If you're not familiar with YAML, it's easy to get a start on the file using something like:
require 'yaml'
data = {
'command' => %w[result execute chart scpfile],
'query' => ['remote command', 'unix command'],
'servername' => 'CHSXEDWHDC002',
}
puts data.to_yaml
Which outputs:
---
command:
- result
- execute
- chart
- scpfile
query:
- remote command
- unix command
servername: CHSXEDWHDC002
Redirect that output to a file ending in .yaml and you're on your way.
To read it back into a script use:
require 'yaml'
data = YAML.load_file('path/to/data.yaml')
A quick round-trip test shows:
require 'yaml'
data = {
'command' => %w[result execute chart scpfile],
'query' => ['remote command', 'unix command'],
'servername' => 'CHSXEDWHDC002',
}
YAML.load(data.to_yaml)
Which looks like:
{"command"=>["result", "execute", "chart", "scpfile"],
"query"=>["remote command", "unix command"],
"servername"=>"CHSXEDWHDC002"}
If you want to have defaults, stored in the YAML file, and override them with command-line flags, read the data from the file then use that resulting object as the base for OptionParse:
require 'optparse'
require 'yaml'
# Note, YAML can deal with symbols as keys, but other languages might not like them.
options = {
:comd => %w[result execute chart scpfile],
:query => ['remote command', 'unix command'],
:hname => 'CHSXEDWHDC002',
}
# we'll overwrite the options variable to pretend we loaded it from a file.
options = YAML.load(options.to_yaml)
OptionParser.new do |opts|
opts.on("-c", "--Command result,execue,chart,scpfile", String, "Single command to execute ") do |c|
options[:comd] = c
end
opts.on("-q", "--query remote command, unix command", String, "performs the command on local or remote machine") do |q|
options[:query] = q
end
opts.on("-s", "--Servername CHSXEDWHDC002 ", String, "server name to execute the command") do |v|
options[:hname] = v
end
opts.on_tail('-h', '--help', 'Show this message') do
puts opts
exit
end
end.parse!
That's not tested, but we do similar things at work all the time, so save it to a file and poke at it with a stick for a while, and see what you come up with.

Extracting filenames from command line arguments with Ruby

I'm trying to use optparse to parse command line arguments. I would like my program to accept arguments like that:
$ ./myscript.rb [options] filename
I can easily manage the [options] part:
require 'optparse'
options = { :verbose => false, :type => :html }
opts = OptionParser.new do |opts|
opts.on('-v', '--verbose') do
options[:verbose] = true
end
opts.on('-t', '--type', [:html, :css]) do |type|
options[:type] = type
end
end
opts.parse!(ARGV)
But how do I get the filename?
I could extract it manually from ARGV, but there has to be a better solution, just can't figure out how
The "parse" method returns the unprocessed ARGV. So in your example, it will return a one element array with the filename in it.
I can't just use ARGV.pop. For example
when the last argument is "css" it
could either be a file or belong to
--type switch.
But if your script requires the last argument to be a filename (which is what your usage output inquires) this case should never happen the script should exit with a non-zero and the user should get a usage report or error.
Now if you want to make a default filename or not require a filename as the last argument but leave it optional then you could just test to see if the last argument is a valid file. If so use it as expected otherwise continue without etc.
Hope this answer can still be useful.
Ruby has one built-in variable __FILE__ can do this type of work.
puts __FILE__
it will print out your file's name.
I don't think extracting it before sending it to OptionParser is bad, I think it makes sense. I probably say this because I have never used OptionParser before, but oh well.
require 'optparse'
file = ARGV.pop
opts = OptionParser.new do |opts|
# ...
end

Resources