Skip to content

Commit

Permalink
up: update some logic and some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 2, 2022
1 parent 803b3bf commit d84d60b
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 89 deletions.
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2016 inhere

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Go multi version env manager

- features TODO

> **[EN README](README.md)**
> **[中文说明](README.zh-CN.md)**
## Install

Expand Down Expand Up @@ -57,3 +57,14 @@ go run ./cmd/goenv
```bash
go install ./cmd/goenv
```

## Base on

- https://github/gookit/color
- https://github/gookit/config
- https://github/gookit/gcli
- https://github/gookit/goutil

## LICENSE

[MIT](LICENSE)
49 changes: 47 additions & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,61 @@

- features TODO

> **[中文说明](README.zh-CN.md)**
> **[EN README](README.md)**
## Install

**Use go install**

```shell
go get github.com/inherelab/goenv
go install github.com/inherelab/goenv/cmd/goenv
```

## Usage

```shell
goenv
```

switch version:

```shell
goenv switch 1.16
```
Or:

```shell
goenv use 1.16
```

## Development

### Clone

```shell
go clone https://github.com/inherelab/goenv
cd goenv
```

### Run

```bash
go run ./cmd/goenv
```

### Install

```bash
go install ./cmd/goenv
```

## Base on

- https://github/gookit/color
- https://github/gookit/config
- https://github/gookit/gcli
- https://github/gookit/goutil

## LICENSE

