Skip to content

Commit 40b323f

Browse files
get-probes:add non-interactivte flag support
Signed-off-by: Shivam Purohit <[email protected]>
1 parent 22f1d1d commit 40b323f

File tree

2 files changed

+52
-38
lines changed

2 files changed

+52
-38
lines changed

pkg/apis/probe/probe.go

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ func ListProbeRequest(pid string, probetypes []*models.ProbeType, cred types.Cre
2020
gqlReq.Variables.Filter = models.ProbeFilterInput{
2121
Type: probetypes,
2222
}
23-
2423
query, err := json.Marshal(gqlReq)
2524
if err != nil {
2625
return ListProbeResponse{}, errors.New("Error in listing probes" + err.Error())

pkg/cmd/get/probe.go

+52-37
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"os"
2020
"strconv"
21+
"strings"
2122
"text/tabwriter"
2223
"time"
2324

@@ -50,47 +51,61 @@ var probesCmd = &cobra.Command{
5051
}
5152
}
5253

53-
prompt := promptui.Select{
54-
Label: "Do you want to enable advance filter probes?",
55-
Items: []string{"Yes", "No"},
56-
}
57-
_, option, err := prompt.Run()
58-
if err != nil {
59-
fmt.Printf("Prompt failed %v\n", err)
60-
return
61-
}
62-
fmt.Printf("You chose %q\n", option)
54+
interactiveMode, err := cmd.Flags().GetBool("non-interactive")
55+
6356
var selectedItems []*models.ProbeType
64-
if option == "Yes" {
65-
items := []models.ProbeType{"httpProbe", "cmdProbe", "promProbe", "k8sProbe", "done"}
66-
for {
67-
prompt := promptui.Select{
68-
Label: "Select ProbeType",
69-
Items: items,
70-
Templates: &promptui.SelectTemplates{
71-
Active: `▸ {{ . | cyan }}`,
72-
Inactive: ` {{ . | white }}`,
73-
Selected: `{{ "✔" | green }} {{ . | bold }}`,
74-
},
75-
}
7657

77-
selectedIndex, result, err := prompt.Run()
78-
if err != nil {
79-
fmt.Printf("Prompt failed %v\n", err)
80-
os.Exit(1)
81-
}
58+
if interactiveMode == true {
59+
prompt := promptui.Select{
60+
Label: "Do you want to enable advance filter probes?",
61+
Items: []string{"Yes", "No"},
62+
}
63+
_, option, err := prompt.Run()
64+
if err != nil {
65+
fmt.Printf("Prompt failed %v\n", err)
66+
return
67+
}
68+
fmt.Printf("You chose %q\n", option)
69+
70+
if option == "Yes" {
71+
items := []models.ProbeType{"httpProbe", "cmdProbe", "promProbe", "k8sProbe", "done"}
72+
for {
73+
prompt := promptui.Select{
74+
Label: "Select ProbeType",
75+
Items: items,
76+
Templates: &promptui.SelectTemplates{
77+
Active: `▸ {{ . | cyan }}`,
78+
Inactive: ` {{ . | white }}`,
79+
Selected: `{{ "✔" | green }} {{ . | bold }}`,
80+
},
81+
}
82+
83+
selectedIndex, result, err := prompt.Run()
84+
if err != nil {
85+
fmt.Printf("Prompt failed %v\n", err)
86+
os.Exit(1)
87+
}
88+
89+
if items[selectedIndex] == "done" {
90+
break
91+
}
92+
93+
final := models.ProbeType(result)
94+
selectedItems = append(selectedItems, &final)
95+
items = append(items[:selectedIndex], items[selectedIndex+1:]...)
8296

83-
if items[selectedIndex] == "done" {
84-
break
8597
}
8698

87-
final := models.ProbeType(result)
88-
selectedItems = append(selectedItems, &final)
89-
items = append(items[:selectedIndex], items[selectedIndex+1:]...)
90-
99+
fmt.Printf("Selected Probe Types: %v\n", selectedItems)
100+
}
101+
} else {
102+
var probeTypes string
103+
probeTypes, err = cmd.Flags().GetString("probe-types")
104+
values := strings.Split(probeTypes, ",")
105+
for _, value := range values {
106+
probeType := models.ProbeType(value)
107+
selectedItems = append(selectedItems, &probeType)
91108
}
92-
93-
fmt.Printf("Selected Probe Types: %v\n", selectedItems)
94109
}
95110

96111
probes_get, _ := apis.ListProbeRequest(projectID, selectedItems, credentials)
@@ -129,7 +144,7 @@ var probesCmd = &cobra.Command{
129144

130145
// Check if it's the last item or if user wants to see more
131146
paginationPrompt := promptui.Prompt{
132-
Label: "Press Enter to show more environments (or type 'q' to quit)",
147+
Label: "Press Enter to show more probes (or type 'q' to quit)",
133148
AllowEdit: true,
134149
Default: "",
135150
}
@@ -150,6 +165,6 @@ func init() {
150165
GetCmd.AddCommand(probesCmd)
151166

152167
probesCmd.Flags().String("project-id", "", "Set the project-id to get Probe from a particular project.")
153-
probesCmd.Flags().String("non-intereactive-mode", "", "Set the non-intereactive-mode to true to false to pass probetype value as a flag")
168+
probesCmd.Flags().BoolP("non-interactive", "n", false, "Set it to true for non interactive mode | Note: Always set the boolean flag as --non-interactive=Boolean")
154169
probesCmd.Flags().String("probe-types", "", "Set the probe-types as comma separated values to filter the probes")
155170
}

0 commit comments

Comments
 (0)