I have two Ruby files:
db/migrate/20120920180336_create_admin_pages.rb
db/migrate/20120920150716_devise_create_users.rb
There's a constant variable in this file: _create_admin_pages.rb
How can I find it in a file and make the following command work right?
gsub_file "db/migrate/*_create_admin_pages.rb", "t.boolean :published", "t.boolean :published, :default => false"
ruby -pe 'gsub("t.boolean :published", "t.boolean :published, :default => false")' < db/migrate/*_create_admin_pages.rb
Ruby can run with several options. Here the -pe option (actually two options) let Ruby execute this loop:
Until no more input:
read a line of input,
run the string after -pe as code,
print the result.
All using the implicit variable $_, the last read line.
You can try this:
gsub_file Dir['db/migrate/*_create_admin_pages.rb'].first, "t.boolean :published", "t.boolean :published, :default => false"
Related
I want to call Ruby function from command line with array as an argument.
Script name is test.rb
In below code Environments are like test,dev,uat.', am passing as ['test','dev','uat']
I have tried as below:
ruby -r "./test.rb" -e "start_services '['dev','test','uat']','developer','welcome123'"
def start_services(environments,node_user_name,node_password)
environments.each do |env|
puts env
end
puts node_user_name
puts node_password
end
Output:
-e:1: syntax error, unexpected tIDENTIFIER, expecting end-of-input start_services '['dev','test','uat']','developer',' ^
You clearly want to pass an array as the first parameter into start_services method, so you should do it like this:
$ ruby -r "./test.rb" -e "start_services ['dev','test','uat'],'developer','welcome123'"
# output:
dev
test
uat
developer
welcome123
What you've been trying so far was attempt to pass '[\'dev\',\'test\',\'uat\']' string, which was malformed, because you didn't escape ' characters.
Don't pass your credentials as arguments, any user on your system would be able to see them.
Instead, you could save them as environment variables or in a config file.
if ARGV.size == 0
puts "Here's how to launch this script : ruby #{__FILE__} env_name1 env_name2 ..."
exit
end
# Define those environment variables before launching the script.
# Alternative : write credentials in a json or yml file.
node_username = ENV["NODE_USERNAME"]
node_password = ENV["NODE_PASSWORD"]
ARGV.each do |env|
puts "Launching environment #{env}"
end
I have the following code:
#!/usr/bin/env ruby
require 'yaml'
require 'json'
require 'getoptlong'
DEFAULT_CONF_FILE = 'conf.yaml'
opts = GetoptLong.new(
[ '--asn', '-a', GetoptLong::OPTIONAL_ARGUMENT ],
[ '--modify', '-m', GetoptLong::OPTIONAL_ARGUMENT ]
)
config_file = ARGV[0]
if config_file.to_s.empty?
config_file = DEFAULT_CONF_FILE
end
opts.each do |opt, arg|
case opt
when '--asn'
write_asn_database(arg,config_file)
when '--modify'
generate_modify_conf_file(arg,config_file)
end
end
This code generates some json files according to the given YAML configuration file. And what I am trying to do is:
If in the command line is given a conf file, the program will use that given file;
If not, it will use the default file.
Something like:
Example 1 (with a conf file):
$ ./my_script.rb new_conf_file.yaml -a
Example 2 (without conf file):
$ ./my_script.rb -a
The first example works, the second example gives me the following error:
No such file or directory # rb_sysopen - -a (Errno::ENOENT)
And it makes sense because the program assumes that the option -a is a file.
I'm new to Ruby and I never used the command line to run scripts.
OK, I would post it here. Please do not do this, since it’s just ugly:
config_file = ARGV[0]
unless File.exist?(config_file)
config_file = DEFAULT_CONF_FILE
end
From the very first paragraph of GetoptLong documentation:
The empty option -- (two minus symbols) is used to end option processing. This can be particularly important if options have optional arguments.
Use your code by calling a script as:
# ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓
./my_script.rb -a -- new_conf_file.yaml
Also, one should put the opts reading part before dealing with ARGV explicitly.
And no, with GetoptLong it is impossible to parse the command line you provided properly.
I have such a simple code in Ruby (test.rb):
#! /usr/bin/env ruby
require 'optparse'
OptionParser.new do |option|
option.on("--sort", "Sort data") do
puts "--sort passed"
end
end.parse!
then I run it: ./test.rb -s and got:
--sort passed
Have I missed something?
I want the only --sort (long) option works, not the short one.
How do I get it?
I found the code which causes this behavior, in optparse.rb, lines 1378-1380:
# if no short options match, try completion with long
# options.
sw, = complete(:long, opt)
If you don't like that behavior, it seems your best option is to create a copy of optparse.rb within your project, remove the offending rescue InvalidOption clause within the copy, and load that rather than the standard library's version.
It is interesting behaviour that if you define the similar long option that begins with the same letter, in the example is s. It does not allow to use -s key with exception OptionParser::AmbiguousOption, but it seems that there no a way to disable the short option for OptionParser without invading into its code:
#! /usr/bin/env ruby
require 'optparse'
OptionParser.new do |option|
option.on("--sport", "Sport data") do
puts "--sport passed"
end
option.on("--sort", "Sort data") do
puts "--sort passed"
end
end.parse!
This is the expanded version of on method:
OptionParser.new do |option|
opts = [ "--sort", "Sort data" ]
sw = option.make_switch(opts)
block = proc { puts "--sort passed" }
sw[0].instance_variable_set :#block, block
option.top.append *sw
p sw
# => [#<OptionParser::Switch::NoArgument:0x806c770 #pattern=/.*/m, #conv=#<Proc:0x806dd8c#/home/malo/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/optparse.rb:1617>, #short=[], #long=["--sort"], #arg=nil, #desc=["Sort data"], #block=#<Proc:0x806c70c#./1.rb:8>>, [], ["sort"], nil, []]
end.parse!
# => --sort passed when ./1.rb --sort and ./1.rb -s
It is interesting that #short variable is empty but the app reacts on -s key.
I would prefer to use micro-optparse gem. Use it as follows:
Gemfile
gem 'micro-optparse', :git => 'https://github.com/3aHyga/micro-optparse.git', :branch => 'no-short' # for now it is available only from git repo
ruby_script.rb
require 'micro-optparse'
options = Parser.new do |p|
p.banner = "This is a fancy script, for usage see below"
p.option :sport, "sport", :default => "Sport", :short => "p"
p.option :sort, "sort", :default => "Sort", :short => false
end.process!
p options
Simulation:
$ bundle exec ./1.rb --sort 111
{:sport=>"Sport", :sort=>"111"}
$ bundle exec ./1.rb -s 111
ambiguous option: -s
$ bundle exec ./1.rb -p 111
{:sport=>"111", :sort=>"Sort"}
You can reopen OptionParser::OptionMap to disable completions with:
class OptionParser::OptionMap
def complete(key, icase = false, pat = nil)
# disable completions
nil
end
end
This will disable the predefined behavior of searching for stuff to complete.
My program has a parameter '--sort' which may accept arguments like '-s', 's' , '+s', etc
In that case you can pass an array of valid arguments to your option:
require 'optparse'
OptionParser.new do |option|
option.on("--sort TYPE", %w(-s s +s), "Sort data") do |type|
puts "--sort passed with argument #{type}"
end
end.parse!
Usage:
$ ./test.rb --sort -s
--sort passed with argument -s
$ ./test.rb --sort s
--sort passed with argument s
$ ./test.rb --sort +s
--sort passed with argument +s
Note that you can still use the shorthand -s:
$ ./test.rb -s -s
--sort passed with argument -s
$ ./test.rb -s s
--sort passed with argument s
$ ./test.rb -s +s
--sort passed with argument +s
From the documentation, it appears that this isn't possible.
The #on method uses the syntax of #make_switch, which is described here. The whole documentation makes no mention of being able to turn long or short variables on or off.
However, is this really a problem? The convention is that options are accessible via long and short names, and forcing a change in that behaviour might frustrate your users.
If you really don't want to allow short names, the best option would be to look at some other libraries (e.g. highline, slop, trollop) or roll your own.
In puppet, if define command is > 80 characters, how can I wrap into two line to do it?
exec { 'create_domain':
command => "some command exceed 80 character...........................................................how to do how to do?.......",
}
It's sort of ugly, but if the last character in a string is a \ followed by a newline, then the string is continued on the next line. My sample.pp manifest is below:
exec { 'wrapped_string_example':
command => "/bin/echo 12345678901234567890123456789012345678901234567890\
wrapped > /var/tmp/test.txt";
}
Running this with puppet apply sample.pp gives the following output
$ puppet apply sample.pp
notice: /Stage[main]/Exec[wrapped_string_example]/returns: executed successfully
notice: Finished catalog run in 0.10 seconds
And catting the created file shows the lines have wrapped:
$ cat /var/tmp/test.txt
12345678901234567890123456789012345678901234567890wrapped
See https://github.com/puppetlabs/puppet/blob/9fbb36de/lib/puppet/parser/lexer.rb#L537 (as of Puppet v2.7.0)
Also this is sort of a known issue: http://projects.puppetlabs.com/issues/5022
For big chunks of data, heredocs are the best way of dealing with long lines in Puppet manifests. The /L interpolation option is particularly useful. /L causes \ at the end of a line to remove newlines. For example, the following does what you'd expect, stripping indentation and newlines, including the trailing newline.
sshkey { 'example.com':
ensure => present,
type => 'ssh-rsa',
key => #(KEY/L),
RfrXBrU1T6qMNllnhXsJdaud9yBgWWm6OprdEQ3rpkTvCc9kJKH0k8MNfKxeBiGZVsUn435q\
e83opnamtGBz17gUOrzjfmpRuBaDDGmGGTPcO8Dohwz1zYuir93bJmxkNldjogbjAWPfrX10\
8aoDw26K12sK61lOt6GTdR9yjDPdG4zL5G3ZjXCuDyQ6mzcNHdAPPFRQdlRRyCtG2sQWpWan\
3AlYe6h6bG48thlo6vyNvOD8s9K0YBnwl596DJiNCY6EsxnSAhA3Uf9jeKqlVqqrxhEzHufx\
07iP1nXIXCMUV
|-KEY
target => '/home/user/.ssh/authorized_keys',
}
Or to keep the final newline, leave out the - before the end text:
exec { 'create_domain':
command => #(CMD/L),
/bin/echo 123456789012345678901234567890123456789012345678901234567890123456\
wrapped > /var/tmp/test.txt
| CMD
}
As of Puppet 3.5 you have a couple of options that i have used. Ruby allows you to concat strings over a couple of lines.
string = "line #1"\
"line #2"\
"line #3"
p string # => "line #1line #2line #3"
Another option, as of Puppet 3.5 they have added HereDoc functionality. This will allow you to put the string in a section of a source code file that is treated as if it were a separate file.
$mytext = #(EOT)
This block of text is
visibly separated from
everything around it.
| EOT
The puppet documentation is here: https://docs.puppet.com/puppet/4.9/lang_data_string.html#heredocs
If you really care about the 80cols limit you can always abuse a template to achieve that goal
exec {'VeryLongExec':
command => template("${module}/verylongexec")
}
Then put the actual command in that template file
Credits should go to Jan Vansteenkiste to figure
I have a Ruby script that defines a class. I would like the script to execute the statement
BoolParser.generate :file_base=>'bool_parser'
only when the script is invoked as an executable, not when it is require'd from irb (or passed on the command line via -r). What can I wrap around the statement above to prevent it from executing whenever my Ruby file is loaded?
The condition $0 == __FILE__ ...
!/usr/bin/ruby1.8
class BoolParser
def self.generate(args)
p ['BoolParser.generate', args]
end
end
if $0 == __FILE__
BoolParser.generate(:file_base=>__FILE__)
end
... is true when the script is run from the command line...
$ /tmp/foo.rb
["BoolParser.generate", {:file_base=>"/tmp/foo.rb"}]
... but false when the file is required or loaded by another ruby script.
$ irb1.8
irb(main):001:0> require '/tmp/foo'
=> true
irb(main):002:0>
use $0
in irb the value of $0 is "irb"
in your file is "/path/to/file"
an explanation here