Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI option for providing configfile. #824

Merged
merged 4 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ The command line arguments always take priority over the config file and environ
## Configuration file
Default values are placed in the configuration file. They can be overridden with environment variables and command line arguments.

The configuration file must named **indexer**, **indexer.yml**, or **indexer.yaml**. It must also be in the correct location. Only one configuration file is loaded, the path is searched in the following order:
The configuration file must named **indexer**, **indexer.yml**, or **indexer.yaml**. The filepath may be set on the CLI using `--configfile` or `-c`.
When the filepath is not provided on the CLI, it must also be in the correct location. Only one configuration file is loaded, the path is searched in the following order:
* `./` (current working directory)
* `$HOME`
* `$HOME/.algorand-indexer`
Expand All @@ -172,6 +173,11 @@ If it is in the current working directory along with the indexer command we can
~$ ./algorand-indexer daemon
```

If it is not in the current working directory along with the indexer command we can start the indexer daemon with:
```
~$ ./algorand-indexer daemon -c <full-file-location>/indexer.yml
```

## Example environment variable

Environment variables are also available to configure indexer. Environment variables override settings in the config file and are overridden by command line arguments.
Expand Down
14 changes: 14 additions & 0 deletions cmd/algorand-indexer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ var rootCmd = &cobra.Command{
err = pprof.StartCPUProfile(profFile)
maybeFail(err, "%s: start pprof, %v", cpuProfile, err)
}
if configFile != "" {
configs, err := os.Open(configFile)
if err != nil {
maybeFail(err, "%v", err)
}
defer configs.Close()
err = viper.ReadConfig(configs)
if err != nil {
maybeFail(err, "invalid config file (%s): %v", viper.ConfigFileUsed(), err)
}
fmt.Printf("Using configuration file: %s\n", configFile)
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if cpuProfile != "" {
Expand All @@ -83,6 +95,7 @@ var (
logLevel string
logFile string
logger *log.Logger
configFile string
)

func indexerDbFromFlags(opts idb.IndexerDbOptions) (idb.IndexerDb, chan struct{}) {
Expand Down Expand Up @@ -117,6 +130,7 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&dummyIndexerDb, "dummydb", "n", false, "use dummy indexer db")
rootCmd.PersistentFlags().StringVarP(&cpuProfile, "cpuprofile", "", "", "file to record cpu profile to")
rootCmd.PersistentFlags().StringVarP(&pidFilePath, "pidfile", "", "", "file to write daemon's process id to")
rootCmd.PersistentFlags().StringVarP(&configFile, "configfile", "c", "", "file path to configuration file (indexer.yml)")
rootCmd.PersistentFlags().BoolVarP(&doVersion, "version", "v", false, "print version and exit")

viper.RegisterAlias("postgres", "postgres-connection-string")
Expand Down