Skip to content

Commit

Permalink
Add CLI option for providing configfile. (#824)
Browse files Browse the repository at this point in the history
  • Loading branch information
shiqizng authored Jan 25, 2022
1 parent 014875a commit f8daa21
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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

0 comments on commit f8daa21

Please sign in to comment.