/
trait_help.go
258 lines (222 loc) · 6.82 KB
/
trait_help.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
"strings"
"github.com/fatih/structs"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/resources"
"github.com/apache/camel-k/pkg/trait"
"github.com/apache/camel-k/pkg/util/indentedwriter"
)
func newTraitHelpCmd(rootCmdOptions *RootCmdOptions) (*cobra.Command, *traitHelpCommandOptions) {
options := traitHelpCommandOptions{
RootCmdOptions: rootCmdOptions,
}
cmd := cobra.Command{
Use: "trait",
Short: "Trait help information",
Long: `Displays help information for traits in a specified output format.`,
PreRunE: decode(&options),
RunE: func(cmd *cobra.Command, args []string) error {
if err := options.validate(args); err != nil {
return err
}
return options.run(cmd, args)
},
Annotations: map[string]string{
offlineCommandLabel: "true",
},
}
cmd.Flags().Bool("all", false, "Include all traits")
cmd.Flags().StringP("output", "o", "", "Output format. One of json, yaml")
return &cmd, &options
}
type traitHelpCommandOptions struct {
*RootCmdOptions
IncludeAll bool `mapstructure:"all"`
OutputFormat string `mapstructure:"output"`
}
type traitDescription struct {
Name trait.ID `json:"name" yaml:"name"`
Platform bool `json:"platform" yaml:"platform"`
Profiles []string `json:"profiles" yaml:"profiles"`
Properties []traitPropertyDescription `json:"properties" yaml:"properties"`
Description string `json:"description" yaml:"description"`
}
type traitPropertyDescription struct {
Name string `json:"name" yaml:"name"`
TypeName string `json:"type" yaml:"type"`
DefaultValue interface{} `json:"defaultValue,omitempty" yaml:"defaultValue,omitempty"`
Description string `json:"description" yaml:"description"`
}
type traitMetaData struct {
Traits []traitDescription `yaml:"traits"`
}
func (command *traitHelpCommandOptions) validate(args []string) error {
if command.IncludeAll && len(args) > 0 {
return errors.New("invalid combination: both all flag and a named trait is set")
}
if !command.IncludeAll && len(args) == 0 {
return errors.New("invalid combination: neither all flag nor a named trait is set")
}
return nil
}
func (command *traitHelpCommandOptions) run(cmd *cobra.Command, args []string) error {
var traitDescriptions []*traitDescription
catalog := trait.NewCatalog(nil)
traitMetaData := &traitMetaData{}
err := yaml.Unmarshal(resources.Resource("/traits.yaml"), traitMetaData)
if err != nil {
return err
}
for _, tp := range v1.AllTraitProfiles {
traits := catalog.TraitsForProfile(tp)
for _, t := range traits {
if len(args) == 1 && trait.ID(args[0]) != t.ID() {
continue
}
td := findTraitDescription(t.ID(), traitDescriptions)
if td == nil {
td = &traitDescription{
Name: t.ID(),
Platform: t.IsPlatformTrait(),
Profiles: make([]string, 0),
}
var targetTrait *traitDescription
for _, item := range traitMetaData.Traits {
item := item
if item.Name == t.ID() {
targetTrait = &item
td.Description = item.Description
break
}
}
computeTraitProperties(structs.Fields(t), &td.Properties, targetTrait)
traitDescriptions = append(traitDescriptions, td)
}
td.addProfile(string(tp))
}
}
if len(args) == 1 && len(traitDescriptions) == 0 {
return fmt.Errorf("no trait named '%s' exists", args[0])
}
switch strings.ToUpper(command.OutputFormat) {
case "JSON":
res, err := json.Marshal(traitDescriptions)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), string(res))
case "YAML":
res, err := yaml.Marshal(traitDescriptions)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), string(res))
default:
res, err := outputTraits(traitDescriptions)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), res)
}
return nil
}
func (td *traitDescription) addProfile(tp string) {
for _, p := range td.Profiles {
if p == tp {
return
}
}
td.Profiles = append(td.Profiles, tp)
}
func findTraitDescription(id trait.ID, traitDescriptions []*traitDescription) *traitDescription {
for _, td := range traitDescriptions {
if td.Name == id {
return td
}
}
return nil
}
func computeTraitProperties(fields []*structs.Field, properties *[]traitPropertyDescription, targetTrait *traitDescription) {
for _, f := range fields {
if f.IsEmbedded() && f.IsExported() && f.Kind() == reflect.Struct {
computeTraitProperties(f.Fields(), properties, targetTrait)
}
if !f.IsExported() || f.IsEmbedded() {
continue
}
property := f.Tag("property")
if property == "" {
continue
}
tp := traitPropertyDescription{}
tp.Name = property
switch f.Kind() {
case reflect.Ptr:
tp.TypeName = reflect.TypeOf(f.Value()).Elem().String()
case reflect.Slice:
tp.TypeName = fmt.Sprintf("slice:%s", reflect.TypeOf(f.Value()).Elem().String())
default:
tp.TypeName = f.Kind().String()
}
if f.IsZero() {
if tp.TypeName == "bool" {
tp.DefaultValue = false
} else {
tp.DefaultValue = nil
}
} else {
tp.DefaultValue = f.Value()
}
// apply the description from metadata
if targetTrait != nil {
for _, item := range targetTrait.Properties {
if item.Name == tp.Name {
tp.Description = item.Description
}
}
}
*properties = append(*properties, tp)
}
}
func outputTraits(descriptions []*traitDescription) (string, error) {
return indentedwriter.IndentedString(func(out io.Writer) error {
w := indentedwriter.NewWriter(out)
for _, td := range descriptions {
w.Writef(0, "Name:\t%s\n", td.Name)
w.Writef(0, "Profiles:\t%s\n", strings.Join(td.Profiles, ","))
w.Writef(0, "Platform:\t%t\n", td.Platform)
w.Writef(0, "Properties:\n")
for _, p := range td.Properties {
w.Writef(1, "%s:\n", p.Name)
w.Writef(2, "Type:\t%s\n", p.TypeName)
if p.DefaultValue != nil {
w.Writef(2, "Default Value:\t%v\n", p.DefaultValue)
}
}
w.Writelnf(0, "")
}
return nil
})
}