Documentation
¶
Index ¶
- func BinaryPathList(path string) ([]string, error)
- func CanUseGoCmd() error
- func GetInstalledGoVersion() (string, error)
- func GetLatestVer(modulePath string) (string, error)
- func GetPackageVersion(cmdName string) string
- func GoBin() (string, error)
- func InstallLatest(importPath string) error
- func InstallMainOrMaster(importPath string) error
- type GoPaths
- type Package
- type Version
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BinaryPathList ¶
BinaryPathList return list of binary paths.
Example ¶
package main
import (
"fmt"
"log"
"github.com/google/go-cmp/cmp"
"github.com/nao1215/gup/internal/goutil"
)
func main() {
// Get list of files in the current directory
got, err := goutil.BinaryPathList(".")
if err != nil {
log.Fatal(err)
}
want := []string{
"examples_test.go",
"goutil.go",
"goutil_test.go",
}
if cmp.Equal(got, want) {
fmt.Println("Example BinaryPathList: OK")
}
}
Output: Example BinaryPathList: OK
func CanUseGoCmd ¶
func CanUseGoCmd() error
CanUseGoCmd check whether go command install in the system.
Example ¶
package main
import (
"fmt"
"log"
"github.com/nao1215/gup/internal/goutil"
)
func main() {
// If `go` command is available, CanUseGoCmd returns no error
if err := goutil.CanUseGoCmd(); err != nil {
log.Fatal(err) // no go command found
}
fmt.Println("Example CanUseGoCmd: OK")
}
Output: Example CanUseGoCmd: OK
func GetInstalledGoVersion ¶ added in v0.27.0
GetInstalledGoVersion return installed go version.
func GetLatestVer ¶ added in v0.8.0
GetLatestVer execute "$ go list -m -f {{.Version}} <importPath>@latest"
Example ¶
package main
import (
"fmt"
"github.com/nao1215/gup/internal/goutil"
)
func main() {
// Demonstrate error handling when the module is not available.
if _, err := goutil.GetLatestVer("example.com/this/module/does/not/exist"); err != nil {
fmt.Println("Example GetLatestVer: error")
return
}
fmt.Println("Example GetLatestVer: OK")
}
Output: Example GetLatestVer: error
func GetPackageVersion ¶ added in v0.5.0
GetPackageVersion return golang package version
Example (Unknown) ¶
package main
import (
"fmt"
"log"
"github.com/nao1215/gup/internal/goutil"
)
func main() {
// GetPackageVersion returns the version of the package installed via `go install`.
// In this example, we specify a package that is not installed.
got := goutil.GetPackageVersion("gup_dummy")
// Non existing binary returns "unknown"
want := "unknown"
if got == want {
fmt.Println("Example GetPackageVersion: OK")
} else {
log.Fatalf(
"example GetPackageVersion failed. unexpected return. got: %s, want: %s",
got, want,
)
}
}
Output: Example GetPackageVersion: OK
func GoBin ¶
GoBin return $GOPATH/bin directory path.
Example ¶
package main
import (
"fmt"
"log"
"github.com/nao1215/gup/internal/goutil"
)
func main() {
pathDirGoBin, err := goutil.GoBin()
if err != nil {
log.Fatal(err)
}
// By default, GoBin returns the value of GOBIN or GOPATH environment variable.
// But note that on race condition `os.Getenv()` may return a temporary
// directory. Such as `/bin` on U*ix environments.
if pathDirGoBin == "" {
log.Fatal("example GoBin failed. path to go binary is empty")
}
fmt.Println("Example GoBin: OK")
}
Output: Example GoBin: OK
func InstallLatest ¶ added in v0.22.0
InstallLatest execute "$ go install <importPath>@latest"
Example ¶
package main
import (
"fmt"
"log"
"strings"
"github.com/nao1215/gup/internal/goutil"
)
func main() {
// Install installs an executable from a Go package.
err := goutil.InstallLatest("example.com/unknown_user/unknown_package")
// If the package is not found or invalid, Install returns an error.
// In this case it should be an error.
if err == nil {
log.Fatal("example Install failed. non existing/invalid package should return error")
}
// Error message should contain the package path
expectMsg := "can't install example.com/unknown_user/unknown_package"
if strings.Contains(err.Error(), expectMsg) {
fmt.Println("Example Install: OK")
} else {
fmt.Println(err.Error())
}
}
Output: Example Install: OK
func InstallMainOrMaster ¶ added in v0.22.0
InstallMainOrMaster execute "$ go install <importPath>@main" or "$ go install <importPath>@master"
Types ¶
type GoPaths ¶ added in v0.7.1
type GoPaths struct {
// GOBIN is $GOBIN
GOBIN string
// GOPATH is $GOPATH
GOPATH string
// TmpPath is tmporary path for dry run
TmpPath string
}
GoPaths has $GOBIN and $GOPATH
func NewGoPaths ¶ added in v0.7.1
func NewGoPaths() *GoPaths
NewGoPaths return GoPaths instance.
Example ¶
package main
import (
"fmt"
"log"
"github.com/nao1215/gup/internal/goutil"
)
func main() {
// Instantiate GoPaths object
gp := goutil.NewGoPaths()
// By default, NewGoPaths returns a GoPaths object with the value of GOBIN
// or GOPATH environment variable of Go. But note that on race condition
// `os.Getenv()` may return a temporary directory.
if gp.GOBIN == "" && gp.GOPATH == "" {
log.Fatal("example NewGoPaths failed. both GOBIN and GOPATH are empty")
}
fmt.Println("Example NewGoPaths: OK")
}
Output: Example NewGoPaths: OK
func (*GoPaths) EndDryRunMode ¶ added in v0.7.1
EndDryRunMode restore the GOBIN or GOPATH settings.
func (*GoPaths) StartDryRunMode ¶ added in v0.7.1
StartDryRunMode change the GOBIN or GOPATH settings to install the binaries in the temporary directory.
Example ¶
package main
import (
"fmt"
"log"
"os"
"github.com/google/go-cmp/cmp"
"github.com/nao1215/gup/internal/goutil"
)
func main() {
gh := goutil.NewGoPaths()
// StartDryRunMode starts dry run mode. In dry run mode, GoPaths will temporarily
// change the OS env variables of GOBIN or GOPATH. The original values will be
// restored when the `EndDryRunMode` method is called.
if err := gh.StartDryRunMode(); err != nil {
log.Fatalf("example GoPaths.StartDryRunMode failed to start dry mode: %s", err.Error())
}
onDryRunMode := []string{
os.Getenv("GOBIN"),
os.Getenv("GOPATH"),
}
// End dry run mode.
if err := gh.EndDryRunMode(); err != nil {
log.Fatalf("example GoPaths.StartDryRunMode failed to end dry mode: %s", err.Error())
}
offDryRunMode := []string{
os.Getenv("GOBIN"),
os.Getenv("GOPATH"),
}
if cmp.Equal(onDryRunMode, offDryRunMode) {
log.Fatal("example GoPaths.StartDryRunMode failed. dry run mode did not change to temp dir")
}
fmt.Println("Example GoPaths.StartDryRunMode: OK")
}
Output: Example GoPaths.StartDryRunMode: OK
type Package ¶
type Package struct {
// Name is package name
Name string
// ImportPath is the package import path used by 'go install'
ImportPath string
// ModulePath is path where go.mod is stored.
// May not be set if module path cannot be determined
ModulePath string
// Version store Package version (current and latest).
Version *Version
// GoVersion stores version of Go toolchain
GoVersion *Version
}
Package is package information
func GetPackageInformation ¶
GetPackageInformation return golang package information.
Example ¶
package main
import (
"fmt"
"log"
"path/filepath"
"runtime"
"github.com/google/go-cmp/cmp"
"github.com/nao1215/gup/internal/goutil"
)
func main() {
// Prepare the path of go binary module for the example
nameDirCheckSuccess := "check_success"
nameFileBin := "gal"
if runtime.GOOS == "windows" {
nameDirCheckSuccess = "check_success_for_windows"
nameFileBin = "gal.exe" // remember the extension
}
pathFileBin := filepath.Join("..", "..", "cmd", "testdata", nameDirCheckSuccess, nameFileBin)
pkgInfo := goutil.GetPackageInformation([]string{pathFileBin})
if pkgInfo == nil {
log.Fatal("example GetPackageInformation failed. The returned package information is nil")
}
// Expected package information on Linux and macOS
want := []string{
nameFileBin,
"github.com/nao1215/gal/cmd/gal",
"github.com/nao1215/gal",
}
// Actual package information
got := []string{
pkgInfo[0].Name,
pkgInfo[0].ImportPath,
pkgInfo[0].ModulePath,
}
if cmp.Equal(got, want) {
fmt.Println("Example GetPackageInformation: OK")
} else {
log.Fatalf("example GetPackageInformation failed. got: %#v, want: %#v", got, want)
}
}
Output: Example GetPackageInformation: OK
func (*Package) CurrentToLatestStr ¶ added in v0.7.1
CurrentToLatestStr returns string about the current version and the latest version
func (*Package) IsGoUpToDate ¶ added in v0.28.0
IsGoUpToDate checks if the Golang runtime version is up to date. Returns true if current >= available.
func (*Package) IsPackageUpToDate ¶ added in v0.28.0
IsPackageUpToDate checks if the Package (set by the package author) version is up to date. Returns true if current >= available.
Example ¶
package main
import (
"fmt"
"github.com/nao1215/gup/internal/goutil"
)
func main() {
// Create Version object with Current and Latest package and Go versions
ver := goutil.Version{
Current: "v1.9.0",
Latest: "v1.9.1",
}
goVer := goutil.Version{
Current: "go1.21.1",
Latest: "go1.22.4",
}
pkg := goutil.Package{Version: &ver, GoVersion: &goVer}
// Check if Current is already up to date (expected: false)
if pkg.IsPackageUpToDate() && pkg.IsGoUpToDate() {
fmt.Println("Example IsUpToDate: already up to date.")
} else {
fmt.Println("Example IsUpToDate: outdated. Newer latest version or installed Go toolchain exists.")
}
}
Output: Example IsUpToDate: outdated. Newer latest version or installed Go toolchain exists.
func (*Package) SetLatestVer ¶ added in v0.7.1
func (p *Package) SetLatestVer()
SetLatestVer set package latest version.
Example ¶
----------------------------------------------------------------------------
Type: Package
----------------------------------------------------------------------------
package main
import (
"fmt"
"log"
"github.com/nao1215/gup/internal/goutil"
)
func main() {
packages := goutil.GetPackageInformation([]string{"../../cmd/testdata/check_success/gal"})
if len(packages) == 0 {
log.Fatal("example GetPackageInformation failed. The returned package information is nil")
}
// test with the first package found
pkgInfo := packages[0]
// By default, the Latest field of Package object is empty
before := pkgInfo.Version.Latest
// Execute method and update the Version.Latest field
pkgInfo.SetLatestVer()
// After calling SetLatestVer, the Latest field should be updated with the latest
// version or `unknown` if the latest version is not found.
after := pkgInfo.Version.Latest
// Require the field to be updated
if before == after {
log.Fatalf(
"example Package.SetLatestVer failed. The latest version is not updated. before: %s, after: %s",
before, after,
)
}
fmt.Println("Example Package.SetLatestVer: OK")
}
Output: Example Package.SetLatestVer: OK
func (*Package) VersionCheckResultStr ¶ added in v0.9.1
VersionCheckResultStr returns string about command version check.
type Version ¶ added in v0.7.1
type Version struct {
// Current(before update) version
Current string
// Latest(after update) version
Latest string
}
Version is package version information.
func NewVersion ¶ added in v0.7.1
func NewVersion() *Version
NewVersion return Version instance.
Example ¶
package main
import (
"fmt"
"log"
"github.com/nao1215/gup/internal/goutil"
)
func main() {
// Instantiate Version object
ver := goutil.NewVersion()
// By default, Current and Latest fields are empty
if ver.Current != "" {
log.Fatal("example NewVersion failed. the field Current is not empty")
}
if ver.Latest != "" {
log.Fatal("example NewVersion failed. the field Latest is not empty")
}
fmt.Println("Example NewVersion: OK")
}
Output: Example NewVersion: OK