Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
hirenko-v authored Sep 20, 2024
1 parent 19bcfe2 commit 0560a23
Showing 1 changed file with 81 additions and 91 deletions.
172 changes: 81 additions & 91 deletions cmd/msg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"
"log"
"os/exec"
"encoding/json"

"github.com/hashicorp/go-plugin"
"github.com/kubeshop/botkube/pkg/api"
Expand Down Expand Up @@ -187,96 +186,87 @@ func initialMessages() executor.ExecuteOutput {

// showBothSelects displays the second dropdown after the first one is selected and adds a "Run command" button if both selections are made.
func showBothSelects(firstSelection, secondSelection string) executor.ExecuteOutput {
fileList, err := getFileOptions()
if err != nil {
log.Fatalf("Error retrieving file options: %v", err)
}
btnBuilder := api.NewMessageButtonBuilder()
cmdPrefix := func(cmd string) string {
return fmt.Sprintf("%s %s %s", api.MessageBotNamePlaceholder, pluginName, cmd)
}

sections := []api.Section{
{
Selects: api.Selects{
ID: "select-id",
Items: []api.Select{
{
Name: "first",
Command: cmdPrefix("select_first"),
OptionGroups: []api.OptionGroup{
{
Name: "Job Name",
Options: fileList,
},
},
InitialOption: &api.OptionItem{
Name: firstSelection,
Value: firstSelection,
},
},
},
},
},
}

// Show second dropdowns based on the script output if the first selection is made
if firstSelection != "" {
// Run the script to get dynamic options
scriptOutput, err := runScript(firstSelection)
if err != nil {
log.Fatalf("Error running script: %v", err)
}

// Create multiple dropdowns based on the options in the script output
for _, option := range scriptOutput.Options {
var dropdownOptions []api.OptionItem
for _, value := range option.Values {
dropdownOptions = append(dropdownOptions, api.OptionItem{
Name: value,
Value: fmt.Sprintf("%s %s", option.Flags[0], value), // Using the first flag for the command
})
}

// Add each dynamic dropdown to the section
sections[0].Selects.Items = append(sections[0].Selects.Items, api.Select{
Name: option.Description, // Use the option description as the dropdown name
Command: cmdPrefix("select_second"),
OptionGroups: []api.OptionGroup{
{
Name: option.Description, // Use the option description as the group name
Options: dropdownOptions, // Use the dynamically created options
},
},
})
}
}

// Only add the "Run command" button if both selections are made
if firstSelection != "" && secondSelection != "" {
code := fmt.Sprintf("run %s %s", firstSelection, secondSelection)
sections = append(sections, api.Section{
Base: api.Base{
Body: api.Body{
CodeBlock: code,
},
},
Buttons: []api.Button{
btnBuilder.ForCommandWithoutDesc("Run command", code, api.ButtonStylePrimary),
},
})
}

return executor.ExecuteOutput{
Message: api.Message{
BaseBody: api.Body{
Plaintext: "You've selected from the first dropdown. Now select from the second group of dropdowns.",
},
Sections: sections,
OnlyVisibleForYou: true,
ReplaceOriginal: true,
},
}
fileList, err := getFileOptions()
if err != nil {
log.Fatalf("Error retrieving file options: %v", err)
}
btnBuilder := api.NewMessageButtonBuilder()
cmdPrefix := func(cmd string) string {
return fmt.Sprintf("%s %s %s", api.MessageBotNamePlaceholder, pluginName, cmd)
}

sections := []api.Section{
{
Selects: api.Selects{
ID: "select-id",
Items: []api.Select{
{
Name: "first",
Command: cmdPrefix("select_first"),
OptionGroups: []api.OptionGroup{
{
Name: "Job Name",
Options: fileList,
},
},
InitialOption: &api.OptionItem{
Name: firstSelection,
Value: firstSelection,
},
},
},
},
},
}

// Show second dropdown if the first selection is made
if firstSelection != "" {
// Run the script to get dynamic options
scriptOutput, err := runScript(firstSelection)

Check failure on line 225 in cmd/msg/main.go

View workflow job for this annotation

GitHub Actions / Release

scriptOutput declared and not used
if err != nil {
log.Fatalf("Error running script: %v", err)
}

sections[0].Selects.Items = append(sections[0].Selects.Items, api.Select{
Name: "second",
Command: cmdPrefix("select_second"),
OptionGroups: []api.OptionGroup{
{
Name: "Second Group",
Options: []api.OptionItem{
{Name: "true", Value: "-i true"},
{Name: "false", Value: "-i false"},
},
},
},
})
}

// Only add the button if both selections are made
if firstSelection != "" && secondSelection != "" {
code := fmt.Sprintf("run %s %s", firstSelection, secondSelection)
sections = append(sections, api.Section{
Base: api.Base{
Body: api.Body{
CodeBlock: scriptOutput.Options,

Check failure on line 251 in cmd/msg/main.go

View workflow job for this annotation

GitHub Actions / Release

undefined: scriptOutput
},
},
Buttons: []api.Button{
btnBuilder.ForCommandWithoutDesc("Run command", code, api.ButtonStylePrimary),
},
})
}

return executor.ExecuteOutput{
Message: api.Message{
BaseBody: api.Body{
Plaintext: "You've selected from the first dropdown. Now select from the second dropdown.",
},
Sections: sections,
OnlyVisibleForYou: true,
ReplaceOriginal: true,
},
}
}


Expand Down

0 comments on commit 0560a23

Please sign in to comment.