gomodrun

package module
v0.4.5 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2022 License: MIT Imports: 16 Imported by: 0

README

gomodrun

Build Status Release Coverage Status Go Report Card Godocs

The forgotten go tool that executes and caches binaries included in go.mod files. This makes it easy to version cli tools in your projects such as golangci-lint and ginkgo that are versioned locked to what you specify in go.mod. Binaries are cached by go version and package version.

Example

  # Run a linter
  gomodrun golangci-lint run

  # Convert a JSON object to a Go struct, properly passing in stdin.
  echo example.json | gomodrun gojson > example.go

  # Specifiy alternative root directory containing a go.mod and tools file.
  gomodrun -r ./alternative-tools-dir golangci-lint run

  # Clean your .gomodrun folder of unused binaries.
  gomodrun --tidy

Installation

homebrew (OSX / Linux):

brew install dustinblackman/tap/gomodrun

scoop (Windows):

$ scoop bucket add dustinblackman https://github.com/dustinblackman/scoop-bucket.git
$ scoop install gomodrun

deb/rpm/apk (Linux):

Download the .deb, .rpm or .apk from the releases page and install with dpkg -i, rpm -i, or apk add --allow-untrusted respectively.

nix (Linux):

nix-env -f '<nixpkgs>' -iA nur.repos.dustinblackman.gomodrun

manually:

Download the pre-compiled binaries from the releases page and copy to the desired location.

go/master branch:

go get -u github.com/dustinblackman/gomodrun/cmd/gomodrun

Usage

gomodrun works by using a tools.go (or any other name) file that sits in the root of your project that contains all the CLI dependencies you want bundled in to your go.mod. Note the // +build tools at the top of the file is required, and allows you to name your tools file anything you like.

tools.go

// +build tools

package myapp

import (
	_ "github.com/golangci/golangci-lint/cmd/golangci-lint"
	_ "github.com/onsi/ginkgo/ginkgo"
)

Run go build tools.go to add the dependencies to your go.mod. The build is expected to fail.

CLI

You can run your tools by prefixing gomodrun. A binary will be built and cached in .gomodrun in the root of your project, allowing all runs after the first to be nice and fast.

  gomodrun golangci-lint run

Programmatically

You can also use gomodrun as a library.

package main

import (
	"os"

	"github.com/dustinblackman/gomodrun"
)

func main() {
	exitCode, err := gomodrun.Run("golangci-lint", []string{"run"}, &gomodrun.Options{
		Stdin:   os.Stdin,
		Stdout:  os.Stdout,
		Stderr:  os.Stderr,
		Env:     os.Environ(),
		PkgRoot: "",
	})
}

License

MIT

Documentation

Overview

Package gomodrun is the forgotten go tool that executes and caches binaries included in go.mod files. This makes it easy to version cli tools in your projects such as `golangci-lint` and `ginkgo` that are versioned locked to what you specify in `go.mod`. Binaries are cached by go version and package version.

Package gomodrun is the forgotten go tool that executes and caches binaries included in go.mod files. This makes it easy to version cli tools in your projects such as `golangci-lint` and `ginkgo` that are versioned locked to what you specify in `go.mod`. Binaries are cached by go version and package version.

Package gomodrun is the forgotten go tool that executes and caches binaries included in go.mod files. This makes it easy to version cli tools in your projects such as `golangci-lint` and `ginkgo` that are versioned locked to what you specify in `go.mod`. Binaries are cached by go version and package version.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCachedBin

func GetCachedBin(pkgRoot, binName, cmdPath string) (string, error)

GetCachedBin returns the path to the cached binary, building it if it doesn't exist.

func GetCommandVersionedPkgPath

func GetCommandVersionedPkgPath(pkgRoot, binName string) (string, error)

GetCommandVersionedPkgPath extracts the command line tools package path and version from go.mod.

func GetPkgRoot

func GetPkgRoot() (string, error)

GetPkgRoot gets your projects package root, allowing you to run gomodrun from any sub directory.

func Run

func Run(binName string, args []string, options *Options) (int, error)

Run executes your binary.

func Tidy added in v0.4.0

func Tidy(pkgRoot string) error

Tidy cleans .gomodrun of any outdated binaries.

Types

type Options

type Options struct {
	Stdin   io.Reader // Stdin passed to tool.
	Stdout  io.Writer // Stdout passed to tool.
	Stderr  io.Writer // Stderr passed to tool.
	Env     []string  // Array of environment variables passed to tool.
	PkgRoot string    // Root directory of go.mod with tools.
}

Options contains parameters that are passed to `exec.Command` when running the binary.

Directories

Path Synopsis
cmd
gomodrun
Package main compiles the binary that uses the gomodrun library.
Package main compiles the binary that uses the gomodrun library.

Jump to

Keyboard shortcuts

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