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 8fcf2e7 commit 08b200b
Showing 1 changed file with 56 additions and 21 deletions.
77 changes: 56 additions & 21 deletions cmd/msg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (MsgExecutor) Metadata(context.Context) (api.MetadataOutput, error) {
}, nil
}

// Execute returns a given command as a response.
// Execute returns a given command as a response.
func (e *MsgExecutor) Execute(_ context.Context, in executor.ExecuteInput) (executor.ExecuteOutput, error) {
if !in.Context.IsInteractivitySupported {
Expand All @@ -71,10 +72,9 @@ func (e *MsgExecutor) Execute(_ context.Context, in executor.ExecuteInput) (exec
}, nil
}

// Assume `in.Command` contains the action and value in a structured format
// Parse the action and value from the command
action, value := parseCommand(in.Command)

// Use a generic key for simplicity; adapt if needed
sessionID := "default_session" // Replace with an actual identifier if available

// Initialize session state if not already present
Expand All @@ -86,7 +86,13 @@ func (e *MsgExecutor) Execute(_ context.Context, in executor.ExecuteInput) (exec
case "select_first":
// Store the selection from the first dropdown
e.state[sessionID]["first"] = value
return showBothSelects(e.state[sessionID]["first"], nil
return showBothSelects(e.state[sessionID])

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

View workflow job for this annotation

GitHub Actions / Release

not enough return values

case "select_dynamic":
// Store dynamic dropdown selections (flag is passed in the command)
flag := strings.Fields(value)[0]
e.state[sessionID][flag] = strings.TrimPrefix(value, flag+" ")
return showBothSelects(e.state[sessionID])

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

View workflow job for this annotation

GitHub Actions / Release

not enough return values
}

if strings.TrimSpace(in.Command) == pluginName {
Expand All @@ -99,6 +105,7 @@ func (e *MsgExecutor) Execute(_ context.Context, in executor.ExecuteInput) (exec
}, nil
}


// parseCommand parses the input command into action and value
func parseCommand(cmd string) (action, value string) {
parts := strings.Fields(cmd)
Expand Down Expand Up @@ -169,12 +176,13 @@ 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) executor.ExecuteOutput {
// showBothSelects dynamically generates dropdowns based on the selected options.
func showBothSelects(state map[string]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)
Expand All @@ -195,8 +203,8 @@ func showBothSelects(firstSelection) executor.ExecuteOutput {
},
},
InitialOption: &api.OptionItem{
Name: firstSelection,
Value: firstSelection,
Name: state["first"], // Get first selection from state
Value: state["first"],
},
},
},
Expand All @@ -205,9 +213,9 @@ func showBothSelects(firstSelection) executor.ExecuteOutput {
}

// Check if first selection is made
if firstSelection != "" {
if state["first"] != "" {
// Run the script to get dynamic options based on the first selection
scriptOutput, err := runScript(firstSelection)
scriptOutput, err := runScript(state["first"])
if err != nil {
log.Fatalf("Error running script: %v", err)
}
Expand All @@ -227,35 +235,43 @@ func showBothSelects(firstSelection) executor.ExecuteOutput {
})
}

// Create and append each dynamic dropdown to the sections
// Use the flag as a key to track dropdown selections
sections[0].Selects.Items = append(sections[0].Selects.Items, api.Select{
Name: option.Description, // Adjust name based on flags
Command: cmdPrefix(fmt.Sprintf("select_dynamic %s", option.Flags[0])), // Handle dynamic dropdown
OptionGroups: []api.OptionGroup{
{
Name: option.Description,
Options: dropdownOptions,
},
},
// InitialOption: &api.OptionItem{
// Name: state[option.Flags[0]], // Fetch previous selection from state
// Value: state[option.Flags[0]],
// },
})
}
}

code := fmt.Sprintf("run %s", firstSelection)
sections = append(sections, api.Section{
Base: api.Base{
Body: api.Body{
CodeBlock: code,
// If all selections are made, show the run button
if allSelectionsMade(state, scriptOutput.Options) {

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

View workflow job for this annotation

GitHub Actions / Release

undefined: scriptOutput
code := buildFinalCommand(state)
sections = append(sections, api.Section{
Base: api.Base{
Body: api.Body{
CodeBlock: code,
},
},
},
Buttons: []api.Button{
btnBuilder.ForCommandWithoutDesc("Run command", code, api.ButtonStylePrimary),
},
})
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.",
Plaintext: "You've selected from the dropdowns. Now run the command if ready.",
},
Sections: sections,
OnlyVisibleForYou: true,
Expand All @@ -264,7 +280,26 @@ func showBothSelects(firstSelection) executor.ExecuteOutput {
}
}

// Helper function to check if all selections are made
func allSelectionsMade(state map[string]string, options []Option) bool {
for _, option := range options {
if state[option.Flags[0]] == "" {
return false
}
}
return true
}

// Helper function to build the final command based on all selections
func buildFinalCommand(state map[string]string) string {
var commandParts []string
for key, value := range state {

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

View workflow job for this annotation

GitHub Actions / Release

key declared and not used
if value != "" {
commandParts = append(commandParts, value)
}
}
return fmt.Sprintf("run %s", strings.Join(commandParts, " "))
}

func (MsgExecutor) Help(context.Context) (api.Message, error) {
msg := description
Expand Down

0 comments on commit 08b200b

Please sign in to comment.