-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.go
146 lines (127 loc) · 3.61 KB
/
test.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
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
// ExternalPlugin represents an external program with name and path
type ExternalPlugin struct {
Name string
Version string
Path string
DirContext string
}
// PluginRequest contains all information kubebuilder received from the CLI
// and plugins executed before it.
type PluginRequest struct {
Command string `json:"command"`
Args []string `json:"args"`
Universe map[string]string `json:"universe"`
}
// PluginResponse is returned to kubebuilder by the plugin and contains all files
// written by the plugin following a certain command.
type PluginResponse struct {
Command string `json:"command"`
Universe map[string]string `json:"universe"`
Error bool `json:"error,omitempty"`
ErrorMsg string `json:"error_msg,omitempty"`
}
// runExternalProgram invokes an external program with the provided program path and
// sends/receives messages from stdin/stdout/stderr
func (p *ExternalPlugin) runExternalProgram(req PluginRequest) (res PluginResponse, err error) {
b, err := json.Marshal(req)
if err != nil {
return res, err
}
// Invoke the command specified by p in the context dir.
cmd := exec.Command(p.Path)
cmd.Dir = p.DirContext
cmd.Stdin = bytes.NewBuffer(b)
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
fmt.Fprint(os.Stdout, string(out))
return res, err
}
if json.Unmarshal(out, &res); err != nil {
return res, err
}
return res, nil
}
func main() {
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
// Create the test plugin context.
p := &ExternalPlugin{
Name: "pythonplugin.my.domain",
Version: "v1-alpha",
DirContext: "testproject",
Path: filepath.Join(wd, "pythonplugin.sh"),
}
var args []string
if len(os.Args) == 1 {
// Some test args.
args = []string{"init", "--domain", "example.com"}
} else {
args = make([]string, len(os.Args)-1)
copy(args, os.Args[1:])
}
fmt.Fprintln(os.Stdout, "Running:", p.Path, strings.Join(args, " "))
// Plugin request setup.
// Much of this will actually be done by the cli library when implemented in kubebuilder.
req := PluginRequest{}
maybeCmdArgs := args[:2]
if args[0] == "init" {
if _, err := os.Stat(p.DirContext); err == nil {
log.Fatalf("project directory %q must not exist", p.DirContext)
}
if err := os.MkdirAll(p.DirContext, 0755); err != nil {
log.Fatalln("failed to create project dir:", err)
}
req.Command = args[0]
req.Args = args[1:]
} else {
if info, err := os.Stat(p.DirContext); err != nil || !info.IsDir() {
log.Fatalf("project directory %q has error: %v", p.DirContext, err)
}
if args[0] == "create" {
req.Command = strings.Join(args[:2], " ")
req.Args = args[2:]
}
}
if req.Command == "" {
log.Fatalf("unknown command: %q", maybeCmdArgs[0])
}
req.Universe = map[string]string{}
// Run the plugin.
res, err := p.runExternalProgram(req)
if err != nil {
log.Fatalln("unable to run external program:", err)
}
// Error if the plugin failed.
if res.Error {
log.Fatal(res.ErrorMsg)
}
output, err := json.MarshalIndent(res, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Fprint(os.Stdout, "Universe:\n", string(output))
// Write all files in the returned universe.
for subpath, data := range res.Universe {
if err := os.MkdirAll(filepath.Dir(subpath), 0755); err != nil {
log.Fatal(err)
}
p := filepath.Join(p.DirContext, subpath)
if err := os.WriteFile(p, []byte(data), 0644); err != nil {
log.Fatalln("failed to write project file:", err)
}
}
}