-
Notifications
You must be signed in to change notification settings - Fork 27
/
extcommand.go
82 lines (72 loc) · 1.68 KB
/
extcommand.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
70
71
72
73
74
75
76
77
78
79
80
81
82
package bundle
/*
Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*/
import (
"context"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/facebookincubator/go2chef/util"
)
func chefRuby() string {
if runtime.GOOS == "windows" {
return "C:/opscode/chef/embedded/bin/ruby.exe"
}
return "/opt/chef/embedded/bin/ruby"
}
type BundleEntrypointFinder func(fullPath string, ctx context.Context) *exec.Cmd
var unixLoadOrder = []string{
"bundle.rb",
"bundle.sh",
}
var loadOrder = map[string][]string{
"windows": {
"bundle.rb",
"bundle.ps1",
"bundle.bat",
},
"linux": unixLoadOrder,
"darwin": unixLoadOrder,
}
func findEntrypoint(dir string) (string, error) {
var realLoadOrder []string
if lo, ok := loadOrder[runtime.GOOS]; ok {
realLoadOrder = lo
} else {
realLoadOrder = unixLoadOrder
}
for _, lo := range realLoadOrder {
loPath := filepath.Join(dir, lo)
if util.PathExists(loPath) {
return loPath, nil
}
}
return "", os.ErrNotExist
}
func commandForPath(path string, ctx context.Context) *exec.Cmd {
switch filepath.Ext(path) {
case ".ps1":
return exec.CommandContext(ctx, "powershell.exe", "-ExecutionPolicy", "Bypass", "-File", path)
case ".bat", ".cmd":
return exec.CommandContext(ctx, "cmd", "/c", path)
case ".rb":
return exec.CommandContext(ctx, chefRuby(), path)
case ".sh":
fi, err := os.Lstat(path)
if err != nil {
return nil
}
mode := fi.Mode().Perm()
// If the script is executable, execute it directly
if mode&0111 == 0111 {
return exec.CommandContext(ctx, path)
}
return exec.CommandContext(ctx, "sh", path)
case ".bash":
return exec.CommandContext(ctx, "bash", path)
default:
return nil
}
}