Skip to content

Commit

Permalink
Recursively search goio.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
sttts committed Jun 2, 2023
1 parent 79865b7 commit f2c4ba6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ A customizable imports organizer for the Go programming language

# <a name='summary'></a>Summary
`goio` is a fully customizable Go imports organizer. The configuration
is project based and is stored in a `goio.yaml` file in the root of your
is project based and is stored in a `goio.yaml` file, e.g. in the root of your
module's project folder alongside the `go.mod` file. For consistency
the `goio.yaml` file should be committed to your projects vcs.


the `goio.yaml` file should be committed to your projects vcs. If no `goio.yaml`
file is found, `goio` will try to find one walking up the directory tree.

# <a name='command-line-tool'></a>Command Line Tool

Expand Down
29 changes: 28 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,19 @@ func main() {
os.Exit(1)
}

// Find a goio.yaml file in the current directory or any parent directory
path, found, err := findFile(currentDir, "goio.yaml")
if err != nil {
fmt.Fprintf(os.Stderr, "error occurred finding configuration file goio.yaml: %v\n", err)
os.Exit(1)
}
if !found {
fmt.Fprint(os.Stderr, "error occurred finding configuration file goio.yaml\n")
os.Exit(1)
}

// Load the configuration from the goio.yaml file
conf, err := config.Load("goio.yaml")
conf, err := config.Load(path)
if err != nil {
fmt.Fprintf(os.Stderr, "error occurred loading configuration file: %s\n", err.Error())
os.Exit(1)
Expand Down Expand Up @@ -184,3 +195,19 @@ func main() {
}
}
}

func findFile(path, fileName string) (string, bool, error) {
for {
_, err := os.Stat(filepath.Join(path, fileName))
if err == nil {
return filepath.Join(path, fileName), true, nil
}
if !os.IsNotExist(err) {
return "", false, err
}
if path == "/" {
return "", false, nil
}
path = filepath.Dir(path)
}
}

0 comments on commit f2c4ba6

Please sign in to comment.