govm

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2024 License: MIT Imports: 19 Imported by: 0

README

English|简体中文

govm

Go Reference Static Badge

govm is a command line tool for managing local go versions, which allows you to switch between different go versions more easily and quickly, and focus more on development work. It is a small tool that I wrote based on my usual usage habits and other similar open source tools. Since it is written in pure go, it can support mainstream windows, linux, and macos well.

Installation

Download

If you have a go environment and the version is greater than go1.16, you can use go install to install

$ go install github.com/Open-Source-CQUT/govm/cmd/govm@latest

Or you can download the latest binary file for the corresponding platform in Release. Currently, only releases for windows, macos, and linux are provided.

linux

Install the govm file to the /var/lib/govm directory, and then link it to /usr/local/bin

$ ln -s /var/lib/govm/govm /usr/local/bin/govm

Check whether govm is available

$ govm version
govm versoin v1.0.0 linux/amd64

Use the install command to download the latest version

$ sudo govm install --use

Add the following content to $HOME/.bashrc

eval "$(govm profile -s --shell=bash)"

After re-login to the shell, test whether the go environment is available

$ go version
go version go1.22.5 linux/amd64
windows

Add the location of govm.exe to the PATH system variable, and then confirm whether govm is available

$ govm version
govm versoin v1.0.0 windows/amd64

gitbash

Add the following content to the %HOME/.bashrc file

eval "$(govm profile -s --shell=gitbash)"

powershell

Add the following file to the $env:USERPROFILE\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 file. If the file does not exist, create it manually

govm profile -s --shell=powershell | Out-String | Invoke-Expression

Re-login to the shell and test whether the go environment is available

$ go version
go version go1.22.5 windows/amd64
macos

Install the govm binary file to the /var/lib/govm directory, and then link it to the /usr/local/bin directory

$ ln -s /var/lib/govm/govm /usr/local/bin/govm

Check whether govm is available

$ govm version
govm versoin v1.0.0 darwin/amd64

Use the install command to download the latest version

$ sudo govm install --use

Add the following content to the $HOME/.zshrc file

eval "$(govm profile -s --shell=bash)"

Re-login to the shell and test whether the go environment is available

$ go version
go version go1.22.5 darwin/amd64
Other platforms

If you are a user of other platforms, go to Go supported platforms to check whether your platform is supported, and then follow the steps below to compile.

First clone the source code to your local

$ git clone https://github.com/Open-Source-CQUT/govm.git

Switch to a specific version

$ git checkout tags/v1.0.0

Make sure you have the go compiler and make installed locally, and then execute your os and arch as parameters, as shown below

$ make build mode=release os=linux arch=amd64

After the compilation is completed, the compiled binary file will be generated in the bin/release/ directory of the current project. Execute the following command to check whether it runs normally. The following output indicates that the compilation is successful.

$ ./govm version govm version untag linux/amd64

Commands

$ govm -h
govm is a tool to manage local Go versions

Usage:
  govm [command]

Available Commands:
  clean       Clean local cache and redundant versions
  completion  Generate the autocompletion script for the specified shell
  config      Manage govm configs
  current     Show current using Go version
  help        Help about any command
  install     Install specified Go version
  list        List local installed Go versions
  profile     Show profile env
  search      Search available go versions from remote
  uninstall   Uninstall specified Go version
  use         Use specified Go version
  version     Show govm version

Flags:
  -h, --help      help for govm
  -s, --silence   Do not show any tip, warn, error

Use "govm [command] --help" for more information about a command.

govm has a total of 10 commands, most of which are very simple. The following is a simple demonstration of the use of the main commands.

Search for available go versions. You can use regular expressions to match. By default, the first 20 items are sorted from high to low by version.

$ govm search
go1.22.6  	   69 MB
go1.22.5  	   69 MB
go1.22.4  	   69 MB
go1.22.3  	   69 MB
go1.22.2  	   69 MB
go1.22.1  	   69 MB
go1.22.0  	   69 MB
go1.21.13 	   67 MB
go1.21.12 	   67 MB
go1.21.11 	   67 MB
......
go1.21.1  	   67 MB

