mageutils

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/mageutils

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


Table of contents


Functions

Compile(string, string, string)
Compile(string, string, string) error

Compile builds a Go application for a specified operating system and architecture. It sets the appropriate environment variables and runs go build. The compiled application is placed in the specified build path.

Parameters:

buildPath: The output directory for the compiled application. goOS: The target operating system (e.g., "linux", "darwin", "windows"). goArch: The target architecture (e.g., "amd64", "arm64").

Returns:

error: An error if the compilation process encounters one.


FindExportedFuncsWithoutTests(string)
FindExportedFuncsWithoutTests(string) []string, error

FindExportedFuncsWithoutTests discovers all exported functions in a given package path that lack corresponding tests.

Parameters:

pkgPath: A string defining the package path to search.

Returns:

[]string: A slice of strings containing the names of exported functions that lack corresponding tests.

error: An error if there was a problem parsing the package or finding the tests.


FindExportedFunctionsInPackage(string)
FindExportedFunctionsInPackage(string) []FuncInfo, error

FindExportedFunctionsInPackage finds all exported functions in a given Go package by parsing all non-test Go files in the package directory. It returns a slice of FuncInfo structs. Each contains the file path and the name of an exported function. If no exported functions are found in the package, an error is returned.

Parameters:

pkgPath: A string representing the path to the directory containing the package to search for exported functions.

Returns:

[]FuncInfo: A slice of FuncInfo structs, each containing the file path and the name of an exported function found in the package. error: An error if no exported functions are found.


GHRelease(string)
GHRelease(string) error

GHRelease creates a new release on GitHub using the given new version. It requires the gh CLI tool to be available on the PATH.

Parameters:

newVer: A string specifying the new version, e.g., "v1.0.1"

Returns:

error: An error if the GHRelease function is not successful.


GoReleaser()
GoReleaser() error

GoReleaser runs the Goreleaser tool to generate all the supported binaries specified in a .goreleaser configuration file.

Returns:

error: An error if the Goreleaser function is not successful.


InstallGoDeps([]string)
InstallGoDeps([]string) error

InstallGoDeps installs the specified Go dependencies by executing 'go install' for each dependency.

Parameters:

deps: A slice of strings defining the dependencies to install.

Returns:

error: An error if the InstallGoDeps function didn't run successfully.


InstallVSCodeModules()
InstallVSCodeModules() error

InstallVSCodeModules installs the modules used by the vscode-go extension in Visual Studio Code.

Returns:

error: An error if the InstallVSCodeModules function is not successful.


ModUpdate(bool, bool)
ModUpdate(bool, bool) error

ModUpdate updates go modules by running 'go get -u' or 'go get -u ./...' if recursive is set to true. The function will run in verbose mode if 'v' is set to true.

Parameters:

recursive: A boolean specifying whether to run the update recursively. v: A boolean specifying whether to run the update in verbose mode.

Returns:

error: An error if the ModUpdate function is not successful.


Tidy()
Tidy() error

Tidy executes 'go mod tidy' to clear the module dependencies.

Returns:

error: An error if the Tidy function didn't run successfully.


UpdateMageDeps(string)
UpdateMageDeps(string) error

UpdateMageDeps modifies the dependencies in a given Magefile directory. If no directory is provided, it falls back to the 'magefiles' directory.

Parameters:

magedir: A string defining the path to the magefiles directory.

Returns:

error: An error if the UpdateMageDeps function didn't run successfully.


Installation

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

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

Usage

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

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

Tests

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

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 Compile added in v2.0.2

func Compile(buildPath string, goOS string, goArch string) error

Compile builds a Go application for a specified operating system and architecture. It sets the appropriate environment variables and runs `go build`. The compiled application is placed in the specified build path.

**Parameters:**

buildPath: The output directory for the compiled application. goOS: The target operating system (e.g., "linux", "darwin", "windows"). goArch: The target architecture (e.g., "amd64", "arm64").

**Returns:**

error: An error if the compilation process encounters one.

Example
package main

import (
	"fmt"
	"log"

	mageutils "github.com/l50/goutils/v2/dev/mage"
)

func main() {
	buildPath := "/path/to/output/directory"
	goOS := "linux"
	goArch := "amd64"

	if err := mageutils.Compile(buildPath, goOS, goArch); err != nil {
		log.Fatalf("failed to compile: %v", err)
	}

	fmt.Printf("application compiled successfully at: %s\n", buildPath)
}
Output:

func FindExportedFuncsWithoutTests

func FindExportedFuncsWithoutTests(pkgPath string) ([]string, error)

FindExportedFuncsWithoutTests discovers all exported functions in a given package path that lack corresponding tests.

**Parameters:**

pkgPath: A string defining the package path to search.

**Returns:**

[]string: A slice of strings containing the names of exported functions that lack corresponding tests.

error: An error if there was a problem parsing the package or finding the tests.

Example
package main

import (
	"fmt"
	"log"

	mageutils "github.com/l50/goutils/v2/dev/mage"
)

func main() {
	funcs, err := mageutils.FindExportedFuncsWithoutTests("github.com/myorg/mypackage")
	if err != nil {
		log.Fatalf("failed to find exported functions without tests: %v", err)
	}

	for _, funcName := range funcs {
		fmt.Println(funcName)
	}
}
Output:

func GHRelease

func GHRelease(newVer string) error

GHRelease creates a new release on GitHub using the given new version. It requires the gh CLI tool to be available on the PATH.