[MIT](LICENSE)
2 changes: 1 addition & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ func addCommands() {

// MakeAdaptor instance
func makeAdaptor() (internal.Adaptor, error) {
return internal.NewEnvManager(goenv.Cfg.Mode).MakeAdaptor()
return internal.MakeAdaptor(goenv.Cfg.Mode)
}
53 changes: 43 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,62 @@
package goenv

var defConf = `
#
var defConf = `#
# config for https://github.com/inherelab/goenv
# author: https://github.com/inhere
#
# mode allow: goenv, brew
# mode allow: auto, goenv, brew
mode: brew
# on use brew mode
brew_lib_dir: /usr/local/opt
# use goenv mode.
# https://golang.org/dl
dl_host: https://golang.google.cn/dl
# install go dir.
install_dir: /usr/local/go
# custom add env map
env_map:
GO_ROOT: path/to
APP_ENV: dev
current:
version: 1.16
path: /usr/local/opt/go@1.16
# use goenv mode.
# https://golang.org/dl
dl_host: https://golang.google.cn/dl
# install go dir.
install_dir: /usr/local/go
`

// mode consts
const (
ModeAuto = "auto"
ModeGoEnv = "goenv"
ModeBrew = "brew"
ModeScoop = "scoop"
)

// appConf struct
type appConf struct {
// ConfFile path
ConfFile string

// Mode allow: auto, goenv, brew
Mode string `json:"mode" default:"auto"`
// BrewLibDir path
BrewLibDir string `json:"brew_lib_dir" default:"/usr/local/opt"`

// DlHost address, on use mode=goenv
DlHost string `json:"dl_host" default:"https://golang.org/dl"`
// InstallDir the go install dir.
InstallDir string `json:"install_dir" default:"/usr/local/go"`
}

// IsBrewMode check
func (c *appConf) IsBrewMode() bool {
return c.Mode == ModeBrew
}

// IsGoEnvMode check
func (c *appConf) IsGoEnvMode() bool {
return c.Mode == ModeGoEnv
}
30 changes: 17 additions & 13 deletions etc/goenv.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
#
# config for https://github.com/inherelab/goenv
# author: https://github.com/inhere
#

# mode allow: goenv, brew
mode: brew

# on use brew mode
brew_lib_dir: /usr/local/opt

# use goenv mode.
# https://golang.org/dl
dl_host: https://golang.google.cn/dl

# install go dir.
install_dir: /usr/local/go
mode: brew

env:
GO_ROOT: path/to
# custom add env map
env_map:
APP_ENV: dev

current:
version: 1.16
path: /path/to

brew:
lib_path: /usr/local/opt
# versions:
# - version: 1.16
# path: /usr/local/opt/go@1.16
# - version: 1.19
# path: /usr/local/opt/go@1.19
path: /usr/local/opt/go@1.16
31 changes: 0 additions & 31 deletions goenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import (
"github.com/gookit/goutil/sysutil"
)

const (
ModeGoEnv = "goenv"
ModeBrew = "brew"
)

var defaultFile = "~/.config/goenv/goenv.yml"
var ConfFile = envutil.Getenv("GOENV_CONF_FILE", defaultFile)
var Version = "0.0.1"
Expand All @@ -23,32 +18,6 @@ var CfgMgr *config.Config
// Cfg for the application
var Cfg = &appConf{}

// appConf struct
type appConf struct {
// ConfFile path
ConfFile string

// Mode allow: goenv, brew
Mode string `json:"mode" default:"goenv"`
// BrewLibDir path
BrewLibDir string `json:"brew_lib_dir" default:"/usr/local/opt"`

// DlHost address, on use mode=goenv
DlHost string `json:"dl_host" default:"https://golang.org/dl"`
// InstallDir the go install dir.
InstallDir string `json:"install_dir" default:"/usr/local/go"`
}

// IsBrewMode check
func (c *appConf) IsBrewMode() bool {
return c.Mode == ModeBrew
}

// IsGoEnvMode check
func (c *appConf) IsGoEnvMode() bool {
return c.Mode == ModeGoEnv
}

// Init config and more
func Init() error {
if err := loadConfig(); err != nil {
Expand Down
33 changes: 17 additions & 16 deletions internal/brew.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func (a *BrewAdaptor) fmtVerAndLibPath(ver string) (string, string) {
func (a *BrewAdaptor) Switch(ver string) error {
ver, insPath := a.fmtVerAndLibPath(ver)
if !fsutil.PathExists(insPath) {
if interact.Confirm(fmt.Sprintf("Not found go%s. Install now?", ver), true) {
return a.Install(ver)
}

return errorx.Rawf("not found Go %s on %s", ver, "/usr/local/opt")
}

Expand All @@ -96,26 +100,24 @@ func (a *BrewAdaptor) Switch(ver string) error {
return nil
}

var line string

cmdline := "brew unlink go@" + old
cliutil.Magentaln("Unbinding links for go:", cmdline)
line, err = cliutil.ExecLine(cmdline)
// cmdline = "brew unlink go@" + old
cmdArgs := arrutil.Strings{"brew", "unlink", "go@" + old}
cliutil.Magentaln("Unbinding links for go:", cmdArgs.Join(" "))
err = sysutil.FlushExec(cmdArgs[0], cmdArgs[1:]...)
if err != nil {
return err
}
fmt.Println(line)

cmdline = "brew link go@" + ver
cliutil.Magentaln("Binding links for go:", cmdline)
line, err = cliutil.ExecLine(cmdline)
// "brew link go@" + ver
cmdArgs = arrutil.Strings{"brew", "link", "go@" + ver}
cliutil.Magentaln("Binding links for go:", cmdArgs.Join(" "))
err = sysutil.FlushExec(cmdArgs[0], cmdArgs[1:]...)
if err != nil {
return err
}
fmt.Println(line)

cliutil.Infoln("Switch successful!")
return sysutil.NewCmd("go", "version").OutputToStd().Run()
return sysutil.NewCmd("go", "version").FlushRun()
}

// Install go by given version
Expand All @@ -129,12 +131,11 @@ func (a *BrewAdaptor) Install(ver string) error {

c := sysutil.NewCmd("brew", "install")
c.WithArgf("go@%s", ver)
c.OutputToStd()
c.BeforeExec = func(c *sysutil.Cmd) {
c.RunBefore = func(c *cmdr.Cmd) {
cliutil.Yellowln(">", c.Cmdline())
}

return c.Run()
return c.FlushRun()
}

// Update go by given version
Expand All @@ -150,7 +151,7 @@ func (a *BrewAdaptor) Update(ver string) error {
c := sysutil.NewCmd("brew", "upgrade")
c.WithArgf("go@%s", ver)
c.OutputToStd()
c.BeforeExec = func(c *sysutil.Cmd) {
c.RunBefore = func(c *cmdr.Cmd) {
cliutil.Yellowln(">", c.Cmdline())
}

Expand All @@ -169,7 +170,7 @@ func (a *BrewAdaptor) Uninstall(ver string) error {
c := sysutil.NewCmd("brew", "uninstall")
c.WithArgf("go@%s", ver)
c.OutputToStd()
c.BeforeExec = func(c *sysutil.Cmd) {
c.RunBefore = func(c *cmdr.Cmd) {
cliutil.Yellowln(">", c.Cmdline())
}

Expand Down
Loading

0 comments on commit d84d60b

Please sign in to comment.