简体   繁体   中英

Ruby Gem: Install configuration files to the user-home directory

I have to make a REST-Client in Ruby.

The client must be runnable from the command-line like a binary and also it must be "requirable" in a ruby script and provide different functions.

My gemspec does exactly what it should.

But i have no idea how to install a configuration file (YAML) in the user-home folder? The config file should be in the user directory to provide easy access for the user.

  • Is this even possible?
  • Should i check on the first run if there is a config file and create it?
  • Can i execute an own installation routine while installing a gem?

I did exactly the same thing in Python and it worked fine, so the Ruby client should behave similar.

For such decisions, I wrote gem persey . If you look at the description of the use of this gem, you can see that it provides what you expect:

# Rails.root are not initialized here
app_path = File.expand_path('../../', __FILE__)

# ...

# config with secret keys
# you don't want store this config in repository and copy to secret folder on host machine
my_secret_key_config = '/home/user/secret/keys.yml'

# ...

# Persey.init ENV["environment"] do # set current environment
Persey.init Rails.env do # set current environment

  source :yaml, my_secret_key_config, :secret   # no comments. It's secret!

  env :production do
    # ...
  end

  env :development, :parent => :production do
    # ...
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM