Compare commits

..

No commits in common. "736ff08c064ef074521f36f2f37220d255dd9a27" and "354428dd3ac5dde260f897b8a848096ea946a1bb" have entirely different histories.

2 changed files with 20 additions and 22 deletions

View file

@ -5,17 +5,16 @@ Simple Server for Webhook usage.
## Usage
```sh
./whook -c ./test.sh -d
./whook -c ./test.sh -d $(pwd)
```
The `test.sh` script contains only `echo "hellow"` so that the output after starting
the application and opening the given URL in side the browser we see following output:
```plain
2022/01/17 21:48:43 The HTTP server is running: http://localhost:8080/
2022/01/17 21:48:49 Executing a command...
2022/01/17 21:48:49 hellow
2022/01/17 19:14:28 The HTTP server is running: http://localhost:8080/
Executing a command...
hallow
```
## Options
@ -23,12 +22,11 @@ the application and opening the given URL in side the browser we see following o
A small server to listen for requests to execute some particular code.
USAGE:
whook [FLAGS]
whook [FLAGS]
FLAGS:
-c, --cmd string REQUIRED: The command to execute.
-d, --dir string The Directory to execute the command.
Defaults to the directory the tool was called on.
-d, --dir string REQUIRED: The Directory to execute the command.
-h, --help Prints this help message and exits.
-p, --port string Port to listen for incoming connections. (default "8080")
```

28
main.go
View file

@ -29,20 +29,17 @@ func init() {
flags.BoolVarP(&isHelp, "help", "h", false, "Prints this help message and exits.")
flags.StringVarP(&cmdPath, "cmd", "c", "", "REQUIRED: The command to execute.")
flags.StringVarP(&commandExecDir, "dir", "d", "", "The Directory to execute the command.\nDefaults to the directory the tool was called on.")
flags.StringVarP(&commandExecDir, "dir", "d", "", "REQUIRED: The Directory to execute the command.")
flags.Parse()
}
// handles the webhook requests
func handler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close() // so I don't forget to close it -> eventhough it's not read.
defer r.Body.Close()
log.Println("Executing a command...")
cmd := exec.Command(cmdPath)
// set directory only if it's present.
if commandExecDir != "" {
cmd.Dir = commandExecDir
}
cmd.Dir = commandExecDir
out, err := cmd.Output()
if err != nil {
log.Println(err)
@ -56,11 +53,10 @@ func handler(w http.ResponseWriter, r *http.Request) {
func main() {
if isHelp {
printHelp()
// printing help is treated as a successful execution.
os.Exit(0)
}
err := validateParams()
err := validate()
if err != nil {
log.Fatalln(err)
}
@ -68,7 +64,7 @@ func main() {
// use the default mux because it implements the Handler interface
// which we need for the server struct.
mux := http.NewServeMux()
mux.HandleFunc("/", handler) // only add one handler on root path
mux.HandleFunc("/", handler) // only add out handler on root path
// custom server struct to set custom timeouts for better performance.
server := &http.Server{
@ -101,13 +97,14 @@ func startHTTPServer(server *http.Server, wg *sync.WaitGroup) {
func listenToStopHTTPServer(server *http.Server, wg *sync.WaitGroup) {
defer wg.Done()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, os.Kill)
<-stop // block til signal is captured.
<-stop
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("An error happened on the shutdown of the server: %v", err)
}
@ -117,17 +114,20 @@ func printHelp() {
fmt.Println(`A small server to listen for requests to execute some particular code.
USAGE:
whook [FLAGS]
whook [FLAGS]
FLAGS:`)
flags.PrintDefaults()
}
// validateParams if required params are present.
func validateParams() error {
// validate if required params are presend.
func validate() error {
if cmdPath == "" {
return errors.New("Missing required 'cmd' parameter")
}
if commandExecDir == "" {
return errors.New("Missing required 'dir' parameter")
}
return nil
}