Skip to content

Commit 100e37a

Browse files
committed
make flags conditionally required
1 parent 940712f commit 100e37a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,36 @@ func NewCmd(p *print.Printer) *cobra.Command {
8585
`$ stackit beta server create --machine-type t1.1 --name server1 --boot-volume-source-id xxx --boot-volume-source-type image --boot-volume-size 64`,
8686
),
8787
),
88+
PreRun: func(cmd *cobra.Command, _ []string) {
89+
bootVolumeSourceId, _ := cmd.Flags().GetString(bootVolumeSourceIdFlag)
90+
bootVolumeSourceType, _ := cmd.Flags().GetString(bootVolumeSourceTypeFlag)
91+
bootVolumeSize, _ := cmd.Flags().GetInt64(bootVolumeSizeFlag)
92+
imageId, _ := cmd.Flags().GetString(imageIdFlag)
93+
94+
if imageId == "" && bootVolumeSourceId == "" && bootVolumeSourceType == "" {
95+
p.Warn("Either Image ID or boot volume flags must be provided.\n")
96+
}
97+
98+
if imageId == "" {
99+
err := flags.MarkFlagsRequired(cmd, bootVolumeSourceIdFlag, bootVolumeSourceTypeFlag)
100+
cobra.CheckErr(err)
101+
}
102+
103+
if bootVolumeSourceType == "image" {
104+
if bootVolumeSize == 0 {
105+
p.Warn("Boot volume size must be provided when `source_type` is `image`.\n")
106+
}
107+
cmd.MarkFlagRequired(bootVolumeSizeFlag)
108+
}
109+
110+
if bootVolumeSourceId == "" && bootVolumeSourceType == "" {
111+
cmd.MarkFlagRequired(imageIdFlag)
112+
}
113+
114+
if imageId != "" && (bootVolumeSourceId != "" || bootVolumeSourceType != "") {
115+
p.Warn("Image ID flag cannot be used together with any of the boot volume flags.\n")
116+
}
117+
},
88118
RunE: func(cmd *cobra.Command, _ []string) error {
89119
ctx := context.Background()
90120
model, err := parseInput(p, cmd)

0 commit comments

Comments
 (0)