Skip to content
This repository was archived by the owner on May 1, 2025. It is now read-only.

otakakot/errifmt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

iferrfmt

iferrfmt is a command-line tool written in Go that reformats error handling code in Go source files.

Overview

The tool automates the refactoring of error handling patterns. It scans Go source files or directories, identifies assignments involving error variables followed by an if statement that checks if the error is non-nil, and modifies the code to use a standardized variable name (err). This helps maintain consistent error handling throughout the codebase.

Features

  • Directory Traversal: Recursively walks through a directory, processing all .go files while skipping the vendor directory.
  • File Processing: If a file path is provided and it ends with .go, that file is processed directly.
  • AST Manipulation: Utilizes Go’s AST (Abstract Syntax Tree) to:
    • Detect error assignments with a single error-type variable.
    • Replace specific error variable names with err.
    • Update subsequent if statements that check the error value.
  • Formatting: Rewrites the file with the updated code formatting if any changes are made.

Usage

Build the tool:

go build main.go

Run the tool by providing either a file or a directory:

./iferrfmt <path-or-file>
  • When a directory is specified, all Go files in that directory (and its subdirectories, excluding vendor) will be processed.
  • When a file is specified, only that file will be processed.

Example

Suppose you have a Go source file with non-standard error handling:

_, err := someFunc()
// some other code
if err != nil {
    // handle error
}

After running iferrfmt, the code will be reformatted to:

if _, err := someFunc(); err != nil {
    // handle error
}

Error Messages

  • If no path or file is provided, the tool prints a usage message:

    Usage: iferrfmt <path-or-file>
    
  • Standard error reporting is done to stderr if file I/O or parsing issues occur.

License

Refer to the LICENSE file for license details.

About

No description or website provided.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages