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

Recursively search goio.yaml #10

Merged
merged 1 commit into from
Jun 2, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}
}