Skip to content

Commit

Permalink
Code optimization: 代码实现优化。
Browse files Browse the repository at this point in the history
  • Loading branch information
voidint committed May 9, 2019
1 parent 9a37c1a commit 88ed83b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 36 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# g
[![Build Status](https://travis-ci.org/voidint/g.svg?branch=master)](https://travis-ci.org/voidint/g)
[![GoDoc](https://godoc.org/github.com/voidint/g?status.svg)](https://godoc.org/github.com/voidint/g)
[![Go Report Card](https://goreportcard.com/badge/github.com/voidint/g)](https://goreportcard.com/report/github.com/voidint/g)

Expand Down
23 changes: 22 additions & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ package cli
import (
"fmt"
"os"
"path/filepath"

"github.com/urfave/cli"
"github.com/voidint/g/build"
)

var (
rootDir string
downloadsDir string
versionsDir string
goroot string
)

// Run 运行g命令行
func Run() {
app := cli.NewApp()
Expand All @@ -21,7 +29,20 @@ func Run() {
Email: "[email protected]",
},
}

app.Before = func(ctx *cli.Context) (err error) {
homeDir, _ := os.UserHomeDir()
rootDir = filepath.Join(homeDir, ".g")
goroot = filepath.Join(rootDir, "go")
downloadsDir = filepath.Join(rootDir, "downloads")
if err = os.MkdirAll(downloadsDir, 0755); err != nil {
return err
}
versionsDir = filepath.Join(rootDir, "versions")
if err = os.MkdirAll(versionsDir, 0755); err != nil {
return err
}
return nil
}
app.Commands = commands

if err := app.Run(os.Args); err != nil {
Expand Down
12 changes: 2 additions & 10 deletions cli/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,8 @@ func install(ctx *cli.Context) (err error) {
if vname == "" {
return cli.ShowSubcommandHelp(ctx)
}
homeDir, _ := os.UserHomeDir()
rootDir := filepath.Join(homeDir, ".g")
downloadsDir := filepath.Join(rootDir, "downloads")
versionsDir := filepath.Join(rootDir, "versions")
targetV := filepath.Join(versionsDir, vname)

_ = os.MkdirAll(downloadsDir, 0755)
_ = os.MkdirAll(versionsDir, 0755)

// 检查版本是否已经安装
if finfo, err := os.Stat(targetV); err == nil && finfo.IsDir() {
return cli.NewExitError(fmt.Sprintf("[g] %q version has been installed.", vname), 1)
Expand Down Expand Up @@ -79,10 +72,9 @@ func install(ctx *cli.Context) (err error) {
return cli.NewExitError(fmt.Sprintf("[g] %s", err.Error()), 1)
}
// 重新建立软链接
goDir := filepath.Join(rootDir, "go")
_ = os.Remove(goDir)
_ = os.Remove(goroot)

if err := os.Symlink(targetV, goDir); err != nil {
if err := os.Symlink(targetV, goroot); err != nil {
return cli.NewExitError(fmt.Sprintf("[g] %s", err.Error()), 1)
}
fmt.Println("installed successfully")
Expand Down
6 changes: 1 addition & 5 deletions cli/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package cli
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"

Expand All @@ -13,9 +11,7 @@ import (
)

func list(ctx *cli.Context) (err error) {
homeDir, _ := os.UserHomeDir()

infos, err := ioutil.ReadDir(filepath.Join(homeDir, ".g", "versions"))
infos, err := ioutil.ReadDir(versionsDir)
if err != nil {
fmt.Printf("No version installed yet\n\n")
return nil
Expand Down
14 changes: 6 additions & 8 deletions cli/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@ import (
)

func uninstall(ctx *cli.Context) (err error) {
version := ctx.Args().First()
if version == "" {
vname := ctx.Args().First()
if vname == "" {
return cli.ShowSubcommandHelp(ctx)
}
homeDir, _ := os.UserHomeDir()
rootDir := filepath.Join(homeDir, ".g")
versionDir := filepath.Join(rootDir, "versions", version)
targetV := filepath.Join(versionsDir, vname)

if finfo, err := os.Stat(versionDir); err != nil || !finfo.IsDir() {
return cli.NewExitError(fmt.Sprintf("[g] %q version is not installed.", version), 1)
if finfo, err := os.Stat(targetV); err != nil || !finfo.IsDir() {
return cli.NewExitError(fmt.Sprintf("[g] %q version is not installed.", vname), 1)
}

if err := os.RemoveAll(versionDir); err != nil {
if err := os.RemoveAll(targetV); err != nil {
return cli.NewExitError(fmt.Sprintf("[g] Uninstall failed ==> %s", err.Error()), 1)
}
fmt.Println("Uninstall successfully")
Expand Down
21 changes: 9 additions & 12 deletions cli/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,23 @@ import (
)

func use(ctx *cli.Context) (err error) {
version := ctx.Args().First()
if version == "" {
vname := ctx.Args().First()
if vname == "" {
return cli.ShowSubcommandHelp(ctx)
}
homeDir, _ := os.UserHomeDir()
rootDir := filepath.Join(homeDir, ".g")
versionDir := filepath.Join(rootDir, "versions", version)
targetV := filepath.Join(versionsDir, vname)

if finfo, err := os.Stat(versionDir); err != nil || !finfo.IsDir() {
return cli.NewExitError(fmt.Sprintf("[g] The %q version does not exist, please install it first.", version), 1)
if finfo, err := os.Stat(targetV); err != nil || !finfo.IsDir() {
return cli.NewExitError(fmt.Sprintf("[g] The %q version does not exist, please install it first.", vname), 1)
}

goDir := filepath.Join(rootDir, "go")
_ = os.Remove(goDir)
_ = os.Remove(goroot)

if err := os.Symlink(versionDir, goDir); err != nil {
if err := os.Symlink(targetV, goroot); err != nil {
return cli.NewExitError(fmt.Sprintf("[g] %s", err.Error()), 1)
}
if output, err := exec.Command(filepath.Join(goDir, "bin", "go"), "version").Output(); err == nil {
fmt.Println(string(output))
if output, err := exec.Command(filepath.Join(goroot, "bin", "go"), "version").Output(); err == nil {
fmt.Printf(string(output))
}
return nil
}

0 comments on commit 88ed83b

Please sign in to comment.