...

/

Adding a Configuration File to Multi-git

Adding a Configuration File to Multi-git

Let's continue refactoring Multi-git and utilize Viper to read a configuration file. We'll also discuss backward compatibility in general and in the context of multi-git.

Adding a configuration file

The first question to ask is where to put the configuration file. I recommend following the XDG guidelines and using $HOME/.config as the configuration directory. However, to support other use cases you should be able to provide the path to the configuration file as an optional command-line argument.

Let’s set it up first in cmd/root.go. We define a configFilename variable and then bind the "config" command-line flag to it, so you can pass it on the command-line as --config <filename>. If it’s not provided the default configuration file is $HOME/.config/multi-git.toml

var configFilename string


...

func init() {
	...
	// Find home directory.
	home, err := homedir.Dir()
	check(err)

	defaultConfigFilename := path.Join(home, ".config/multi-git.toml")
	rootCmd.Flags().StringVar(&configFilename,
		"config",
		defaultConfigFilename,
		"config file path
...