Skip to content

Commit

Permalink
feat: search config recursively (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabnock01 authored Nov 5, 2023
1 parent cd8bd94 commit 01466a1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
25 changes: 24 additions & 1 deletion cmd/flags.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
package main

import (
"os"
"path/filepath"

"github.com/urfave/cli/v2"
)

func findConfigFile() string {
currentDir, _ := os.Getwd()
return findConfigRecursively(currentDir, "mocktimism.toml")
}

func findConfigRecursively(dir, fileName string) string {
configPath := filepath.Join(dir, fileName)
if _, err := os.Stat(configPath); err == nil {
return configPath
}

parentDir := filepath.Dir(dir)
if parentDir == dir {
// We've reached the root directory, and the file is not found.
return ""
}

return findConfigRecursively(parentDir, fileName)
}

var (
ConfigFlag = &cli.StringFlag{
Name: "config",
Value: "./mocktimism.toml",
Value: findConfigFile(),
Aliases: []string{"c"},
Usage: "path to config file",
EnvVars: []string{"MOCKTIMISM_CONFIG"},
Expand Down
2 changes: 1 addition & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Configuration

Mocktimism can be configured via cli flags or a `mocktimism.toml` file.
Mocktimism can be configured via cli flags or a `mocktimism.toml` file. By default, `mocktimism.toml` is expected to exist in the root of the package. Otherwise, the program will recursively look for the file.

## Table of Contents
- [Global Configuration](#global-configuration)
Expand Down

0 comments on commit 01466a1

Please sign in to comment.