-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
69 lines (58 loc) · 1.25 KB
/
info.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
package operator
import (
"encoding/json"
"io"
"github.com/ghodss/yaml"
"github.com/spf13/cobra"
)
type Info interface {
Name() string
Data() *InfoData
TextSummary(w io.Writer) error
}
type InfoData struct {
OpName string `json:"name"`
Command *Command `json:"command"`
}
func (id *InfoData) Copy() *InfoData {
cp := *id
cp.Command = id.Command
return &cp
}
func (id *InfoData) Data() *InfoData { return id }
func (id *InfoData) Name() string { return id.OpName }
func (id *InfoData) TextSummary(w io.Writer) error {
_, err := w.Write([]byte(id.Command.UsageString()))
return err
}
func (id *InfoData) Encode(w io.Writer) error {
targetb, err := json.Marshal(id.Command.Target)
if err != nil {
return err
}
pe := &PlanEntry{
Name: id.Name(),
Args: targetb,
}
b, err := yaml.Marshal(pe)
if err != nil {
return err
}
if _, err := w.Write(b); err != nil {
return err
}
if _, err := w.Write([]byte("---\n")); err != nil {
return err
}
return nil
}
type CommandArgFunc func(cmd *cobra.Command, args []string, target interface{}) error
type Command struct {
*cobra.Command
ApplyArgs CommandArgFunc
Target interface{}
}
type PlanEntry struct {
Name string `json:"name"`
Args json.RawMessage `json:"args,omitempty"`
}