Skip to content

Commit

Permalink
rb
Browse files Browse the repository at this point in the history
  • Loading branch information
hirenko-v authored Sep 19, 2024
1 parent cd6a6f1 commit 6cc81ac
Showing 1 changed file with 49 additions and 111 deletions.
160 changes: 49 additions & 111 deletions cmd/msg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ const (
var version = "dev"

// MsgExecutor implements the Botkube executor plugin interface.
type MsgExecutor struct {
selectedJob string
selectedParam string
}
type MsgExecutor struct{}

// Metadata returns details about the Msg plugin.
func (MsgExecutor) Metadata(context.Context) (api.MetadataOutput, error) {
Expand All @@ -32,159 +29,100 @@ func (MsgExecutor) Metadata(context.Context) (api.MetadataOutput, error) {
}, nil
}

// Execute handles the command execution logic.
func (m *MsgExecutor) Execute(_ context.Context, in executor.ExecuteInput) (executor.ExecuteOutput, error) {
// Execute returns a given command as a response.
//
//nolint:gocritic //hugeParam: in is heavy (80 bytes); consider passing it by pointer
func (MsgExecutor) Execute(_ context.Context, in executor.ExecuteInput) (executor.ExecuteOutput, error) {
if !in.Context.IsInteractivitySupported {
return executor.ExecuteOutput{
Message: api.NewCodeBlockMessage("Interactivity for this platform is not supported", true),
}, nil
}

// Initial command to show job selection
if strings.TrimSpace(in.Command) == pluginName {
return m.initialJobSelection(), nil
}

// Handle job selection
if strings.HasPrefix(in.Command, "select job") {
m.selectedJob = strings.TrimPrefix(in.Command, "select job ")
return m.showParametersForJob(), nil
}

// Handle parameter selection
if strings.HasPrefix(in.Command, "select param") {
m.selectedParam = strings.TrimPrefix(in.Command, "select param ")
return m.showRunButton(), nil
}

// Handle final execution of the command
if strings.HasPrefix(in.Command, "run job") {
if m.selectedJob == "" || m.selectedParam == "" {
return executor.ExecuteOutput{
Message: api.NewCodeBlockMessage("Error: Job or parameter missing", true),
}, nil
}

// Execute final kubectl run command
return executor.ExecuteOutput{
Message: api.NewCodeBlockMessage(fmt.Sprintf("Executing: kubectl run %s %s", m.selectedJob, m.selectedParam), true),
}, nil
return initialMessages(), nil
}

// Fallback for unmatched commands
msg := fmt.Sprintf("Plain command: %s", in.Command)
return executor.ExecuteOutput{
Message: api.NewCodeBlockMessage(msg, true),
}, nil
}

// initialJobSelection shows only the job dropdown
func (m *MsgExecutor) initialJobSelection() executor.ExecuteOutput {
func initialMessages() executor.ExecuteOutput {
btnBuilder := api.NewMessageButtonBuilder()
cmdPrefix := func(cmd string) string {
return fmt.Sprintf("%s %s %s", api.MessageBotNamePlaceholder, pluginName, cmd)
}

// Define a list of jobs
jobs := []api.OptionItem{
{Name: "Job1", Value: "job1"},
{Name: "Job2", Value: "job2"},
{Name: "Job3", Value: "job3"},
}

return executor.ExecuteOutput{
Message: api.Message{
BaseBody: api.Body{
Plaintext: "Select a job:",
Plaintext: "Showcases interactive message capabilities",
},
Sections: []api.Section{
{
Buttons: []api.Button{
btnBuilder.ForCommandWithDescCmd("Run po", fmt.Sprintf("%s po", "kubectl get"), api.ButtonStylePrimary),
},
},
{
Selects: api.Selects{
ID: "job-dropdown",
ID: "select-id",
Items: []api.Select{
{
Name: "Select Job",
Command: cmdPrefix("select job"),
Name: "first",
Command: cmdPrefix("selects first"),
OptionGroups: []api.OptionGroup{
{
Name: "Jobs",
Options: jobs,
Name: cmdPrefix("selects first"),
Options: []api.OptionItem{
{Name: "BAR", Value: "BAR"},
{Name: "BAZ", Value: "BAZ"},
{Name: "XYZ", Value: "XYZ"},
},
},
},
InitialOption: &jobs[0], // Optional: Set an initial value
},
},
},
},
},
OnlyVisibleForYou: true,
ReplaceOriginal: false,
},
}
}

// showParametersForJob shows the parameter dropdown after a job is selected
func (m *MsgExecutor) showParametersForJob() executor.ExecuteOutput {
cmdPrefix := func(cmd string) string {
return fmt.Sprintf("%s %s %s", api.MessageBotNamePlaceholder, pluginName, cmd)
}

// Define a list of parameters
params := []api.OptionItem{
{Name: "Param1", Value: "param1"},
{Name: "Param2", Value: "param2"},
{Name: "Param3", Value: "param3"},
}

return executor.ExecuteOutput{
Message: api.Message{
BaseBody: api.Body{
Plaintext: fmt.Sprintf("You selected job: %s. Now select a parameter:", m.selectedJob),
},
Sections: []api.Section{
{
Selects: api.Selects{
ID: "param-dropdown",
Items: []api.Select{
{
Name: "Select Parameter",
Command: cmdPrefix("select param"),
Name: "second",
Command: cmdPrefix("selects second"),
OptionGroups: []api.OptionGroup{
{
Name: "Parameters",
Options: params,
Name: cmdPrefix("selects second"),
Options: []api.OptionItem{
{Name: "BAR", Value: "BAR"},
{Name: "BAZ", Value: "BAZ"},
{Name: "XYZ", Value: "XYZ"},
},
},
{
Name: cmdPrefix("selects second/section2"),
Options: []api.OptionItem{
{Name: "123", Value: "123"},
{Name: "456", Value: "456"},
{Name: "789", Value: "789"},
},
},
},
InitialOption: &params[0], // Optional: Set an initial value
// MUST be defined also under OptionGroups.Options slice.
InitialOption: &api.OptionItem{
Name: "789", Value: "789",
},
},
},
},
},
},
OnlyVisibleForYou: true,
ReplaceOriginal: false,
},
}
}

// showRunButton shows the "Run" button after both job and parameter are selected
func (m *MsgExecutor) showRunButton() executor.ExecuteOutput {
btnBuilder := api.NewMessageButtonBuilder()
cmdPrefix := func(cmd string) string {
return fmt.Sprintf("%s %s %s", api.MessageBotNamePlaceholder, pluginName, cmd)
}

return executor.ExecuteOutput{
Message: api.Message{
BaseBody: api.Body{
Plaintext: fmt.Sprintf("You selected job: %s and parameter: %s.", m.selectedJob, m.selectedParam),
},
Sections: []api.Section{
PlaintextInputs: []api.LabelInput{
{
Buttons: []api.Button{
btnBuilder.ForCommandWithDescCmd("Run", cmdPrefix(fmt.Sprintf("run job %s %s", m.selectedJob, m.selectedParam)), api.ButtonStylePrimary),
},
Command: cmdPrefix("input-text"),
DispatchedAction: api.DispatchInputActionOnEnter,
Placeholder: "String pattern to filter by",
Text: "Filter output",
},
},

OnlyVisibleForYou: true,
ReplaceOriginal: false,
},
Expand Down

0 comments on commit 6cc81ac

Please sign in to comment.