Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(commands): do not transfrom string answer to integer #135

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions commands/siem.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"net/http"
"strconv"
"strings"

cst "github.com/DelineaXPM/dsv-cli/constants"
Expand Down Expand Up @@ -255,11 +254,6 @@ func promptSiemData(vcli vaultcli.CLI) (*siemUpdateRequest, error) {
Name: "Port",
Prompt: &survey.Input{Message: "Port:"},
Validate: vaultcli.SurveyRequiredPortNumber,
Transform: func(ans interface{}) (newAns interface{}) {
answer := strings.TrimSpace(ans.(string))
val, _ := strconv.Atoi(answer)
return val
},
}
questionEndpoint := &survey.Question{
Name: "Endpoint",
Expand Down
26 changes: 13 additions & 13 deletions vaultcli/survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (

// SurveyRequired verifies that there is some answer.
// Built-in function "survey.Required()" does not trim spaces.
func SurveyRequired(ans interface{}) error {
func SurveyRequired(ans any) error {
answer := strings.TrimSpace(ans.(string))
if len(answer) == 0 {
return errValueRequired
Expand All @@ -38,7 +38,7 @@ func SurveyRequired(ans interface{}) error {
}

// SurveyRequiredInt verifies that the answer is a valid integer number.
func SurveyRequiredInt(ans interface{}) error {
func SurveyRequiredInt(ans any) error {
answer := strings.TrimSpace(ans.(string))
_, err := strconv.Atoi(answer)
if err != nil {
Expand All @@ -48,7 +48,7 @@ func SurveyRequiredInt(ans interface{}) error {
}

// SurveyRequiredPortNumber verifies that the answer is a valid port number.
func SurveyRequiredPortNumber(ans interface{}) error {
func SurveyRequiredPortNumber(ans any) error {
answer := strings.TrimSpace(ans.(string))
num, err := strconv.Atoi(answer)
if err != nil || num > 65535 || num < 0 {
Expand All @@ -58,7 +58,7 @@ func SurveyRequiredPortNumber(ans interface{}) error {
}

// SurveyRequiredFile verifies that the answer is a valid path to a file.
func SurveyRequiredFile(ans interface{}) error {
func SurveyRequiredFile(ans any) error {
answer := strings.TrimSpace(ans.(string))
if len(answer) == 0 {
return errValueRequired
Expand All @@ -71,7 +71,7 @@ func SurveyRequiredFile(ans interface{}) error {
}

// SurveyRequiredPath checks path.
func SurveyRequiredPath(ans interface{}) error {
func SurveyRequiredPath(ans any) error {
answer := strings.TrimSpace(ans.(string))
if len(answer) == 0 {
return errValueRequired
Expand All @@ -80,7 +80,7 @@ func SurveyRequiredPath(ans interface{}) error {
}

// SurveyRequiredName checks name.
func SurveyRequiredName(ans interface{}) error {
func SurveyRequiredName(ans any) error {
answer := strings.TrimSpace(ans.(string))
if len(answer) == 0 {
return errValueRequired
Expand All @@ -89,8 +89,8 @@ func SurveyRequiredName(ans interface{}) error {
}

// SurveyRequiredProfileName checks profile name.
func SurveyRequiredProfileName(existingProfiles []string) func(ans interface{}) error {
return func(ans interface{}) error {
func SurveyRequiredProfileName(existingProfiles []string) func(ans any) error {
return func(ans any) error {
answer := strings.TrimSpace(ans.(string))
if len(answer) == 0 {
return errValueRequired
Expand All @@ -113,7 +113,7 @@ func SurveyRequiredProfileName(existingProfiles []string) func(ans interface{})
}

// SurveySelectAtLeastOne requires the answer is a list with at least one item.
func SurveySelectAtLeastOne(ans interface{}) error {
func SurveySelectAtLeastOne(ans any) error {
list, ok := ans.([]core.OptionAnswer)
if !ok {
return fmt.Errorf("unexpected type %T", ans)
Expand All @@ -125,7 +125,7 @@ func SurveySelectAtLeastOne(ans interface{}) error {
}

// SurveyOptionalCIDR verifies that the answer is either empty or a valid CIDR.
func SurveyOptionalCIDR(ans interface{}) error {
func SurveyOptionalCIDR(ans any) error {
answer := strings.TrimSpace(ans.(string))
if len(answer) == 0 {
// Answer is optional.
Expand All @@ -138,13 +138,13 @@ func SurveyOptionalCIDR(ans interface{}) error {
}

// SurveyOptionalJSON verifies that the answer is either empty or a valid JSON.
func SurveyOptionalJSON(ans interface{}) error {
func SurveyOptionalJSON(ans any) error {
answer := strings.TrimSpace(ans.(string))
if len(answer) == 0 {
// Answer is optional.
return nil
}
m := map[string]interface{}{}
m := map[string]any{}
err := json.Unmarshal([]byte(answer), &m)
if err != nil {
return fmt.Errorf("Invalid JSON: %v", err)
Expand All @@ -153,6 +153,6 @@ func SurveyOptionalJSON(ans interface{}) error {
}

// SurveyTrimSpace trims spaces.
func SurveyTrimSpace(ans interface{}) (newAns interface{}) {
func SurveyTrimSpace(ans any) (newAns any) {
return strings.TrimSpace(ans.(string))
}
Loading