gohookr/config/command.go

42 lines
783 B
Go
Raw Normal View History

2021-08-04 11:04:01 +00:00
package config
2021-08-06 13:29:24 +00:00
import (
"fmt"
2023-10-28 20:48:54 +00:00
"net/http"
2021-08-06 13:29:24 +00:00
"os/exec"
2023-10-28 20:48:54 +00:00
"strings"
2021-08-06 13:29:24 +00:00
)
2021-08-04 11:04:01 +00:00
type Command struct {
Program string
Arguments []string
AppendPayload bool
2023-10-28 20:48:54 +00:00
AppendHeaders bool
2021-08-04 11:04:01 +00:00
}
2023-10-28 20:48:54 +00:00
func (c Command) Execute(payload string, header http.Header) ([]byte, error) {
arguments := make([]string, len(c.Arguments))
copy(arguments, c.Arguments)
2023-10-28 20:48:54 +00:00
2021-08-04 11:04:01 +00:00
if c.AppendPayload {
arguments = append(arguments, payload)
}
2023-10-28 20:48:54 +00:00
if c.AppendHeaders {
var header_builder strings.Builder;
header.Write(&header_builder);
arguments = append(arguments, header_builder.String())
}
2021-08-04 11:04:01 +00:00
return exec.Command(c.Program, arguments...).Output()
}
2021-08-06 13:29:24 +00:00
func (c Command) String() string {
return fmt.Sprintf(
"<Command cmd=%v AppendPayload=%v>",
append([]string{c.Program}, c.Arguments...),
c.AppendPayload,
)
}