lint

package
v2.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 12 Imported by: 0

README

goutils/v2/lint

The lint package is a collection of utility functions designed to simplify common lint tasks.


Table of contents


Functions

AddFencedCB(string, string)
AddFencedCB(string, string) error

AddFencedCB addresses MD040 issues found with markdownlint by adding the input language to fenced code blocks in the input filePath.

Parameters:

filePath: Path to the markdown file to modify. language: Language to be added to the fenced code block.

Returns:

error: An error if the markdown file fails to be modified.


ClearPCCache()
ClearPCCache() error

ClearPCCache clears the pre-commit cache.

Returns:

error: An error if the cache fails to clear.


InstallGoPCDeps()
InstallGoPCDeps() error

InstallGoPCDeps installs dependencies used for pre-commit with Golang projects.

Returns:

error: An error if the dependencies fail to install.


InstallPCHooks()
InstallPCHooks() error

InstallPCHooks installs pre-commit hooks locally.

Returns:

error: An error if the hooks fail to install.


RunHookTool(string, ...string)
RunHookTool(string, ...string) error

RunHookTool executes the specified pre-commit hook on a set of files. It constructs a command to run 'pre-commit' with the given hook and file arguments. If no files are provided, it defaults to "all". The function then executes the command and handles any resulting error.

Parameters:

hook: A string specifying the name of the pre-commit hook to be run. files: A variadic string slice containing file paths to be included in the pre-commit hook execution. If no files are specified, it defaults to running the hook on all files.

Returns:

error: An error if any issue occurs during the execution of the pre-commit hook, otherwise nil if the hook runs successfully.


RunPCHooks(...int)
RunPCHooks(...int) error

RunPCHooks runs pre-commit hooks with a provided timeout. If no timeout is provided, it defaults to 600.

Parameters:

timeout (optional): An integer specifying the timeout duration in seconds.

Returns:

error: An error if the pre-commit hook execution fails.


UpdatePCHooks()
UpdatePCHooks() error

UpdatePCHooks updates pre-commit hooks locally.

Returns:

error: An error if the hooks fail to update.


Installation

To use the goutils/v2/lint package, you first need to install it. Follow the steps below to install via go get.

go get github.com/l50/goutils/v2/lint

Usage

After installation, you can import the package in your Go project using the following import statement:

import "github.com/l50/goutils/v2/lint"

Tests

To ensure the package is working correctly, run the following command to execute the tests for goutils/v2/lint:

go test -v

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddFencedCB

func AddFencedCB(filePath string, language string) error

AddFencedCB addresses MD040 issues found with markdownlint by adding the input language to fenced code blocks in the input filePath.

**Parameters:**

filePath: Path to the markdown file to modify. language: Language to be added to the fenced code block.

**Returns:**

error: An error if the markdown file fails to be modified.

Example
package main

import (
	"log"

	lint "github.com/l50/goutils/v2/dev/lint"
)

func main() {
	if err := lint.AddFencedCB("README.md", "go"); err != nil {
		log.Fatalf("error modifying markdown file: %v", err)
	}
}
Output:

func ClearPCCache

func ClearPCCache() error

ClearPCCache clears the pre-commit cache.

**Returns:**

error: An error if the cache fails to clear.

Example
package main

import (
	"log"
	"path/filepath"

	lint "github.com/l50/goutils/v2/dev/lint"
	"github.com/l50/goutils/v2/sys"
)

func main() {
	if err := sys.Cd(filepath.Join("..", "..")); err != nil {
		log.Fatalf("failed to change directory: %v", err)
	}
	if err := lint.ClearPCCache(); err != nil {
		log.Fatalf("error clearing cache: %v", err)
	}
}
Output:

func InstallGoPCDeps

func InstallGoPCDeps() error

InstallGoPCDeps installs dependencies used for pre-commit with Golang projects.

**Returns:**

error: An error if the dependencies fail to install.

Example
package main

import (
	"log"
	"path/filepath"

	lint "github.com/l50/goutils/v2/dev/lint"
	"github.com/l50/goutils/v2/sys"
)

func main() {
	if err := sys.Cd(filepath.Join("..", "..")); err != nil {
		log.Fatalf("failed to change directory: %v", err)
	}
	if err := lint.InstallGoPCDeps(); err != nil {
		log.Fatalf("error installing dependencies: %v", err)
	}
}
Output:

func InstallPCHooks

func InstallPCHooks() error

InstallPCHooks installs pre-commit hooks locally.

**Returns:**

error: An error if the hooks fail to install.

func RunHookTool added in v2.2.0

func RunHookTool(hook string, files ...string) error

RunHookTool executes the specified pre-commit hook on a set of files. It constructs a command to run 'pre-commit' with the given hook and file arguments. If no files are provided, it defaults to "all". The function then executes the command and handles any resulting error.

**Parameters:**

hook: A string specifying the name of the pre-commit hook to be run. files: A variadic string slice containing file paths to be included in the pre-commit hook execution. If no files are specified, it defaults to running the hook on all files.

**Returns:**

error: An error if any issue occurs during the execution of the pre-commit hook, otherwise nil if the hook runs successfully.

Example
package main

import (
	"log"

	lint "github.com/l50/goutils/v2/dev/lint"
)

func main() {
	if err := lint.RunHookTool("golangci-lint", "run"); err != nil {
		log.Fatalf("error running hook tool: %v", err)
	}
}
Output:

func RunPCHooks

func RunPCHooks(timeout ...int) error

RunPCHooks runs pre-commit hooks with a provided timeout. If no timeout is provided, it defaults to 600.

**Parameters:**

timeout (optional): An integer specifying the timeout duration in seconds.

**Returns:**

error: An error if the pre-commit hook execution fails.

Example
package main

import (
	"log"
	"path/filepath"

	lint "github.com/l50/goutils/v2/dev/lint"
	"github.com/l50/goutils/v2/sys"
)

func main() {
	if err := sys.Cd(filepath.Join("..", "..")); err != nil {
		log.Fatalf("failed to change directory: %v", err)
	}

	// Run with a default timeout of 600.
	if err := lint.RunPCHooks(); err != nil {
		log.Fatalf("failed to run pre-commit hooks: %v", err)
	}

	// Runs with a specified timeout of 300.
	if err := lint.RunPCHooks(300); err != nil {
		log.Fatalf("failed to run pre-commit hooks: %v", err)
	}

	if err := lint.RunPCHooks(300); err != nil {
		log.Fatalf("failed to run pre-commit hooks: %v", err)
	}
}
Output:

func UpdatePCHooks

func UpdatePCHooks() error

UpdatePCHooks updates pre-commit hooks locally.

**Returns:**

error: An error if the hooks fail to update.

Example
package main

import (
	"log"
	"path/filepath"

	lint "github.com/l50/goutils/v2/dev/lint"
	"github.com/l50/goutils/v2/sys"
)

func main() {
	if err := sys.Cd(filepath.Join("..", "..")); err != nil {
		log.Fatalf("failed to change directory: %v", err)
	}
	if err := lint.UpdatePCHooks(); err != nil {
		log.Fatalf("error updating hooks: %v", err)
	}
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL