goutil

package
v0.25.1 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BinaryPathList

func BinaryPathList(path string) ([]string, error)

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 GetLatestVer added in v0.8.0

func GetLatestVer(modulePath string) (string, error)

GetLatestVer execute "$ go list -m -f {{.Version}} <importPath>@latest"

Example
package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/nao1215/gup/internal/goutil"
)

func main() {
	// Get the latest version of a package
	verLatest, err := goutil.GetLatestVer("github.com/mattn/go-colorable")
	if err != nil {
		log.Fatal(err)
	}

	// As of 2022/09/17, the latest version of go-colorable is v0.1.13
	expectMin := "v0.1.13"

	if strings.Compare(expectMin, verLatest) <= 0 {
		fmt.Println("Example GetLatestVer: OK")
	} else {
		log.Fatalf("latest version is older than expected. expect: %s, latest: %s",
			expectMin, verLatest)
	}
}
Output:

Example GetLatestVer: OK

func GetPackageVersion added in v0.5.0

func GetPackageVersion(cmdName string) string

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

func GoBin() (string, error)

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

func InstallLatest(importPath string) error

InstallLatest execute "$ go install <importPath>@latest"

func InstallMainOrMaster added in v0.22.0

func InstallMainOrMaster(importPath string) error

InstallMainOrMaster execute "$ go install <importPath>@main" or "$ go install <importPath>@master"

func IsAlreadyUpToDate added in v0.8.0

func IsAlreadyUpToDate(ver Version) bool

IsAlreadyUpToDate return whether binary is already up to date or not.

Example
package main

import (
	"fmt"

	"github.com/nao1215/gup/internal/goutil"
)

func main() {
	// Create Version object with Current and Latest package version
	ver := goutil.Version{
		Current: "v1.9.0",
		Latest:  "v1.9.1",
	}

	// Check if Current is already up to date (expected: false)
	if goutil.IsAlreadyUpToDate(ver) {
		fmt.Println("Example IsAlreadyUpToDate: already up to date.")
	} else {
		fmt.Println("Example IsAlreadyUpToDate: outdated. Newer latest version exists.")
	}

}
Output:

Example IsAlreadyUpToDate: outdated. Newer latest version exists.

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

func (gp *GoPaths) EndDryRunMode() error

EndDryRunMode restore the GOBIN or GOPATH settings.

func (*GoPaths) StartDryRunMode added in v0.7.1

func (gp *GoPaths) StartDryRunMode() error

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 import path for 'go install'
	ImportPath string
	// ModulePath is path where go.mod is stored
	ModulePath string
	// Version store Package version (current and latest).
	Version *Version
}

Package is package information

func GetPackageInformation

func GetPackageInformation(binList []string) []Package

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

func (p *Package) CurrentToLatestStr() string

CurrentToLatestStr returns string about the current version and the latest version

Example
package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/nao1215/gup/internal/goutil"
)

func main() {
	// Set the paths of the target binary
	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]

	wantContain := "Already up-to-date"
	got := pkgInfo.CurrentToLatestStr()

	if !strings.Contains(got, wantContain) {
		log.Fatalf(
			"example Package.CurrentToLatestStr failed. \nwant contain: %s\n got: %s",
			wantContain, got,
		)
	}

	fmt.Println("Example Package.CurrentToLatestStr: OK")
}
Output:

Example Package.CurrentToLatestStr: OK

func (*Package) SetLatestVer added in v0.7.1

func (p *Package) SetLatestVer()

SetLatestVer set package latest version.

Example
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

func (p *Package) VersionCheckResultStr() string

VersionCheckResultStr returns string about command version check.

Example
package main

import (
	"fmt"
	"log"
	"strings"

	"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]

	wantContain := "Already up-to-date"
	got := pkgInfo.VersionCheckResultStr()

	if !strings.Contains(got, wantContain) {
		log.Fatalf(
			"example Package.VersionCheckResultStr failed. \nwant contain: %s\n got: %s",
			wantContain, got,
		)
	}

	fmt.Println("Example Package.VersionCheckResultStr: OK")
}
Output:

Example Package.VersionCheckResultStr: OK

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

Jump to

Keyboard shortcuts

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