Search for a specific version

$ govm search 1.18 -n 10
go1.18.10 	  142 MB
go1.18.9  	  142 MB
go1.18.8  	  142 MB
go1.18.7  	  142 MB
go1.18.6  	  142 MB
go1.18.5  	  142 MB
go1.18.4  	  142 MB
go1.18.3  	  142 MB
go1.18.2  	  142 MB
go1.18.1  	  142 MB
install

Install the specified go version. If no parameters are specified, the latest version will be installed

$ govm install
Fetch go1.22.6 from https://dl.google.com/go/go1.22.6.windows-amd64.zip
Downloading go1.22.6.windows-amd64.zip 100% |█████████████████████████████████████| (76/76 MB, 34 MB/s) [2s]
Extract go1.22.6.windows-amd64.zip to local store
Remove archive from cache
Version go1.22.6 installed

Install and set to use version

$ sudo govm install 1.20.14
Fetch go1.20.14 from https://dl.google.com/go/go1.20.14.windows-amd64.zip
Downloading go1.20.14.windows-amd64.zip 100% |████████████████████████████████████| (114/114 MB, 32 MB/s) [3s]
Extract go1.20.14.windows-amd64.zip to local store
Remove archive from cache
Version go1.20.14 installed
Use go1.20.14 now
use

Set an installed version as the used version

$ govm use 1.22.5
Use go1.22.5 now
list

View the locally installed version

$ govm list
go1.22.6 (*)
go1.22.5
go1.22.3
go1.22.1
go1.21rc2
uninstall

Uninstall a specific version

$ sudo govm uninstall 1.22.5
Version 1.22.5 uninstalled
More

For more help information, please view it through govm command help

Configuration

The configuration file of govm is stored in $HOME/.govm/config.toml in all systems. You can view the configuration by the following command

$ govm config
listapi=https://go.dev/dl/?mode=json&include=all
mirror=https://dl.google.com/go/
proxy=(system proxy)
install=/home/username/.govm/store/
Mirror

The default download mirror of govm is to use the go official website. Chinese users are recommended to use the latter two

Use the following command to modify the mirror.

$ govm cfg -w mirror=https://mirrors.aliyun.com/golang/
Version list

The default version list uses the API provided by go officially

https://go.dev/dl/?mode=json&include=all

Modify it according to the following command

$ govm cfg -w listapi=your_cdn
Proxy

The system proxy is used by default, and you can also specify the proxy manually, modify it with the following command

$ govm cfg -w proxy=your_proxy
Installation path

The default storage location is in the .govm/store/ directory, modify it with the following command

$ govm cfg -w install=new_pos

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Silence bool

Functions

func AppendVersion

func AppendVersion(v Version) error

func Bytes2string

func Bytes2string(bytes []byte) string

Bytes2string convert a byte slice to a string without allocating new memory.

func CheckVersion

func CheckVersion(v string) (string, bool)

func ChooseDownloadURL

func ChooseDownloadURL(version string) (string, string, error)

func CompareVersion

func CompareVersion(v1, v2 string) int

func DefaultInstallation

func DefaultInstallation() (string, error)

func DownloadProcessBar

func DownloadProcessBar(length int64, description string, finishedTip string) *progressbar.ProgressBar

func ErrPrintln added in v1.1.0

func ErrPrintln(a ...any)

func Extract

func Extract(archive *os.File, target string) error

Extract extracts archive file to specified target path, only support .zip and .tar.gz

func ExtractTarGzip

func ExtractTarGzip(ctx context.Context, reader io.Reader, target string) error

ExtractTarGzip extract tar.gz from reader and save to target.

func ExtractZip

func ExtractZip(ctx context.Context, reader io.Reader, target string) error

ExtractZip extract zip from reader and save to target.

func GetCacheDir

func GetCacheDir() (string, error)

func GetConfigDir

func GetConfigDir() (string, error)

