Compare commits
4 commits
354428dd3a
...
736ff08c06
Author | SHA1 | Date | |
---|---|---|---|
736ff08c06 | |||
7a879c746b | |||
ddaddcd6f3 | |||
6cd5aa1953 |
2 changed files with 22 additions and 20 deletions
12
README.md
12
README.md
|
@ -5,16 +5,17 @@ Simple Server for Webhook usage.
|
|||
## Usage
|
||||
|
||||
```sh
|
||||
./whook -c ./test.sh -d $(pwd)
|
||||
./whook -c ./test.sh -d
|
||||
```
|
||||
|
||||
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 19:14:28 The HTTP server is running: http://localhost:8080/
|
||||
Executing a command...
|
||||
hallow
|
||||
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
|
||||
|
||||
```
|
||||
|
||||
## Options
|
||||
|
@ -26,7 +27,8 @@ USAGE:
|
|||
|
||||
FLAGS:
|
||||
-c, --cmd string REQUIRED: The command to execute.
|
||||
-d, --dir string REQUIRED: The Directory to execute the command.
|
||||
-d, --dir string The Directory to execute the command.
|
||||
Defaults to the directory the tool was called on.
|
||||
-h, --help Prints this help message and exits.
|
||||
-p, --port string Port to listen for incoming connections. (default "8080")
|
||||
```
|
||||
|
|
24
main.go
24
main.go
|
@ -29,17 +29,20 @@ 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", "", "REQUIRED: The Directory to execute the command.")
|
||||
flags.StringVarP(&commandExecDir, "dir", "d", "", "The Directory to execute the command.\nDefaults to the directory the tool was called on.")
|
||||
flags.Parse()
|
||||
}
|
||||
|
||||
// handles the webhook requests
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
defer r.Body.Close() // so I don't forget to close it -> eventhough it's not read.
|
||||
|
||||
log.Println("Executing a command...")
|
||||
cmd := exec.Command(cmdPath)
|
||||
// set directory only if it's present.
|
||||
if commandExecDir != "" {
|
||||
cmd.Dir = commandExecDir
|
||||
}
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
@ -53,10 +56,11 @@ 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 := validate()
|
||||
err := validateParams()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
@ -64,7 +68,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 out handler on root path
|
||||
mux.HandleFunc("/", handler) // only add one handler on root path
|
||||
|
||||
// custom server struct to set custom timeouts for better performance.
|
||||
server := &http.Server{
|
||||
|
@ -97,14 +101,13 @@ 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
|
||||
<-stop // block til signal is captured.
|
||||
|
||||
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)
|
||||
}
|
||||
|
@ -120,14 +123,11 @@ FLAGS:`)
|
|||
flags.PrintDefaults()
|
||||
}
|
||||
|
||||
// validate if required params are presend.
|
||||
func validate() error {
|
||||
// validateParams if required params are present.
|
||||
func validateParams() error {
|
||||
if cmdPath == "" {
|
||||
return errors.New("Missing required 'cmd' parameter")
|
||||
}
|
||||
if commandExecDir == "" {
|
||||
return errors.New("Missing required 'dir' parameter")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue