|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "C" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/vanilla-os/vib/api" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +type Shell struct { |
| 14 | + Name string `json:"name"` |
| 15 | + Type string `json:"type"` |
| 16 | + Commands []string `json:"commands"` |
| 17 | + Cwd string `json:"cwd"` |
| 18 | +} |
| 19 | + |
| 20 | +//export PlugInfo |
| 21 | +func PlugInfo() *C.char { |
| 22 | + plugininfo := &api.PluginInfo{Name: "shell-final", Type: api.FinalizePlugin} |
| 23 | + pluginjson, err := json.Marshal(plugininfo) |
| 24 | + if err != nil { |
| 25 | + return C.CString(fmt.Sprintf("ERROR: %s", err.Error())) |
| 26 | + } |
| 27 | + return C.CString(string(pluginjson)) |
| 28 | +} |
| 29 | + |
| 30 | +//export PluginScope |
| 31 | +func PluginScope() int32 { // int32 is defined as GoInt32 in cgo which is the same as a C int |
| 32 | + return api.IMAGENAME | api.FS | api.RECIPE |
| 33 | +} |
| 34 | + |
| 35 | +func parsePath(path string, data *api.ScopeData) string { |
| 36 | + path = strings.ReplaceAll(path, "$PROJROOT", data.Recipe.ParentPath) |
| 37 | + path = strings.ReplaceAll(path, "$FSROOT", data.FS) |
| 38 | + return path |
| 39 | +} |
| 40 | + |
| 41 | +func baseCommand(command string, data *api.ScopeData) string { |
| 42 | + commandParts := strings.Split(command, " ") |
| 43 | + if strings.Contains(commandParts[0], "/") { |
| 44 | + return parsePath(commandParts[0], data) |
| 45 | + } else { |
| 46 | + command, err := exec.LookPath(commandParts[0]) |
| 47 | + if err != nil { |
| 48 | + return commandParts[0] |
| 49 | + } |
| 50 | + return command |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func getArgs(command string, data *api.ScopeData) []string { |
| 55 | + commandParts := strings.Split(parsePath(command, data), " ") |
| 56 | + return commandParts[1:] |
| 57 | +} |
| 58 | + |
| 59 | +func genCommand(command string, data *api.ScopeData) []string { |
| 60 | + baseCommand := baseCommand(command, data) |
| 61 | + args := getArgs(command, data) |
| 62 | + return append(append(append([]string{"-c", "'"}, strings.Join(args, " ")), baseCommand), "'") |
| 63 | +} |
| 64 | + |
| 65 | +//export FinalizeBuild |
| 66 | +func FinalizeBuild(moduleInterface *C.char, extraData *C.char) *C.char { |
| 67 | + var module *Shell |
| 68 | + var data *api.ScopeData |
| 69 | + |
| 70 | + err := json.Unmarshal([]byte(C.GoString(moduleInterface)), &module) |
| 71 | + if err != nil { |
| 72 | + return C.CString(fmt.Sprintf("ERROR: %s", err.Error())) |
| 73 | + } |
| 74 | + |
| 75 | + err = json.Unmarshal([]byte(C.GoString(extraData)), &data) |
| 76 | + if err != nil { |
| 77 | + return C.CString(fmt.Sprintf("ERROR: %s", err.Error())) |
| 78 | + } |
| 79 | + |
| 80 | + for _, command := range module.Commands { |
| 81 | + fmt.Println("shell-final:: bash ", "-c ", command) |
| 82 | + |
| 83 | + cmd := exec.Command( |
| 84 | + "bash", "-c", parsePath(command, data), |
| 85 | + ) |
| 86 | + cmd.Stdin = os.Stdin |
| 87 | + cmd.Stdout = os.Stdout |
| 88 | + cmd.Stderr = os.Stderr |
| 89 | + cmd.Env = os.Environ() |
| 90 | + if len(strings.TrimSpace(module.Cwd)) == 0 { |
| 91 | + cmd.Dir = data.Recipe.ParentPath |
| 92 | + } else { |
| 93 | + cmd.Dir = parsePath(module.Cwd, data) |
| 94 | + } |
| 95 | + |
| 96 | + err = cmd.Run() |
| 97 | + if err != nil { |
| 98 | + return C.CString(fmt.Sprintf("ERROR: %s", err.Error())) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return C.CString("") |
| 103 | +} |
| 104 | + |
| 105 | +func main() {} |
0 commit comments