Config file type usage - go

Usually I saw config file in many examples out there are in .env or .json file.
What if I decide to use .go file instead , is it uncommon, how it should be done?
I was thinking since .env file is static, if I want to put config like this
var currentDate = time.Now()
var currentDateFormat = currentDate.Format("2006-01-02")
var logPath = dir + "/log/" + currentDateFormat + ".log"
It can't be done in .env file so should I just keep the above config within function somewhere and stick with .env file?

What if I decide to use .go file instead
Then it is no longer a config file (static content), but a source file, which needs to be compiled and part of your exe (runtime content).
It then could be part of an init() function for instance.
Or part of a config package source, in charge of loading your config as well as initializing the variables in your question.

Related

Viper AddConfigPath only finding file in current folder "."

Have code that looks like
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/.config/myprogram")
viper.AddConfigPath("$HOME/configs")
viper.SetConfigFile("myprogram.yaml")
If I place myprogram.yaml in the current folder it works. However if I try putting it on either
$HOME/.config/myprogram$HOME/configs
The yaml file is not found. Any ideas or suggestions?
From the viper docs:
SetConfigFile explicitly defines the path, name and extension of the
config file. Viper will use this and not check any of the config
paths.
So if you use SetConfigFile the paths will be ignored. Try (as per the example):
viper.SetConfigName("myprogram")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/configs")
viper.AddConfigPath("$HOME/.config/myprogram")

Windows Script Host Javascript Get Path of .js file

I have been using QTTabBar for a while and am using .js scripts with it. The scripts are run using Windows Script Host, but I find myself having to specify hardcoded directories in the .js file instead of relative paths. This is not ideal.
In the .js file, is it possible to get the containing folder of the .js file (no matter what directory it is originally run from)? I just need to avoid specifying absolute paths somehow. For example, part of my .js file might look like this:
var qs = new ActiveXObject( "QTTabBarLib.Scripting" );
var fso = new ActiveXObject("Scripting.FileSystemObject");
var txtFile = fso.OpenTextFile("C:\\Installation\\Scripts\\QTTabBar\\dirs.txt", 1, false, 0);
var fText = txtFile.ReadAll();
I can't just put "dirs.txt" in the OpenTextFile function because when the .js script is run in QTTabBar, the working directory (I think) starts in system32 rather than at the .js file location. So I somehow need to get the path of the .js file itself and combine it with the relative name to create the absolute path. But I'm not sure if this is possible or how to do it.
You can get the path of current JScript file with
js_file_path = WScript.ScriptFullName;
and the absolute path to the text file is
path = WScript.ScriptFullName.split("\\").slice(0, -1).join("\\") + "\\dirs.txt";
No includes or imports are needed if the script is run in Windows Script Host.

Use multiple env files

I'm wondering if there's a way in Laravel to specify a set of env files to load. My exact problem is I want to add something like a suffix to all my .js and .css resources. Ideally I'd have a suffix like the release date because it would be ok for these files to be cached within the same release but I would like the caches to be invalidated on the next release. However I want to avoid reading, modifying and saving the .env file if possible and would instead prefer to create a new file e.g. .env.rdate which would be generated via a script, e.g.
echo APP_RELEASE_DATE=`date +%s` > env.rdate
or something like this. Is this at all possible or do I have to read/update/write the .env file instead?
Create your .env.rdate file next to .env file.
Put this to your AppServiceProvider boot method:
$dotenv = new \Dotenv\Dotenv(base_path(),'.env.rdate');
$dotenv->overload();
After you can use in your project:
ENV('APP_RELEASE_DATE')

File path in golang

I have a project with next structure:
|_main.go
|_config
|_config.go
|_config_test.go
|_config.json
I'm having next code line in config.go:
file, _ := os.Open("config/config.json")
When I'm executing method contained this code line from main.go all is working. But when I'm trying to execute this method from config_test.go it produces error:
open config/config.json: no such file or directory
As I understood it is a working directory issue because I'm launching same code with relative path from different directories. How can I fix this problem without using full path in config.go?
Relative paths are always resolved basis your current directory. Hence it's better to avoid relative paths.
Use command line flags or a configuration management tool (better approach) like Viper
Also according to The Twelve-Factor App your config files should be outside your project.
Eg usage with Viper:
import "github.com/spf13/viper"
func init() {
viper.SetConfigName("config")
// Config files are stored here; multiple locations can be added
viper.AddConfigPath("$HOME/configs")
errViper := viper.ReadInConfig()
if errViper != nil {
panic(errViper)
}
// Get values from config.json
val := viper.GetString("some_key")
// Use the value
}

Where do the files created with File.new actually get stored in Ruby?

I am creating files from within Ruby scripts and adding stuff to them. But where are these files stored that I am creating?
I'm very new to this, sorry!
The files are created at whatever location you specified. For instance:
f = File.new("another_test.txt","w+")
that will create the file in the current working directory. You specify the path along with the file name. For example:
f = File.new("~/Desktop/another_test.txt","w+") # will create the file on the desktop.
For more details, check the File documentation.
Updated:
Included mu is too short correction.

Resources