forked from liamjbennett/sous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sous_metadata_get.go
96 lines (81 loc) · 2.41 KB
/
sous_metadata_get.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
package cli
import (
"flag"
"fmt"
"io"
"github.com/opentable/sous/config"
"github.com/opentable/sous/graph"
sous "github.com/opentable/sous/lib"
"github.com/opentable/sous/util/cmdr"
"github.com/pkg/errors"
"github.com/samsalisbury/yaml"
)
type SousMetadataGet struct {
config.DeployFilterFlags
*sous.ResolveFilter
*sous.State
graph.CurrentGDM
graph.OutWriter
}
func init() { MetadataSubcommands["get"] = &SousMetadataGet{} }
const sousMetadataGetHelp = `query deployment metadata`
func (*SousMetadataGet) Help() string { return sousMetadataGetHelp }
func (smg *SousMetadataGet) AddFlags(fs *flag.FlagSet) {
MustAddFlags(fs, &smg.DeployFilterFlags, MetadataFilterFlagsHelp)
}
func (smg *SousMetadataGet) RegisterOn(psy Addable) {
psy.Add(&smg.DeployFilterFlags)
}
func (smg *SousMetadataGet) Execute(args []string) cmdr.Result {
if smg.DeployFilterFlags.Repo == "" {
return EnsureErrorResult(errors.Errorf("-repo is required"))
}
filtered := smg.CurrentGDM.Clone().Filter(smg.ResolveFilter.FilterDeployment)
if smg.ResolveFilter.Cluster != "" {
dep, err := filtered.Only()
if err != nil {
return EnsureErrorResult(err)
}
if dep == nil {
return EnsureErrorResult(errors.Errorf("No manifest matched by %v", smg.ResolveFilter))
}
if err := outputMetadata(dep.Metadata, smg.ResolveFilter.Cluster, args, smg.OutWriter); err != nil {
return EnsureErrorResult(err)
}
return cmdr.Success()
}
manis, err := filtered.Manifests(smg.State.Defs)
if err != nil {
return EnsureErrorResult(err)
}
mani, err := manis.Only()
if err != nil {
return EnsureErrorResult(err)
}
if mani == nil {
return EnsureErrorResult(errors.Errorf("No manifest matched by %v", smg.ResolveFilter))
}
for clusterName, deploySpec := range mani.Deployments {
smg.OutWriter.Write([]byte(fmt.Sprintf("Metadata for deployment in %s\n", clusterName)))
if err := outputMetadata(deploySpec.Metadata, clusterName, args, smg.OutWriter); err != nil {
return EnsureErrorResult(err)
}
}
return cmdr.Success()
}
func outputMetadata(metadata sous.Metadata, clusterName string, args []string, out io.Writer) error {
if len(args) == 0 {
yml, err := yaml.Marshal(metadata)
if err != nil {
return EnsureErrorResult(err)
}
out.Write(yml)
return nil
}
value, present := metadata[args[0]]
if !present {
return errors.Errorf("No value for %q in cluster %s", args[0], clusterName)
}
fmt.Fprint(out, value)
return nil
}