Repository navigation
Expand file tree
/
Copy pathin_out.go
More file actions
155 lines (134 loc) · 3.77 KB
/
Copy pathin_out.go
File metadata and controls
155 lines (134 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package cmd
import (
"fmt"
"github.com/Masterminds/cookoo"
"os"
"strings"
//"os/user"
"os/exec"
"path/filepath"
)
// AlreadyGliding emits a warning (and stops) if we're in a glide session.
//
// This should be used when you want to make sure that we're not already in a
// glide environment.
func AlreadyGliding(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
if os.Getenv("ALREADY_GLIDING") == "1" {
Warn("You're already gliding. Use `exit` to leave the glide shell.\n")
return true, &cookoo.Stop{}
}
return false, nil
}
// ReadyToGlide fails if the environment is not sufficient for using glide.
//
// Most importantly, it fails if glide.yaml is not present in the current
// working directory.
func ReadyToGlide(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
fname := p.Get("filename", "glide.yaml").(string)
if _, err := os.Stat(fname); err != nil {
cwd, _ := os.Getwd()
return false, fmt.Errorf("%s is missing from %s", fname, cwd)
}
return true, nil
}
func GlideGopath() (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
gopath := fmt.Sprintf("%s/_vendor", cwd)
return gopath, nil
}
// Emits GOPATH for editors and such.
func In(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
gopath, err := GlideGopath()
if err != nil {
return nil, err
}
/*
fmt.Printf("export OLD_PATH=%s\n", os.Getenv("PATH"))
fmt.Printf("export PATH=%s:%s\n", os.Getenv("PATH"), gopath + "/bin")
fmt.Printf("export GOPATH=%s\n", gopath)
fmt.Printf("export ALREADY_GLIDING=1\n")
*/
fmt.Println(gopath)
return nil, nil
}
// Into starts a new shell as a child of glide.
// This new shell inherits the environment typical of a Glide In, but
// without any shell export weirdness. Optionally, if a path is provided, this
// will glide into *that* directory.
//
// PARAMS
// - into (string): The directory to glide into.
func Into(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
cfg := p.Get("conf", &Config{}).(*Config)
into := p.Get("into", "").(string)
if len(into) > 0 {
if err := os.Chdir(into); err != nil {
return nil, err
}
}
// Shell and command args can be overwritten by config.InCommand.
shell := os.Getenv("SHELL")
cmdArgs := []string{shell}
path := os.Getenv("PATH")
/*
u, err := user.Current()
if err != nil {
return nil, err
}
*/
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
gopath := fmt.Sprintf("%s/_vendor", cwd)
os.Setenv("ALREADY_GLIDING", "1")
os.Setenv("GOPATH", gopath)
os.Setenv("GOBIN", gopath+"/bin")
os.Setenv("GLIDE_GOPATH", gopath)
os.Setenv("PATH", path+":"+gopath+"/bin")
os.Setenv("GLIDE_PROJECT", cwd)
os.Setenv("GLIDE_YAML", fmt.Sprintf("%s/glide.yaml", cwd))
pa := os.ProcAttr{
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
Dir: cwd,
}
/*
loginPath, err := exec.LookPath("login")
if err != nil {
return nil, err
}
*/
// Allow incmd to override the Glide In default command.
if len(cfg.InCommand) > 0 {
cmdArgs = strings.Split(cfg.InCommand, " ")
fmt.Printf(">> Running custom 'glide in': %v\n", cmdArgs)
//shell, err = exec.LookPath(cmdArgs[0])
//if err != nil {
//return nil, err
//}
shell = cmdArgs[0]
} else {
fmt.Printf(">> You are now gliding into a new shell. To exit, type 'exit'\n")
}
if !filepath.IsAbs(shell) {
shell, err = exec.LookPath(shell)
if err != nil {
return nil, err
}
}
// Login may work better than executing the shell manually.
//proc, err := os.StartProcess(loginPath, []string{"login", "-fpl", u.Username}, &pa)
proc, err := os.StartProcess(shell, cmdArgs, &pa)
if err != nil {
return nil, err
}
state, err := proc.Wait()
if err != nil {
return nil, err
}
fmt.Printf("Exited glide shell: %s", state.String())
return nil, nil
}