func GetHttpClient

func GetHttpClient() (*http.Client, error)

func GetInstallation

func GetInstallation() (string, error)

func GetMirror

func GetMirror() (string, error)

func GetStoreDir

func GetStoreDir() (string, error)

func GetUsingVersion added in v1.1.0

func GetUsingVersion() (string, error)

func GetVersionListAPI

func GetVersionListAPI() (string, error)

func MaxVersion

func MaxVersion(vs []string) string

func OpenFile

func OpenFile(filename string, flag int, perm os.FileMode) (*os.File, error)

func Printf added in v1.1.0

func Printf(format string, a ...any)

func Println added in v1.1.0

func Println(a ...any)

func String2bytes

func String2bytes(s string) []byte

String2bytes convert a string to a byte slice without allocating new memory.

func Tipf

func Tipf(format string, a ...any)

func ToUnixPath added in v1.1.0

func ToUnixPath(p string) string

ToUnixPath converts Windows path to Unix-style path.

func UserHomeDir

func UserHomeDir() (string, error)

UserHomeDir returns the home directory for the current user.

func Warnf added in v1.1.0

func Warnf(format string, a ...any)

func WriteConfig

func WriteConfig(cfg *Config) error

WriteConfig write config into config file.

func WriteStore

func WriteStore(storeData *Store) error

Types

type Config

type Config struct {
	// where to query Go versions, default https://go.dev/dl/?mode=json&include=all
	ListAPI string `toml:"listapi" mapstructure:"listapi" comment:"where to query Go versions, default https://go.dev/dl/?mode=json&include=all"`
	// download URL for Go release archive, default https://dl.google.com/go/
	Mirror string `toml:"mirror" mapstructure:"mirror" comment:"where to download Go release archive, default https://dl.google.com/go/"`
	// proxy for HTTP requests, default use system proxy
	Proxy string `toml:"proxy" mapstructure:"proxy" comment:"http proxy, default use system proxy"`
	// where to store cache and package, windows: %USERPROFILE%/AppData/Local/govm/root/ other:  $HOME/.local/govm
	Install string `` /* 154-byte string literal not displayed */
	// using version
	Use string `toml:"use" mapstructure:"use" comment:"using version"`
	// contains filtered or unexported fields
}

func DefaultConfig

func DefaultConfig() (*Config, error)

DefaultConfig returns default config, only for config showing.

func ReadConfig

func ReadConfig() (*Config, error)

ReadConfig read config from config file.

type GoVersion

type GoVersion struct {
	Version string    `json:"version"`
	Stable  bool      `json:"stable"`
	Files   []Version `json:"files"`
}

type Pair

type Pair struct {
	Key, Value string
}

Pair key=value pair format.

func ParsePair

func ParsePair(a string) (Pair, error)

func ParsePairList

func ParsePairList(args []string) ([]Pair, error)

type Store

type Store struct {
	Versions map[string]Version `toml:"store"`
}

func ReadStore

func ReadStore() (*Store, error)

type Version

type Version struct {
	Filename string `toml:"filename" json:"filename"`
	Os       string `toml:"os" json:"os"`
	Arch     string `toml:"arch" json:"arch"`
	Version  string `toml:"version" json:"version"`
	Sha256   string `toml:"sha256" json:"sha256"`
	Size     uint64 `toml:"size" json:"size"`
	Kind     string `toml:"kind" json:"kind"`
	Path     string `toml:"path"`
	Using    bool   `toml:"-"`
	Stable   bool   `toml:"-"`
}

func GetLocalVersions

func GetLocalVersions(ascend bool) ([]Version, error)

GetLocalVersions returns the local versions from store.

func GetRemoteVersion

func GetRemoteVersion(ascend, unstable bool) ([]Version, error)

GetRemoteVersion returns all available go versions from versionURL without git. If unstable is false, it only returns stable versions, otherwise it returns all versions that includes unstable versions.

Directories

Path Synopsis
cmd
pkg

Jump to

Keyboard shortcuts

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