This repository was archived by the owner on Jun 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcmd_get.go
More file actions
121 lines (110 loc) · 2.78 KB
/
Copy pathcmd_get.go
File metadata and controls
121 lines (110 loc) · 2.78 KB
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
package main
import (
"bytes"
"fmt"
"strconv"
"strings"
"github.com/spf13/pflag"
"gopkg.in/yaml.v3"
)
var (
flagsGet *pflag.FlagSet
)
func init() {
flagsGet = pflag.NewFlagSet("get", pflag.ContinueOnError)
RegisterCommand(&Command{
Name: "get",
Help: "Get the state of a particular object identified by type and id.",
Flags: flagsGet,
Func: cmdGet,
})
}
var (
ErrGetUsage = ObserveError{Msg: "usage: observe get <object type> <object id>"}
ErrCannotGet = ObserveError{Msg: "cannot get this object type"}
)
func cmdGet(fa FuncArgs) error {
if len(fa.args) != 3 {
return ErrGetUsage
}
otyp := GetObjectType(fa.args[1])
if otyp == nil {
return ErrUnknownObjectType
}
if !otyp.CanGet() {
return ErrCannotGet
}
obj, err := otyp.Get(fa.cfg, fa.op, fa.hc, fa.args[2])
if err != nil {
return NewObserveError(err, "get object type:%s", otyp.TypeName())
}
return obj.PrintToYaml(fa.op, otyp, obj)
}
// printToYamlFromJson converts `object` to `yaml`, sorting the top level fields in a deterministic way.
func printToYamlFromJson(op Output, otyp ObjectType, obj object) error {
buf := bytes.NewBuffer(nil)
enc := yaml.NewEncoder(buf)
// TODO(OB-20759): make a better way to define ordering by preserving the original order returned by the api.
parts := []string{"type", "id", "config", "state", "meta"}
for _, key := range parts {
val, ok := obj[key]
if !ok {
continue
}
if err := enc.Encode(map[string]any{key: val}); err != nil {
return err
}
}
str := buf.String()
strs := strings.Split(str, "\n---\n")
str = strings.Join(strs, "\n")
fmt.Fprint(op, str)
return nil
}
// Note: we probably should use yaml/v3, or some more introspection based
// discovery.
func printToYamlFromObjectInstance(op Output, otyp ObjectType, obj ObjectInstance) error {
fmt.Fprintf(op, "object:\n")
fmt.Fprintf(op, " type: %q\n", otyp.TypeName())
vals := obj.GetValues()
props := otyp.GetProperties()
hasConfig := calcHasConfig(props)
hasState := calcHasState(props)
for _, v := range vals {
p := v.GetDesc()
if p.IsId {
vstr, e := p.Type.ToString(v.GetValue())
if e != nil {
vstr = strconv.Quote(e.Error())
}
fmt.Fprintf(op, " %s: %s\n", p.Name, vstr)
}
}
if hasConfig {
fmt.Fprintf(op, " config:\n")
for _, v := range vals {
p := v.GetDesc()
if !p.IsId && !p.IsComputed {
vstr, e := p.Type.ToString(v.GetValue())
if e != nil {
vstr = strconv.Quote(e.Error())
}
fmt.Fprintf(op, " %s: %s\n", p.Name, vstr)
}
}
}
if hasState {
fmt.Fprintf(op, " state:\n")
for _, v := range vals {
p := v.GetDesc()
if !p.IsId && p.IsComputed {
vstr, e := p.Type.ToString(v.GetValue())
if e != nil {
vstr = strconv.Quote(e.Error())
}
fmt.Fprintf(op, " %s: %s\n", p.Name, vstr)
}
}
}
return nil
}