Skip to content

Commit 59262dc

Browse files
committed
add init and setup command
1 parent 4bd292e commit 59262dc

File tree

5 files changed

+117
-7
lines changed

5 files changed

+117
-7
lines changed

.envrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
mkdir -p ~/.gip
1+
mkdir -p ~/.gip/gip
22
export GOPATH=~/.gip/gip:$GOPATH

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ gip要解决什么问题?
1515
gip怎么做
1616
===
1717

18-
参考了 [vg](https://github.com/GetStream/vg)[gigo](https://github.com/LyricalSecurity/gigo) 的思路,gip想要实现的是python中 [pip](https://pypi.python.org/pypi/pip) 的组合,再配合[direnv](https://github.com/direnv/direnv)来实现GOPATH的切换
18+
参考了 [vg](https://github.com/GetStream/vg)[gigo](https://github.com/LyricalSecurity/gigo) 的思路,gip想要实现的是python中 [pip](https://pypi.python.org/pypi/pip) 的组合,再配合[direnv](https://github.com/direnv/direnv)来实现GOPATH的切换。
19+
20+
NOTE:大量借鉴了[vg](https://github.com/GetStream/vg) 的代码设计和实现。
1921

2022
1. 每次进入一个项目,自动激活这个项目的GOPATH
2123
- 依赖direnv实现;在项目根目录下准备.envrc文件,当进入该文件夹时,自动将配置的GOPATH加到默认GOPATH之前;
@@ -25,6 +27,15 @@ gip怎么做
2527
使用方法
2628
===
2729

30+
0. 安装gip
31+
32+
```
33+
# install gip
34+
go get -u github.com/caojia/gip
35+
# setup of direnv
36+
gip setup
37+
```
38+
2839
1. 初始化一个项目
2940

3041
```

cmd/init.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ import (
2424
)
2525

2626
var envrc = ".envrc"
27-
var template = `
28-
mkdir -p %[1]s
27+
var template = `mkdir -p %[1]s
2928
export GOPATH=%[1]s:$GOPATH
3029
`
3130

cmd/root.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func init() {
7373
// will be global for your application.
7474
//RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.gip.yaml)")
7575
RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose mode")
76-
RootCmd.PersistentFlags().BoolVarP(&ignoreGlobal, "ignore-global", false, "if it is set to true, none of the packages will be installed in global GOPATH.")
76+
RootCmd.PersistentFlags().BoolVar(&ignoreGlobal, "ignore-global", false, "if it is set to true, none of the packages will be installed in global GOPATH.")
7777

7878
// Cobra also supports local flags, which will only run
7979
// when this action is called directly.
@@ -112,6 +112,4 @@ func initConfig() {
112112
if ignoreGlobal {
113113
helper.IgnoreGlobal()
114114
}
115-
116-
RootCmd.DebugFlags()
117115
}

cmd/setup.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright © 2017 NAME HERE <EMAIL ADDRESS>
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package cmd
15+
16+
import (
17+
"fmt"
18+
19+
"github.com/spf13/cobra"
20+
"os/exec"
21+
"github.com/caojia/gip/log"
22+
"runtime"
23+
)
24+
25+
var direnvVersion = "v2.12.2"
26+
27+
func setupBash() {
28+
exec.Command("echo", "'eval $(direnv hook bash)'", ">>", "~/.bashrc")
29+
}
30+
31+
func setupZsh() {
32+
exec.Command("echo", "'eval $(direnv hook zsh)'", ">>", "~/.zshrc")
33+
}
34+
35+
func setupDarwin() {
36+
_, err := exec.Command("command", "-v", "brew").Output()
37+
// if brew is installed, use brew, otherwise, use default
38+
if err == nil {
39+
output, err := exec.Command("brew", "install", "direnv").CombinedOutput()
40+
if err != nil {
41+
log.Error(string(output))
42+
return
43+
}
44+
} else {
45+
setupDefault()
46+
}
47+
}
48+
49+
func setupDefault() {
50+
//https://github.com/direnv/direnv/releases/download/v2.12.2/direnv.darwin-386
51+
url := fmt.Sprintf("https://github.com/direnv/direnv/releases/download/%s/direnv.%s-%s",
52+
direnvVersion, runtime.GOOS, runtime.GOARCH)
53+
exec.Command("mkdir", "-p", "~/bin")
54+
exec.Command("wget", "-O", "~/bin/direnv", url)
55+
exec.Command("chmod", "+x", "~/bin/direnv")
56+
}
57+
58+
// setupCmd represents the setup command
59+
var setupCmd = &cobra.Command{
60+
Use: "setup",
61+
Short: "Install and setup the required commands.",
62+
Long: `Install and setup the required commands. Including:
63+
64+
- install direnv
65+
- setup direnv for bash/zsh`,
66+
Run: func(cmd *cobra.Command, args []string) {
67+
_, err := exec.Command("command", "-v", "direnv").Output()
68+
if err == nil {
69+
log.Info(`direnv is already installed, please make sure it is set up properly.
70+
71+
For more information, check out: https://github.com/direnv/direnv
72+
`)
73+
return
74+
}
75+
switch runtime.GOOS {
76+
case "darwin":
77+
setupDarwin()
78+
case "windows":
79+
panic("Sorry, we don't support windows yet.")
80+
default:
81+
setupDefault()
82+
}
83+
84+
setupBash()
85+
setupZsh()
86+
log.Info(`If you're not using bash or zsh, please follow https://github.com/direnv/direnv to setup direnv for your shell.`)
87+
},
88+
}
89+
90+
func init() {
91+
RootCmd.AddCommand(setupCmd)
92+
93+
// Here you will define your flags and configuration settings.
94+
95+
// Cobra supports Persistent Flags which will work for this command
96+
// and all subcommands, e.g.:
97+
// setupCmd.PersistentFlags().String("foo", "", "A help for foo")
98+
99+
// Cobra supports local flags which will only run when this command
100+
// is called directly, e.g.:
101+
// setupCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
102+
}

0 commit comments

Comments
 (0)