Compare commits

...

4 commits

Author SHA1 Message Date
Augusto Dwenger J. 736ff08c06
Update README
- Update options output
- Update call example
- Update call example output
2022-01-17 21:53:25 +01:00
Augusto Dwenger J. 7a879c746b
Fix usage indentation in the help message 2022-01-17 21:52:54 +01:00
Augusto Dwenger J. ddaddcd6f3
Increase readablity 2022-01-17 21:46:27 +01:00
Augusto Dwenger J. 6cd5aa1953
Make dir param no longer required 2022-01-17 21:45:25 +01:00
2 changed files with 22 additions and 20 deletions

View file

@ -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
@ -22,11 +23,12 @@ hallow
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 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")
```

28
main.go
View file

@ -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)
cmd.Dir = commandExecDir
// 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)
}
@ -114,20 +117,17 @@ func printHelp() {
fmt.Println(`A small server to listen for requests to execute some particular code.
USAGE:
whook [FLAGS]
whook [FLAGS]
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
}