Skip to content

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
PWZER committed Sep 29, 2024
1 parent e31d102 commit bc50d38
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 22 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export PATH=${PATH}:${HOME}/.local/bin:${HOME}/.govm/go/bin
## Usage

```bash
# display info
# display govm info
govm

# list remote versions
Expand All @@ -41,6 +41,12 @@ govm ls
# install a version
govm install go1.23.0

# install with proxy, support environment variable HTTP_PROXY, HTTPS_PROXY, NO_PROXY
HTTP_PROXY=http://proxy:port govm install go1.23.0

# install specify mirror
govm install go1.23.0 --mirror https://golang.google.cn/dl/

# use or change the go version
govm use go1.23.0
```
8 changes: 6 additions & 2 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ var installCmd = &cobra.Command{
Long: "Install specified version of golang",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if installMirror != "" {
internal.Config.InstallMirror = installMirror
}

if len(args) > 0 {
for _, version := range args {
if err := internal.Install(version, installMirror); err != nil {
if err := internal.Install(version); err != nil {
return err
}
}
Expand All @@ -34,6 +38,6 @@ var installCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(installCmd)
installCmd.Flags().StringVarP(&installMirror, "mirror", "m", internal.DefaultMirror,
installCmd.Flags().StringVarP(&installMirror, "mirror", "m", internal.Config.InstallMirror,
"Specify the mirror you want to download the golang package.")
}
9 changes: 1 addition & 8 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,7 @@ var listCmd = &cobra.Command{
Long: "list versions for golang.",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) (err error) {
var getVersionsFunc func(bool) ([]*internal.Version, error)
if listRemoteVersions {
getVersionsFunc = internal.GetRemoteVersions
} else {
getVersionsFunc = internal.GetLocalVersions
}

versions, err := getVersionsFunc(!includeAllVersions)
versions, err := internal.GetVersions(listRemoteVersions, !includeAllVersions)
if err != nil {
return err
}
Expand Down
19 changes: 17 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,23 @@ var rootCmd = &cobra.Command{
}
}

fmt.Println(" Working directory:", internal.Config.WorkingDir)
fmt.Println("Current use version:", version)
infos := map[string]string{
"GoVM version": Version,
"GoVM git commit": GitCommit,
"Working directory": internal.Config.WorkingDir,
"Current use go version": version,
}

maxKeyLen := 0
for key := range infos {
if len(key) > maxKeyLen {
maxKeyLen = len(key)
}
}

for key, value := range infos {
fmt.Printf("%s: %s\n", key+strings.Repeat(" ", maxKeyLen-len(key)), value)
}
return nil
},
}
Expand Down
6 changes: 4 additions & 2 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
)

type ConfigType struct {
WorkingDir string `json:"working_dir"`
WorkingDir string `json:"working_dir"`
InstallMirror string `json:"install_mirror"`
}

var Config ConfigType
Expand All @@ -15,7 +16,8 @@ func init() {
defaultWorkingDir := filepath.Join(getUserHomeDir(), ".govm")

Config = ConfigType{
WorkingDir: defaultWorkingDir,
WorkingDir: defaultWorkingDir,
InstallMirror: "https://golang.google.cn/dl/",
}
}

Expand Down
9 changes: 3 additions & 6 deletions internal/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import (
"strings"
)

const (
DefaultMirror = "https://golang.google.cn/dl/"
)

func init() {
http.DefaultTransport = &userAgentTransport{http.DefaultTransport}
}
Expand Down Expand Up @@ -131,7 +127,7 @@ func downloadArchiveFile(saveFile, url, sha256sum string) error {
return nil
}

func Install(version string, mirror string) (err error) {
func Install(version string) (err error) {
if localVersionAlreadyInstalled(version) {
return nil
}
Expand All @@ -142,7 +138,8 @@ func Install(version string, mirror string) (err error) {
}

saveFile := filepath.Join(getCacheDir(), remoteVersionFile.Filename)
downloadUrl := fmt.Sprintf("%s/%s", mirror, remoteVersionFile.Filename)
downloadUrl := fmt.Sprintf("%s/%s", Config.InstallMirror, remoteVersionFile.Filename)
fmt.Println("Downloading from", downloadUrl)
if err := downloadArchiveFile(saveFile, downloadUrl, remoteVersionFile.SHA256); err != nil {
return err
}
Expand Down
10 changes: 9 additions & 1 deletion internal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func UseGoVersion(version string) (err error) {

if !localVersionAlreadyInstalled(version) {
fmt.Printf("Local version %s not found, installing ...\n", version)
if err = Install(version, DefaultMirror); err != nil {
if err = Install(version); err != nil {
return err
}
}
Expand Down Expand Up @@ -55,3 +55,11 @@ func UseGoVersion(version string) (err error) {
fmt.Printf("Using go version %s\n", version)
return nil
}

func GetVersions(remote bool, all bool) (versions []*Version, err error) {
if remote {
return GetRemoteVersions(all)
} else {
return GetLocalVersions(all)
}
}

0 comments on commit bc50d38

Please sign in to comment.