gobin

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package gobin provides a caster to install and run Go module executables using the "github.com/myitcv/gobin" module command.

See https://pkg.go.dev/github.com/myitcv/gobin for more details about "gobin". The source code of the "gobin" is available at https://github.com/myitcv/gobin.

Go Executable Installation

When installing a Go executable from within a Go module (1) directory using the "go install" command (2), it is installed into the Go executable search path that is defined through the "GOBIN" environment variable (3) and can also be shown and modified using the "go env" command (4). Even though the executable gets installed globally, the "go.mod" file (5) will be updated to include the installed packages since this is the default behavior of the "go get" command (6) when running in "module" mode (7).

Next to this problem, the installed executable will also overwrite any executable of the same module/package that was installed already, but maybe from a different version. Therefore only one version of a executable can be installed at a time which makes it impossible to work on different projects that use the same tool but with different versions.

History and Future

The local installation of executables built from Go modules/"main" packages has always been a somewhat controversial point which unfortunately, partly for historical reasons, does not offer an optimal and user-friendly solution up to now. The "go" command (8) is a fantastic toolchain that provides many great features one would expect to be provided out-of-the-box from a modern and well designed programming language without the requirement to use a third-party solution: from compiling code, running unit/integration/benchmark tests, quality and error analysis, debugging utilities and many more. Unfortunately the way the "go install" command (2) of Go versions less or equal to 1.15 handles the installation is still not optimal.

The general problem of tool dependencies is a long-time known issue/weak point of the current Go toolchain and is a highly rated change request from the Go community with discussions like https://github.com/golang/go/issues/30515, https://github.com/golang/go/issues/25922 and https://github.com/golang/go/issues/27653 to improve this essential feature, but they've been around for quite a long time without a solution that works without introducing breaking changes and most users and the Go team agree on. Luckily, this topic was finally picked up for the next upcoming Go release version 1.16 (9) and https://github.com/golang/go/issues/40276 introduces a way to install executables in module mode outside a module. The release note preview also already includes details about this change (10) and how installation of executables from Go modules will be handled in the future.

The Workaround

Beside the great news and anticipation about an official solution for the problem the usage of a workaround is almost inevitable until Go 1.16 is finally released.

The official Go wiki (11) provides a section on "How can I track tool dependencies for a module?" (12) that describes a workaround that tracks tool dependencies. It allows to use the Go module logic by using a file like "tools.go" with a dedicated "tools" build tag that prevents the included module dependencies to be picked up included for normal executable builds. This approach works fine for non-main packages, but CLI tools that are only implemented in the "main" package can not be imported in such a file.

In order to tackle this problem, a user from the community created "gobin" (13), an experimental, module-aware command to install/run main packages. It allows to install or run main-package commands without "polluting" the "go.mod" file by default. It downloads modules in version-aware mode into a binary cache path within the systems cache directory (14). It prevents problems due to already globally installed executables by placing each version in its own directory. The decision to use a cache directory instead of sub-directories within the "GOBIN" path keeps the system clean.

"gobin" is still in an early development state, but has already received a lot of positive feedback and is used in many projects. There are also members of the core Go team that have contributed to the project and the chance is high that the changes for Go 1.16 were influenced or partially ported from it. It is currently the best workaround to...

  1. prevent the Go toolchain to pick up the "GOMOD" environment variable (4) (see "go env GOMOD" (4)) that is initialized automatically with the path to the "go.mod" file (5) in the current working directory.
  2. install module/package executables globally without "polluting" the "go.mod" file.
  3. install module/package executables globally without overriding already installed executables of different versions.

See gobin's FAQ page (15) in the repository wiki for more details about the project.

The Go Module Caster

To allow to manage the tool dependency problem, this caster uses "gobin" through to prevent the "pollution" of the project "go.mod" file and allows to...

  1. install "gobin" itself into "GOBIN" (`go env GOBIN` (4)).
  2. cast any spell incantation (16) of kind "KindGoModule" (17) by installing the executable globally into the dedicated "gobin" cache.

References

