pkg

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2022 License: MIT Imports: 13 Imported by: 7

Documentation

Overview

Helper methods for installing Go packages on any platform.

This takes into account both the operating system and the shell environment that the process is running within. This enables cross-platform installations on MacOS, Windows with WSL, Windows with PowerShell/CMD and Windows with Git Bash (mingw).

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckCommandVersion added in v0.9.0

func CheckCommandVersion(cmd string, versionCmd string, versionConstraint string) (bool, error)

CheckCommandVersion determines if the specified command is available and if specified, that the version command returned a version that matches the semver constraint. For example, 1.x or ~2.3. See https://github.com/Masterminds/semver for details on how to specify a version constrain.

func DownloadToGopathBin added in v0.3.0

func DownloadToGopathBin(srcTemplate string, name string, version string) error

DownloadToGopathBin downloads an executable file to GOPATH/bin. src can include the following template values:

  • {{.GOOS}}
  • {{.GOARCH}}
  • {{.EXT}}
  • {{.VERSION}}
Example
package main

import (
	"log"

	"github.com/carolynvs/magex/pkg"
	"github.com/carolynvs/magex/pkg/gopath"
)

func main() {
	url := "https://storage.googleapis.com/kubernetes-release/release/{{.VERSION}}/bin/{{.GOOS}}/{{.GOARCH}}/kubectl{{.EXT}}"
	err := pkg.DownloadToGopathBin(url, "kubectl", "v1.19.0")
	if err != nil {
		log.Fatal("could not download kubectl")
	}

	// Add GOPATH/bin to PATH if necessary so that we can immediately
	// use the installed tool
	gopath.EnsureGopathBin()
}
Output:

func EnsureMage

func EnsureMage(defaultVersion string) error

EnsureMage checks if mage is installed, and installs it if needed.

When version is specified, detect if a compatible version is installed, and if not, install that specific version of mage. Otherwise, install the most recent code from the main branch.

Example
package main

import (
	"log"

	"github.com/carolynvs/magex/pkg"
)

func main() {
	// Leave the version parameter blank to only check if it is installed, and
	// if not install the latest version.
	err := pkg.EnsureMage("")
	if err != nil {
		log.Fatal("could not install mage")
	}
}
Output:

func EnsurePackage deprecated

func EnsurePackage(pkg string, defaultVersion string, versionArgs ...string) error

EnsurePackage checks if the package is installed and installs it if needed. Optionally accepts the argument or flag to pass to the command to check the installed version, and a semver range to use to validate the installed version, such as ^1.2.3 or 2.x. When no version arguments are supplied, any installed version is acceptable.

When defaultVersion is specified, and a version constraint is not, the default is used as the minimum version and sets the allowed major version. For example, a defaultVersion of 1.2.3 would result in a constraint of ^1.2.3. When no defaultVersion is specified, the latest version is installed.

Deprecated: Use EnsurePackageWith.

Example
package main

import (
	"log"

	"github.com/carolynvs/magex/pkg"
)

func main() {
	// Install packr2@v2.8.3 using the command `packr2 version` to detect if the
	// correct version is installed.
	err := pkg.EnsurePackage("github.com/gobuffalo/packr/v2/packr2", "v2.8.3", "version")
	if err != nil {
		log.Fatal("could not install packr2")
	}
}
Output:

func EnsurePackageWith added in v0.9.0

func EnsurePackageWith(opts EnsurePackageOptions) error

EnsurePackageWith checks if the package is installed and installs it if needed.

func GetCommandVersion added in v0.9.0

func GetCommandVersion(cmd string, versionCmd string) (string, error)

GetCommandVersion executes the specified command to get its version The result is the contents of standard output of calling the command, and probably includes additional text besides the version number.

func InstallMage

func InstallMage(version string) error

InstallMage mage into GOPATH and add GOPATH/bin to PATH if necessary.

When version is specified, install that version. Otherwise install the most recent code from the default branch.

func InstallPackage

func InstallPackage(pkg string, version string) error

InstallPackage installs the latest version of a package.

When version is specified, install that version. Otherwise, install the most recent version. Deprecated: Use InstallPackageWith instead.

Example
package main

import (
	"log"

	"github.com/carolynvs/magex/pkg"
)

func main() {
	// Install packr2@v2.8.3
	err := pkg.InstallPackage("github.com/gobuffalo/packr/v2/packr2", "v2.8.3")
	if err != nil {
		log.Fatal("could not install packr2")
	}
}
Output:

func InstallPackageWith added in v0.9.0

func InstallPackageWith(opts InstallPackageOptions) error

InstallPackageWith unconditionally installs a package

Example
package main

import (
	"log"

	"github.com/carolynvs/magex/pkg"
)

func main() {
	// Install packr2@v2.8.3 into the bin/ directory
	opts := pkg.InstallPackageOptions{
		Name:        "github.com/gobuffalo/packr/v2/packr2",
		Destination: "bin",
		Version:     "v2.8.3",
	}
	err := pkg.InstallPackageWith(opts)
	if err != nil {
		log.Fatal("could not install packr2@v2.8.3 into bin")
	}
}
Output:

func IsCommandAvailable

func IsCommandAvailable(cmd string, versionCmd string, versionConstraint string) (bool, error)

IsCommandAvailable determines if a command can be called based on the current PATH.

Types

type EnsurePackageOptions added in v0.9.0

type EnsurePackageOptions struct {
	// Name of the Go package
	// Provide the name of the package that should be compiled into a cli,
	// such as github.com/gobuffalo/packr/v2/packr2
	Name string

	// DefaultVersion is the version to install, if not found. When specified, and
	// AllowedVersion is not, DefaultVersion is used as the minimum version and sets
	// the allowed major version. For example, a DefaultVersion of 1.2.3 would result
	// in an AllowedVersion of ^1.2.3. When no DefaultVersion is specified, the
	// latest version is installed.
	DefaultVersion string

	// AllowedVersion is a semver range that specifies which versions are acceptable
	// if found. For example, ^1.2.3 or 2.x. When unspecified, any installed version
	// is acceptable. See https://github.com/Masterminds/semver for further
	// documentation.
	AllowedVersion string

	// Destination is the location where the CLI should be installed
	// Defaults to GOPATH/bin. Using ./bin is recommended to require build tools
	// without modifying the host environment.
	Destination string

	// VersionCommand is the arguments to pass to the CLI to determine the installed version.
	// For example, "version" or "--version". When unspecified the CLI is called without any arguments.
	VersionCommand string
}

EnsurePackageOptions are the set of options that can be passed to EnsurePackageWith.

type InstallPackageOptions added in v0.9.0

type InstallPackageOptions struct {
	// Name of the Go package
	// Provide the name of the package that should be compiled into a cli,
	// such as github.com/gobuffalo/packr/v2/packr2
	Name string

	// Destination is the location where the CLI should be installed
	// Defaults to GOPATH/bin. Using ./bin is recommended to require build tools
	// without modifying the host environment.
	Destination string

	// Version of the package to install.
	Version string
}

InstallPackageOptions are the set of options that can be passed to InstallPackageWith.

Directories

Path Synopsis
Helper methods for working with archived/compressed files.
Helper methods for working with archived/compressed files.

Jump to

Keyboard shortcuts

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