-
Notifications
You must be signed in to change notification settings - Fork 7
/
create.auto.go
179 lines (156 loc) · 4.4 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
177
178
179
// 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 firmware package
func NewCreateCmd(f *cmdutil.Factory) *CreateCmd {
ccmd := &CreateCmd{
factory: f,
}
cmd := &cobra.Command{
Use: "create",
Short: "Create firmware package",
Long: `Create a new firmware package (managedObject)`,
Example: heredoc.Doc(`
$ c8y firmware create --name "iot-linux" --description "Linux image for IoT devices"
Create a firmware package
$ echo -e "c8y_Linux\nc8y_MacOS" | c8y firmware create --name "iot-linux" --description "Linux image for IoT devices"
Create the same firmware package for multiple device types
`),
PreRunE: func(cmd *cobra.Command, args []string) error {
return f.CreateModeEnabled()
},
RunE: ccmd.RunE,
}
cmd.SilenceUsage = true
cmd.Flags().String("name", "", "name")
cmd.Flags().String("description", "", "Description of the firmware package")
cmd.Flags().String("deviceType", "", "Device type filter. Only allow firmware to be applied to devices of this type (accepts pipeline)")
completion.WithOptions(
cmd,
)
flags.WithOptions(
cmd,
flags.WithProcessingMode(),
flags.WithData(),
f.WithTemplateFlag(cmd),
flags.WithExtendedPipelineSupport("deviceType", "c8y_Filter.type", false, "c8y_Filter.type", "deviceType", "type"),
)
// 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("deviceType", "c8y_Filter.type"),
flags.WithDataFlagValue(),
flags.WithStringValue("name", "name"),
flags.WithStringValue("description", "description"),
flags.WithStringValue("deviceType", "c8y_Filter.type"),
flags.WithDefaultTemplateString(`
{type: 'c8y_Firmware', c8y_Global:{}}`),
cmdutil.WithTemplateValue(n.factory),
flags.WithTemplateVariablesValue(),
flags.WithRequiredProperties("type", "name"),
)
if err != nil {
return cmderrors.NewUserError(err)
}
// path parameters
path := flags.NewStringTemplate("inventory/managedObjects")
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)
}