(1) https://golang.org/ref/mod
(2) https://pkg.go.dev/cmd/go#hdr-Compile_and_install_packages_and_dependencies
(3) https://pkg.go.dev/cmd/go/#hdr-Environment_variables
(4) https://pkg.go.dev/cmd/go/#hdr-Print_Go_environment_information
(5) https://golang.org/ref/mod#go-mod-file
(6) https://pkg.go.dev/cmd/go/#hdr-Print_Go_environment_information
(7) https://golang.org/ref/mod#mod-commands
(8) https://golang.org/cmd/go
(9) https://github.com/golang/go/milestone/145
(10) https://tip.golang.org/doc/go1.16#modules
(11) https://github.com/golang/go/wiki
(12) https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module
(13) https://github.com/myitcv/gobin
(14) https://pkg.go.dev/os/#UserCacheDir
(16) https://github.com/myitcv/gobin/wiki/FAQ
(16) https://pkg.go.dev/github.com/svengreb/wand/pkg/spell#Incantation
(17) https://pkg.go.dev/github.com/svengreb/wand/pkg/spell#KindGoModule

Index

Constants

View Source
const (
	// CasterName is the name of the Go toolchain command caster.
	CasterName = "gobin"

	// DefaultExec is the default name of the "github.com/myitcv/gobin" module executable.
	DefaultExec = "gobin"

	// DefaultGoModulePath is the default "gobin" module import path.
	DefaultGoModulePath = "github.com/myitcv/gobin"

	// DefaultGoModuleVersion is the default "gobin" module version.
	DefaultGoModuleVersion = "v0.0.14"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Caster

type Caster struct {
	// contains filtered or unexported fields
}

Caster is a "github.com/myitcv/gobin" module caster.

func NewCaster

func NewCaster(opts ...Option) (*Caster, error)

NewCaster creates a new "github.com/myitcv/gobin" module caster.

func (*Caster) Cast

func (c *Caster) Cast(si spell.Incantation) error

Cast casts a spell incantation. It returns an error of type *cast.ErrCast when the spell is not a spell.KindGoModule and any other error that occurs during the command execution.

func (*Caster) GetExec

func (c *Caster) GetExec() string

GetExec returns the path to the installed executable of the "github.com/myitcv/gobin" module.

func (*Caster) GoModule

func (c *Caster) GoModule() project.GoModuleID

GoModule returns partial Go module identifier information for the "github.com/myitcv/gobin" module.

func (*Caster) Handles

func (c *Caster) Handles() spell.Kind

Handles returns the supported spell.Kind.

func (*Caster) Install

func (c *Caster) Install(goCaster *castGoToolchain.Caster) error

Install installs the executable of the "github.com/myitcv/gobin" module. It does not "pollute" the "go.mod" file of the project the installation outside of the project root directory but using a the systems temporary directory instead.

See the package documentation for details: https://pkg.go.dev/github.com/svengreb/wand/pkg/cast/gobin

func (*Caster) Validate

func (c *Caster) Validate() error

Validate validates the "github.com/myitcv/gobin" module caster. It returns an error of type *cast.ErrCast when the binary executable does not exists at the configured path and when it is also not available in the executable search paths of the current environment.

type Option

type Option func(*Options)

Option is a "github.com/myitcv/gobin" module caster option.

func WithExec

func WithExec(nameOrPath string) Option

WithExec sets the name or path to the "github.com/myitcv/gobin" module executable. Defaults to DefaultExec.

func WithModulePath

func WithModulePath(path string) Option

WithModulePath sets the "gobin" module import path. Defaults to DefaultGoModulePath.

func WithModuleVersion

func WithModuleVersion(version *semver.Version) Option

WithModuleVersion sets the "gobin" module version. Defaults to DefaultGoModuleVersion.

type Options

type Options struct {
	// Env are caster specific environment variables.
	Env map[string]string

	// Exec ist the name or path of the "gobin" module executable.
	Exec string
	// contains filtered or unexported fields
}

Options stores "github.com/myitcv/gobin" module caster options.

func NewOptions

func NewOptions(opts ...Option) (*Options, error)

NewOptions creates new "github.com/myitcv/gobin" module caster options.

Jump to

Keyboard shortcuts

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