Skip to content

Commit d36d951

Browse files
authored
Refactor database input flavor error (#64)
1 parent c73361f commit d36d951

File tree

5 files changed

+22
-16
lines changed

5 files changed

+22
-16
lines changed

internal/cmd/mongodbflex/instance/create/create.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,12 @@ func parseInput(cmd *cobra.Command) (*inputModel, error) {
174174

175175
if flavorId == nil && (cpu == nil || ram == nil) {
176176
return nil, &cliErr.DatabaseInputFlavorError{
177-
Service: "mongodbflex",
178-
Operation: cmd.Use,
177+
Cmd: cmd,
179178
}
180179
}
181180
if flavorId != nil && (cpu != nil || ram != nil) {
182181
return nil, &cliErr.DatabaseInputFlavorError{
183-
Service: "mongodbflex",
184-
Operation: cmd.Use,
182+
Cmd: cmd,
185183
}
186184
}
187185

internal/cmd/mongodbflex/instance/update/update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
156156

157157
if flavorId != nil && (cpu != nil || ram != nil) {
158158
return nil, &cliErr.DatabaseInputFlavorError{
159-
Service: "mongodbflex",
160-
Operation: cmd.Use,
159+
Cmd: cmd,
160+
Args: inputArgs,
161161
}
162162
}
163163

internal/cmd/postgresflex/instance/create/create.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,12 @@ func parseInput(cmd *cobra.Command) (*inputModel, error) {
174174

175175
if flavorId == nil && (cpu == nil || ram == nil) {
176176
return nil, &cliErr.DatabaseInputFlavorError{
177-
Service: "postgresflex",
178-
Operation: cmd.Use,
177+
Cmd: cmd,
179178
}
180179
}
181180
if flavorId != nil && (cpu != nil || ram != nil) {
182181
return nil, &cliErr.DatabaseInputFlavorError{
183-
Service: "postgresflex",
184-
Operation: cmd.Use,
182+
Cmd: cmd,
185183
}
186184
}
187185

internal/cmd/postgresflex/instance/update/update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
156156

157157
if flavorId != nil && (cpu != nil || ram != nil) {
158158
return nil, &cliErr.DatabaseInputFlavorError{
159-
Service: "postgresflex",
160-
Operation: cmd.Use,
159+
Cmd: cmd,
160+
Args: inputArgs,
161161
}
162162
}
163163

internal/pkg/errors/errors.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ For more details on the available plans, run:
5757
DATABASE_INVALID_INPUT_FLAVOR = `the instance flavor was not correctly provided.
5858
5959
Either provide flavor ID by:
60-
$ stackit %[1]s instance %[2]s --project-id xxx --flavor-id <FLAVOR ID> [flags]
60+
$ %[1]s --flavor-id <FLAVOR ID> [flags]
6161
6262
or provide CPU and RAM:
63-
$ stackit %[1]s instance %[2]s --project-id xxx --cpu <CPU> --ram <RAM> [flags]
63+
$ %[1]s --cpu <CPU> --ram <RAM> [flags]
6464
6565
For more details on the available flavors, run:
66-
$ stackit %[1]s options --flavors`
66+
$ stackit %[2]s options --flavors`
6767

6868
DATABASE_INVALID_FLAVOR = `the provided instance flavor is not valid.
6969
@@ -131,6 +131,7 @@ func (e *DSAInputPlanError) Error() string {
131131
if len(e.Args) > 0 {
132132
fullCommandPath = fmt.Sprintf("%s %s", fullCommandPath, strings.Join(e.Args, " "))
133133
}
134+
// Assumes a structure of the form "stackit <service> <resource> <operation>"
134135
service := e.Cmd.Parent().Parent().Use
135136

136137
return fmt.Sprintf(DSA_INVALID_INPUT_PLAN, fullCommandPath, service)
@@ -148,10 +149,19 @@ func (e *DSAInvalidPlanError) Error() string {
148149
type DatabaseInputFlavorError struct {
149150
Service string
150151
Operation string
152+
Cmd *cobra.Command
153+
Args []string
151154
}
152155

153156
func (e *DatabaseInputFlavorError) Error() string {
154-
return fmt.Sprintf(DATABASE_INVALID_INPUT_FLAVOR, e.Service, e.Operation)
157+
fullCommandPath := e.Cmd.CommandPath()
158+
if len(e.Args) > 0 {
159+
fullCommandPath = fmt.Sprintf("%s %s", fullCommandPath, strings.Join(e.Args, " "))
160+
}
161+
// Assumes a structure of the form "stackit <service> <resource> <operation>"
162+
service := e.Cmd.Parent().Parent().Use
163+
164+
return fmt.Sprintf(DATABASE_INVALID_INPUT_FLAVOR, fullCommandPath, service)
155165
}
156166

157167
type DatabaseInvalidFlavorError struct {

0 commit comments

Comments
 (0)