Ruby scripting - how to keep track of configurations - ruby

I am writing a system script in Ruby.
I'm using the classic gem structure: lib, bin, spec for RSpec.
I want to build a configurable script: I want to be able to provide options like --set-stuff and alike. A perfect example is:
git config --global user.name "Andrea"
which writes the given information out to a file, in order to be able to retrieve this information later.
How can I do this in a clean way?
I'd rather not use the environment variable solution: I know I could just set an env variable to point to a configuration file, but then I'd have to save this env variable in, say, .bashrc. Then again, how do I deal with zsh? Or how do I deal with people (like me) who keep their .bashrcs super-neat or even have a separate .env-variables file in their system?

Just stick the configuration into a Hash and serialize it into a file in the user's home directory as YAML or JSON...

Related

How to rename .env variables in package.json?

What I have
I have multiple projects using Percy for Cypress where I set the PERCY_TOKEN env variable inside the .env file. The token is different for each project. In the CI I set different env variables for each project, but locally I have to do it in the .env file. Because of this, I have to edit the .env file whenever I change between projects.
Goal
I would like to set them in the .env file this way:
PROJECT_A_PERCY_TOKEN=tokenhash1
PROJECT_B_PERCY_TOKEN=tokenhash2
So later I could rename these variables to PERCY_TOKEN, eliminating the need to constantly change the .env file.
What I tried
I'm trying to do this inside the package.json file's scripts property. Unfortunately echo $PROJECT_A_PERCY_TOKEN prints nothing. I know that I could create a shell/python/js script that parses the .env file, then passes the value back or calls npm run directly but I would like to do this without an external script.
Problem
It appears to me that I can't access the env variables inside package.json. Is there a way to rename the variable only using the npm script?
tl;dr
If the package you try to configure has the ability to do configuration via a JavaScript file, you can add the renaming at the beginning of it:
process.env.PERCY_TOKEN = process.env.CYPRESS_PERCY_SALESFORCE_TOKEN;
Explanation
While this isn't the solution I was looking for, it is a workaround for this specific use case. Percy supports JavaScript config files so I migrated my YAML config file, then I logged process.env and the .env file's variables were there, so I just need to copy the correct one. This might work for other packages that support JavaScript config files (or some alternative kind of hook/preloader where custom code can be placed), but if they don't, then the question is still unanswered.

What is the standard way to describe the paths to config files of my ruby application (linux)?

What is the standard way to describe the paths to config files of my ruby application (linux)? (not rails, but I think it doesn't matter right now)
How does this "fit" into standard paths like /etc/ and /usr/local/etc/?
Let's say I want to put config in /usr/local/etc/myapp/config.json during global installation,
but in $HOME/.config/myapp/config.json during local installation.
I want to provide the default configuration file with my application, not pass it from somewhere else (although this is also possible, but only as an option).
Can gemspec do this? I found in the description of Gem::Specification only paths to bin and exe files, but not to configuration files.

Is there a way to set non-secret environment variables in Github Actions on the Settings page?

As far as I know, there are two ways to set environment variables in Github Actions:
Hardcoding them into YAML file
Adding them as repository secrets on the settings page
Repository secrets page
But what if I don't want them to be secret? On the picture above, SERVER_PREFIX and ANALYTICS_ENABLED shouldn't be secret. Is there a way to set up env variables on the settings page and make them visible? In Travis we had that option.
There isn't an option to add non-secret ENV variables on GitHub page at now.
You can create workflow-scope ENV variables in workflow step.
env:
SERVER_PREFIX: SOME_PREFIX
Then access by:
${{ env.SERVER_PREFIX }}
If you don't need to use them in the Action's YAML, just define your variables in a downloadable file and then use something like curl or wget to get them into your build environment.
For instance, I've done something similar for common CI files and now I've multiple projects running the same project building scripts, their local action is simply like: download an .sh file, run it.
If you need to set up variables in one of your build steps, to be used later by some other action, have a look at this (but I've never tried it myself).

Set an environment variable in a Sinatra app?

I want to set MONGOHQ_URL in my sinatra app in order to be able to do this:
uri = URI.parse(ENV['MONGOHQ_URL'])
How do I setup the MONGOHQ_URL?
on Windows: set MONGOHQ_URL=test
on Unix (bash): export MONGOHQ_URL=test
on Unix (csh): setenv MONGOHQ_URL test
In order for your environment variables to always be available to your app, you will need to make sure they get exported whenever a new terminal session launches. It's common to put these in .bashrc for example
export MONGOHQ_URL=https://some.long.secure.url # for example
But for your local development purposes you might want to check out dotenv gem which allows you to store local environment variables in .env file in root of your project. For production, you should be able to Figaro with Sinatra, for more see answer to this question or see readme on the github repo
In general you should always make sure not to commit sensitive config information in your codebase so make sure to add any files like .env or config/application.yml to your .gitignore file.

How can I track system-specific config files in a repo/project?

I have a ruby project, and the database host and port might be different on dev and production. I need a way to get different values for those into my scripts for the two environments.
The project should be complete - so there should be some way to specify default values. I don't want a clone to be missing the config files. So ignoring them completely won't work.
How do you solve this problem with git?
I would recommend using:
a template config file (a file with variable name in place of the host and port value)
a script able to replace those variable names with the appropriate values depending on the environment (detected by the script)
The Git solution is then a git attribute filter driver (see also GitPro book).
A filter driver consists of a clean command and a smudge command, either of which can be left unspecified.
Upon checkout, when the smudge command is specified, the command is fed the blob object from its standard input, and its standard output is used to update the worktree file.
Similarly, the clean command is used to convert the contents of worktree file upon check-in.
That way, the script (managed with Git) referenced by the smudge can replace all the variables by environement-specific values, while the clean script will restore its content to an untouched config file.
When you checkout your Git repo on a prod environment, the smudge process will produce a prod-like config file in the resulting working tree.

Resources