/
main.go
94 lines (78 loc) · 2.15 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
/*
Simple plugin for Drone that allows execution of the Rancher Compose CLI.
*/
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/drone/drone-plugin-go/plugin"
)
// We accept a list of Rancher Compose commands as input parameters.
type ComposerCommands struct {
Commands []string `json:"commands"`
}
// Cleans out empty strings from the given array of strings.
func CleanSlice(arr []string) (result []string) {
for _, v := range arr {
if len(v) > 0 {
result = append(result, v)
}
}
return
}
func main() {
var err error
var path, wd string
repo := plugin.Repo{}
build := plugin.Build{}
workspace := plugin.Workspace{}
cmds := ComposerCommands{}
plugin.Param("repo", &repo)
plugin.Param("build", &build)
plugin.Param("workspace", &workspace)
plugin.Param("vargs", &cmds)
err = plugin.Parse()
if err != nil {
fmt.Printf("Error while attempting to parse input: %s\n", err)
os.Exit(1)
}
fmt.Printf("Got commands list:\n")
for _, c := range cmds.Commands {
fmt.Printf("- %s\n", c)
}
// let's try to find the Rancher Compose executable
path, err = exec.LookPath("rancher-compose")
if err != nil {
fmt.Printf("Could not find rancher-compose executable on path: %s\n", err)
os.Exit(1)
}
// chdir to the repo path
if workspace.Path != "" {
fmt.Printf("Using working directory: %s\n", workspace.Path)
os.Chdir(workspace.Path)
} else {
wd, err = os.Getwd()
if err != nil {
fmt.Printf("Using working directory: %s\n", wd)
} else {
fmt.Printf("WARNING: Error while attempting to get working dir: %s\n", err)
}
}
// execute each of our Rancher Compose commands in sequence
for _, c := range cmds.Commands {
args := CleanSlice(strings.Split(c, " "))
fmt.Printf("Executing Rancher Compose: %s %s\n", path, strings.Join(args[:], " "))
cmd := exec.Command(path, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
// exit if any command fails
if err != nil {
fmt.Printf("Error while executing command: %s\n", err)
os.Exit(1)
}
}
fmt.Printf("Successfully executed commands\n")
}