forked from Masterminds/glide
/
drop_shell.go
69 lines (58 loc) · 1.6 KB
/
drop_shell.go
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
package cmd
import (
"fmt"
"os"
"os/exec"
"github.com/Masterminds/cookoo"
)
// DropToShell executes a glide plugin. A command that's implemented by
// another application is executed in a similar manner to the way git commands
// work. For example, 'glide foo' would try to execute the application glide-foo.
// Params:
// - command: the name of the command to attempt executing.
func DropToShell(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
args := c.Get("os.Args", nil).([]string)
command := p.Get("command", "").(string)
if len(args) == 0 {
return nil, fmt.Errorf("Could not get os.Args.")
}
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
projpath := cwd
if tmp := os.Getenv("GLIDE_PROJECT"); len(tmp) != 0 {
projpath = tmp
}
cmd := "glide-" + command
var fullcmd string
if fullcmd, err = exec.LookPath(cmd); err != nil {
fullcmd = projpath + "/" + cmd
if _, err := os.Stat(fullcmd); err != nil {
return nil, fmt.Errorf("Command %s does not exist.", cmd)
}
}
// Turning os.Args first argument from `glide` to `glide-command`
args[0] = cmd
// Removing the first argument (command)
removed := false
for i, v := range args {
if removed == false && v == command {
args = append(args[:i], args[i+1:]...)
removed = true
}
}
pa := os.ProcAttr{
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
Dir: cwd,
}
fmt.Printf("Delegating to plugin %s (%v)\n", fullcmd, args)
proc, err := os.StartProcess(fullcmd, args, &pa)
if err != nil {
return nil, err
}
if _, err := proc.Wait(); err != nil {
return nil, err
}
return nil, nil
}