Cannot read properties from custom config - grails

I am using Grails 2.2.1. I want to put some configuration into another file besides Config.groovy.
I put it in a file called My-config.groovy. It's contents are short and simple
uten {
currency="USD"
user="smacko"
}
I add the following to the top of my Config.groovy
grails.config.locations = [ "classpath:My-config.groovy"]
In my code when I do:
String user = grailsApplication.config.uten["user"]
At runtime, I check in the debugger and the user variable is just a String with the value groovy.util.ConfigObject. What are my doing wrong?

If you want uten to be a map your the config should look like this:
uten = [
currency: "USD",
user: "smacko"
]

Personally I am skeptical about using a file name with -. Your approach is correct in using the config file. If there is no resistance in changing the file name to MyConfig.groovy then this option will work effortlessly:
grails.config.locations = [MyConfig]

Related

how to add dynamicity in a variable value in jenkins groovy

i have a json file like the below one..
{
"env1":{
"region":{
"region1":{
"var1":"test"
},
"region2":{
"var1":"test"
},
"region3":{
"var1":"test"
}
}
},
"env2":{
"region":{
"region1":{
"var1":"test"
},
"region2":{
"var1":"test"
},
"region3":{
"var1":"test"
}
}
}
}
config_all = readJSON file: "${env.WORKSPACE}/<above-json-name>.json"
my concern is how do i dynamically get the config for different region at runtime..
i want to do something like this based on a variable..
def region = env.getProperty("region")
config = config_all.env2.${region} << something like this.. but i cannot aceive it..
is there a way to aceive this sort of dynamicity in the varilable value assignment in jenkins groovy.. thanks in advance
If you want to access values in a map with dynamic/variable keys, then you cannot use the object syntax, but rather must use the native syntax for accessing values from keys in a Map with [<key>].
Following this, we update your code snippet like:
config = config_all['env2'][region]
However, based on your JSON, that is probably not going to traverse your data correctly since it is missing the top-level region key and the var1 key. In that case, it would more likely need to be:
config = config_all['env2']['region'][region]['var1']
That will assign the value test to your variable config.

How to import an image in Fable?

I already have all the webpack loaders, so all I need to do is this, but in F#:
import loading from "../images/loading.gif";
This should be an easy thing, I am just lost in the docs and I mostly find info about [<Import>] attribute which seems to be for something else...
In the module Fable.Core.JsInterop we do provide helpers for dealing with import.
Source
I think the one you are looking for is:
/// Works like `ImportAttribute` (same semantics as ES6 imports).
/// You can use "*" or "default" selectors.
let import<'T> (selector: string) (path: string):'T = jsNative
And you can use it like that:
open Fable.Core.JsInterop
let loading : string = import "*" "../images/loading.gif"
I set the type to string because WebPack seems to give you the file URL.
Is there a specific reason you need to do the import for the image?
You can easily display the images in your view (eg. in elmish) without having to import the images, as long as your url's are valid.
let view model dispatch =
div [] [
img [ Class "mr-3 mt-1"
Style [ Width 33% ; Height "80px" ]
Src "../images/loading.gif"
Alt cat.Text; Placeholder "image" ]
]

Accessing Ruby Hash value using a string

I have a ruby array like below
tomcats = [
'sandbox',
'sandbox_acserver',
'sandbox_vgw'
]
I need to pass the string as a hash index like below
tomcats.each do |tomcat_name|
obi_tomcat '#{tomcat_name}' do
Chef::Log::info("Creating tomcat instance - #{tomcat_name}")
Chef::Log::info("#{node['obi']['tomcat']['sandbox'][:name]}") // works
Chef::Log::info("#{node['obi']['tomcat']['#{tomcat_name}'][:name]}") // doesn't work
end
end
The last log throws an error since the access with #{tomcat_name} is nil. I'm new to ruby. How do I access with key as the tomcat_name ?
In normal code, you'd write:
node['obi']['tomcat'][tomcat_name][:name]
In a string interpolation (useless here, because it's the only thing in the string in this case), it is completely the same:
"#{node['obi']['tomcat'][tomcat_name][:name]}"
#{} only works in double quote, as "#{tomcat_name}".
But you don't need the syntax here, just use [tomcat_name] directly.
When I saw this question, I'm thinking whether ruby placeholder could be put inside other placeholder in string interpolation. And I found that ruby actually support it, and most interesting thing is that you don't need to escape the " inside the string.
Although it is not very useful in this case, it still works if you write as below:
Chef::Log::info("#{node['obi']['tomcat']["#{tomcat_name}"][:name]}")
Below is an simple example of placeholder inside other placeholder:
tomcats = [
'sandbox',
'sandbox_acserver',
'sandbox_vgw'
]
node = {
'sandbox_name' => "sandbox name",
'sandbox_acserver_name' => "sandbox_acserver name",
'sandbox_vgw_name' => "sandbox_vgw name",
}
tomcats.each do | tomcat |
puts "This is tomcat : #{node["#{tomcat}_name"]}"
end

How to add custom facters in Ruby

I have file text structured as follows
key1,value1
I want to add these key values as facters in my host. I have splitted those key (configs[0]) , values (configs[1]). How to add those to facters using ruby. I have tried following code but not succeeded
configs = File.read("........configuration.txt").split(",").map(&:strip)
Facter.add(configs[0]){
setcode { configs[1])
}
Regards,
Malintha

Grails URLMapping deep path

I have a URL like:
/test/abs/rdc/tx.js
and I need to be able to get the /abs/rdc/tx.js part for my controller.
I tried (in URLMapping):
/test/$target**
and it returns everything but the .js
I have
grails.mime.file.extensions = true
Any ideas?
UrlMapping and file name extension didn't work as it always returns .html
Grails 2.0 has made some changes to the way the format is parsed. Using the example from the link you provided, just update the code to use response.format:
def path = params.path
if (!FilenameUtils.getExtension(path) && response.format) {
path += ".${response.format}"
}
More info is in the manual under Content Negotiation. Scroll down to the section titled Request format vs. Response format.
You can try this one:
//Grails 2.3.x
"/$controller/$action?/$id?(.${format})?"{
constraints {
// apply constraints here
}
}

Resources