From 0560a230c2ff420fdd91b792983e5fda0ce82fa9 Mon Sep 17 00:00:00 2001 From: hirenko-v <132065511+hirenko-v@users.noreply.github.com> Date: Fri, 20 Sep 2024 16:10:32 +0300 Subject: [PATCH] test --- cmd/msg/main.go | 172 +++++++++++++++++++++++------------------------- 1 file changed, 81 insertions(+), 91 deletions(-) diff --git a/cmd/msg/main.go b/cmd/msg/main.go index dd77754..18877b0 100644 --- a/cmd/msg/main.go +++ b/cmd/msg/main.go @@ -7,7 +7,6 @@ import ( "strings" "log" "os/exec" - "encoding/json" "github.com/hashicorp/go-plugin" "github.com/kubeshop/botkube/pkg/api" @@ -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) + 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, + }, + }, + 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, + }, + } }