-
Notifications
You must be signed in to change notification settings - Fork 135
/
playground.go
68 lines (57 loc) · 1.51 KB
/
playground.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
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
cmdtpl "github.com/k14s/ytt/pkg/cmd/template"
"github.com/k14s/ytt/pkg/playground"
"github.com/spf13/cobra"
)
type PlaygroundOptions struct {
ListenAddr string
BinaryPath string
CheckCookie bool
}
func NewPlaygroundOptions() *PlaygroundOptions {
return &PlaygroundOptions{
BinaryPath: os.Args[0],
}
}
func NewPlaygroundCmd(o *PlaygroundOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "playground",
Aliases: []string{"pg"},
Short: "Starts playground HTTP server",
RunE: func(_ *cobra.Command, _ []string) error { return o.Run() },
}
cmd.Flags().StringVar(&o.ListenAddr, "listen-addr", "localhost:8080", "Listen address")
return cmd
}
func (o *PlaygroundOptions) Server() *playground.Server {
opts := playground.ServerOpts{
ListenAddr: o.ListenAddr,
TemplateFunc: o.execBinary,
ErrorFunc: o.bulkOutErr,
CheckCookie: o.CheckCookie,
}
return playground.NewServer(opts)
}
func (o *PlaygroundOptions) Run() error {
return o.Server().Run()
}
func (o *PlaygroundOptions) execBinary(data []byte) ([]byte, error) {
var out, stderr bytes.Buffer
cmd := exec.Command(o.BinaryPath, "tpl", "--bulk-in", string(data), "--bulk-out")
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return nil, fmt.Errorf("error: %s", stderr.String())
}
return out.Bytes(), nil
}
func (*PlaygroundOptions) bulkOutErr(err error) ([]byte, error) {
return json.Marshal(cmdtpl.BulkFiles{Errors: err.Error()})
}