-
Notifications
You must be signed in to change notification settings - Fork 8
/
create.auto.go
176 lines (154 loc) · 4.15 KB
/
create.auto.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
// Code generated from specification version 1.0.0: DO NOT EDIT
package create
import (
"io"
"net/http"
"github.com/MakeNowJust/heredoc/v2"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd/subcommand"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmderrors"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmdutil"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/completion"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/flags"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/mapbuilder"
"github.com/reubenmiller/go-c8y/pkg/c8y"
"github.com/spf13/cobra"
)
// CreateCmd command
type CreateCmd struct {
*subcommand.SubCommand
factory *cmdutil.Factory
}
// NewCreateCmd creates a command to Create tenant option
func NewCreateCmd(f *cmdutil.Factory) *CreateCmd {
ccmd := &CreateCmd{
factory: f,
}
cmd := &cobra.Command{
Use: "create",
Short: "Create tenant option",
Long: ``,
Example: heredoc.Doc(`
$ c8y tenantoptions create --category "c8y_cli_tests" --key "option1" --value "1"
Create a tenant option
`),
PreRunE: func(cmd *cobra.Command, args []string) error {
return f.CreateModeEnabled()
},
RunE: ccmd.RunE,
}
cmd.SilenceUsage = true
cmd.Flags().String("category", "", "Category of option")
cmd.Flags().String("key", "", "Key of option (accepts pipeline)")
cmd.Flags().String("value", "", "Value of option")
completion.WithOptions(
cmd,
completion.WithTenantOptionCategory("category", func() (*c8y.Client, error) { return ccmd.factory.Client() }),
completion.WithTenantOptionKey("key", "category", func() (*c8y.Client, error) { return ccmd.factory.Client() }),
)
flags.WithOptions(
cmd,
flags.WithProcessingMode(),
flags.WithData(),
f.WithTemplateFlag(cmd),
flags.WithExtendedPipelineSupport("key", "key", false, "id"),
)
// Required flags
ccmd.SubCommand = subcommand.NewSubCommand(cmd)
return ccmd
}
// RunE executes the command
func (n *CreateCmd) RunE(cmd *cobra.Command, args []string) error {
cfg, err := n.factory.Config()
if err != nil {
return err
}
// Runtime flag options
flags.WithOptions(
cmd,
flags.WithRuntimePipelineProperty(),
)
client, err := n.factory.Client()
if err != nil {
return err
}
inputIterators, err := cmdutil.NewRequestInputIterators(cmd, cfg)
if err != nil {
return err
}
// query parameters
query := flags.NewQueryTemplate()
err = flags.WithQueryParameters(
cmd,
query,
inputIterators,
flags.WithCustomStringSlice(func() ([]string, error) { return cfg.GetQueryParameters(), nil }, "custom"),
)
if err != nil {
return cmderrors.NewUserError(err)
}
queryValue, err := query.GetQueryUnescape(true)
if err != nil {
return cmderrors.NewSystemError("Invalid query parameter")
}
// headers
headers := http.Header{}
err = flags.WithHeaders(
cmd,
headers,
inputIterators,
flags.WithCustomStringSlice(func() ([]string, error) { return cfg.GetHeader(), nil }, "header"),
flags.WithProcessingModeValue(),
)
if err != nil {
return cmderrors.NewUserError(err)
}
// form data
formData := make(map[string]io.Reader)
err = flags.WithFormDataOptions(
cmd,
formData,
inputIterators,
)
if err != nil {
return cmderrors.NewUserError(err)
}
// body
body := mapbuilder.NewInitializedMapBuilder(true)
err = flags.WithBody(
cmd,
body,
inputIterators,
flags.WithOverrideValue("key", "key"),
flags.WithDataFlagValue(),
flags.WithStringValue("category", "category"),
flags.WithStringValue("key", "key"),
flags.WithStringValue("value", "value"),
cmdutil.WithTemplateValue(n.factory),
flags.WithTemplateVariablesValue(),
flags.WithRequiredProperties("category", "key", "value"),
)
if err != nil {
return cmderrors.NewUserError(err)
}
// path parameters
path := flags.NewStringTemplate("/tenant/options")
err = flags.WithPathParameters(
cmd,
path,
inputIterators,
)
if err != nil {
return err
}
req := c8y.RequestOptions{
Method: "POST",
Path: path.GetTemplate(),
Query: queryValue,
Body: body,
FormData: formData,
Header: headers,
IgnoreAccept: cfg.IgnoreAcceptHeader(),
DryRun: cfg.ShouldUseDryRun(cmd.CommandPath()),
}
return n.factory.RunWithWorkers(client, cmd, &req, inputIterators)
}