**Parameters:**

newVer: A string specifying the new version, e.g., "v1.0.1"

**Returns:**

error: An error if the GHRelease function is not successful.

Example
package main

import (
	"log"

	mageutils "github.com/l50/goutils/v2/dev/mage"
)

func main() {
	newVer := "v1.0.1"
	if err := mageutils.GHRelease(newVer); err != nil {
		log.Fatalf("failed to create new GH release: %v", err)
	}
}
Output:

func GoReleaser

func GoReleaser() error

GoReleaser runs the Goreleaser tool to generate all the supported binaries specified in a .goreleaser configuration file.

**Returns:**

error: An error if the Goreleaser function is not successful.

Example

Example GoReleaser

package main

import (
	"log"

	mageutils "github.com/l50/goutils/v2/dev/mage"
)

func main() {
	if err := mageutils.GoReleaser(); err != nil {
		log.Fatalf("failed to run GoReleaser: %v", err)
	}
}
Output:

func InstallGoDeps

func InstallGoDeps(deps []string) error

InstallGoDeps installs the specified Go dependencies by executing 'go install' for each dependency.

**Parameters:**

deps: A slice of strings defining the dependencies to install.

**Returns:**

error: An error if the InstallGoDeps function didn't run successfully.

Example
package main

import (
	"log"

	mageutils "github.com/l50/goutils/v2/dev/mage"
)

func main() {
	deps := []string{"github.com/stretchr/testify", "github.com/go-chi/chi"}

	if err := mageutils.InstallGoDeps(deps); err != nil {
		log.Fatalf("failed to install Go dependencies: %v", err)
	}
}
Output:

func InstallVSCodeModules

func InstallVSCodeModules() error

InstallVSCodeModules installs the modules used by the vscode-go extension in Visual Studio Code.

**Returns:**

error: An error if the InstallVSCodeModules function is not successful.

Example
package main

import (
	"log"

	mageutils "github.com/l50/goutils/v2/dev/mage"
)

func main() {
	if err := mageutils.InstallVSCodeModules(); err != nil {
		log.Fatalf("failed to install VS Code modules: %v", err)
	}
}
Output:

func ModUpdate

func ModUpdate(recursive bool, v bool) error

ModUpdate updates go modules by running 'go get -u' or 'go get -u ./...' if recursive is set to true. The function will run in verbose mode if 'v' is set to true.

**Parameters:**

recursive: A boolean specifying whether to run the update recursively. v: A boolean specifying whether to run the update in verbose mode.

**Returns:**

error: An error if the ModUpdate function is not successful.

Example
package main

import (
	"log"

	mageutils "github.com/l50/goutils/v2/dev/mage"
)

func main() {
	recursive := true
	verbose := true

	if err := mageutils.ModUpdate(recursive, verbose); err != nil {
		log.Fatalf("failed to update modules: %v", err)
	}
}
Output:

func Tidy

func Tidy() error

Tidy executes 'go mod tidy' to clear the module dependencies.

**Returns:**

error: An error if the Tidy function didn't run successfully.

Example
package main

import (
	"log"

	mageutils "github.com/l50/goutils/v2/dev/mage"
)

func main() {
	if err := mageutils.Tidy(); err != nil {
		log.Fatalf("failed to tidy modules: %v", err)
	}
}
Output:

func UpdateMageDeps

func UpdateMageDeps(magedir string) error

UpdateMageDeps modifies the dependencies in a given Magefile directory. If no directory is provided, it falls back to the 'magefiles' directory.

**Parameters:**

magedir: A string defining the path to the magefiles directory.

**Returns:**

error: An error if the UpdateMageDeps function didn't run successfully.

Example
package main

import (
	"log"

	mageutils "github.com/l50/goutils/v2/dev/mage"
)

func main() {
	magedir := "custom/mage/dir"

	if err := mageutils.UpdateMageDeps(magedir); err != nil {
		log.Fatalf("failed to update Mage dependencies: %v", err)
	}
}
Output:

Types

type FuncInfo

type FuncInfo struct {
	FilePath string // FilePath is the file path of the source file containing the function declaration.
	FuncName string // FuncName is the name of the exported function.
}

FuncInfo represents information about an exported function within a Go package.

**Attributes:**

FilePath: A string representing the path to the source file containing the function declaration. FuncName: A string representing the name of the exported function.

func FindExportedFunctionsInPackage

func FindExportedFunctionsInPackage(pkgPath string) ([]FuncInfo, error)

FindExportedFunctionsInPackage finds all exported functions in a given Go package by parsing all non-test Go files in the package directory. It returns a slice of FuncInfo structs. Each contains the file path and the name of an exported function. If no exported functions are found in the package, an error is returned.

**Parameters:**

pkgPath: A string representing the path to the directory containing the package to search for exported functions.

**Returns:**

[]FuncInfo: A slice of FuncInfo structs, each containing the file path and the name of an exported function found in the package. error: An error if no exported functions are found.

Example
package main

import (
	"log"

	mageutils "github.com/l50/goutils/v2/dev/mage"
)

func main() {
	packagePath := "/path/to/your/go/package"

	funcs, err := mageutils.FindExportedFunctionsInPackage(packagePath)
	if err != nil {
		log.Fatalf("failed to find exported functions: %v", err)
	}

	for _, f := range funcs {
		log.Printf("Exported function %s found in file %s\n", f.FuncName, f.FilePath)
	}
}
Output:

Jump to

Keyboard shortcuts

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