Skip to content

Commit 1ff48ce

Browse files
committed
improve flag handling
1 parent a885f1e commit 1ff48ce

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

internal/cmd/beta/server/create/create.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,31 +195,51 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
195195
return nil, &cliErr.ProjectIdError{}
196196
}
197197

198-
bootVolumeSourceId, _ := cmd.Flags().GetString(bootVolumeSourceIdFlag)
199-
bootVolumeSourceType, _ := cmd.Flags().GetString(bootVolumeSourceTypeFlag)
200-
bootVolumeSize, _ := cmd.Flags().GetInt64(bootVolumeSizeFlag)
201-
imageId, _ := cmd.Flags().GetString(imageIdFlag)
198+
bootVolumeSourceId := flags.FlagToStringPointer(p, cmd, bootVolumeSourceIdFlag)
199+
bootVolumeSourceType := flags.FlagToStringPointer(p, cmd, bootVolumeSourceTypeFlag)
200+
bootVolumeSize := flags.FlagToInt64Pointer(p, cmd, bootVolumeSizeFlag)
201+
imageId := flags.FlagToStringPointer(p, cmd, imageIdFlag)
202202

203-
if imageId == "" && bootVolumeSourceId == "" && bootVolumeSourceType == "" {
203+
if imageId == nil && bootVolumeSourceId == nil && bootVolumeSourceType == nil {
204204
return nil, &cliErr.ServerCreateMissingFlagsError{
205205
Cmd: cmd,
206206
}
207207
}
208208

209-
if imageId == "" {
209+
if imageId == nil {
210210
err := flags.MarkFlagsRequired(cmd, bootVolumeSourceIdFlag, bootVolumeSourceTypeFlag)
211211
cobra.CheckErr(err)
212212
}
213213

214-
if bootVolumeSourceType == "image" && bootVolumeSize == 0 {
215-
err := cmd.MarkFlagRequired(bootVolumeSizeFlag)
214+
if bootVolumeSourceId != nil && bootVolumeSourceType == nil {
215+
err := cmd.MarkFlagRequired(bootVolumeSourceTypeFlag)
216216
cobra.CheckErr(err)
217-
return nil, &cliErr.ServerCreateError{
217+
218+
return nil, &cliErr.ServerCreateMissingVolumeTypeError{
218219
Cmd: cmd,
219220
}
220221
}
221222

222-
if bootVolumeSourceId == "" && bootVolumeSourceType == "" {
223+
if bootVolumeSourceType != nil {
224+
if bootVolumeSourceId == nil {
225+
err := cmd.MarkFlagRequired(bootVolumeSourceIdFlag)
226+
cobra.CheckErr(err)
227+
228+
return nil, &cliErr.ServerCreateMissingVolumeIdError{
229+
Cmd: cmd,
230+
}
231+
}
232+
233+
if *bootVolumeSourceType == "image" && bootVolumeSize == nil {
234+
err := cmd.MarkFlagRequired(bootVolumeSizeFlag)
235+
cobra.CheckErr(err)
236+
return nil, &cliErr.ServerCreateError{
237+
Cmd: cmd,
238+
}
239+
}
240+
}
241+
242+
if bootVolumeSourceId == nil && bootVolumeSourceType == nil {
223243
err := cmd.MarkFlagRequired(imageIdFlag)
224244
cobra.CheckErr(err)
225245
}

internal/pkg/errors/errors.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,29 @@ To enable it, run:
141141

142142
IAAS_SERVER_MISSING_VOLUME_SIZE = `The "boot-volume-size" flag must be provided when "boot-volume-source-type" is "image".`
143143

144+
IAAS_SERVER_MISSING_VOLUME_ID = `The "boot-volume-source-id" flag must be provided together with "boot-volume-source-type" flag.`
145+
146+
IAAS_SERVER_MISSING_VOLUME_TYPE = `The "boot-volume-source-type" flag must be provided together with "boot-volume-source-id" flag.`
147+
144148
IAAS_SERVER_MISSING_IMAGE_OR_VOLUME_FLAGS = `Either "image-id" or "boot-volume-source-type" and "boot-volume-source-id" flags must be provided.`
145149
)
146150

151+
type ServerCreateMissingVolumeIdError struct {
152+
Cmd *cobra.Command
153+
}
154+
155+
func (e *ServerCreateMissingVolumeIdError) Error() string {
156+
return IAAS_SERVER_MISSING_VOLUME_ID
157+
}
158+
159+
type ServerCreateMissingVolumeTypeError struct {
160+
Cmd *cobra.Command
161+
}
162+
163+
func (e *ServerCreateMissingVolumeTypeError) Error() string {
164+
return IAAS_SERVER_MISSING_VOLUME_TYPE
165+
}
166+
147167
type ServerCreateMissingFlagsError struct {
148168
Cmd *cobra.Command
149169
}

0 commit comments

Comments
 (0)