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 ## Usage
```sh ```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 `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: the application and opening the given URL in side the browser we see following output:
```plain ```plain
2022/01/17 21:48:43 The HTTP server is running: http://localhost:8080/ 2022/01/17 19:14:28 The HTTP server is running: http://localhost:8080/
2022/01/17 21:48:49 Executing a command... Executing a command...
2022/01/17 21:48:49 hellow hallow
``` ```
## Options ## 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. A small server to listen for requests to execute some particular code.
USAGE: USAGE:
whook [FLAGS] whook [FLAGS]
FLAGS: FLAGS:
-c, --cmd string REQUIRED: The command to execute. -c, --cmd string REQUIRED: The command to execute.
-d, --dir string The Directory to execute the command. -d, --dir string REQUIRED: The Directory to execute the command.
Defaults to the directory the tool was called on.
-h, --help Prints this help message and exits. -h, --help Prints this help message and exits.
-p, --port string Port to listen for incoming connections. (default "8080") -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.BoolVarP(&isHelp, "help", "h", false, "Prints this help message and exits.")
flags.StringVarP(&cmdPath, "cmd", "c", "", "REQUIRED: The command to execute.") 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() flags.Parse()
} }
// handles the webhook requests // handles the webhook requests
func handler(w http.ResponseWriter, r *http.Request) { 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...") log.Println("Executing a command...")
cmd := exec.Command(cmdPath) cmd := exec.Command(cmdPath)
// set directory only if it's present. cmd.Dir = commandExecDir
if commandExecDir != "" {
cmd.Dir = commandExecDir
}
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -56,11 +53,10 @@ func handler(w http.ResponseWriter, r *http.Request) {
func main() { func main() {
if isHelp { if isHelp {
printHelp() printHelp()
// printing help is treated as a successful execution.
os.Exit(0) os.Exit(0)
} }
err := validateParams() err := validate()
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -68,7 +64,7 @@ func main() {
// use the default mux because it implements the Handler interface // use the default mux because it implements the Handler interface
// which we need for the server struct. // which we need for the server struct.
mux := http.NewServeMux() 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. // custom server struct to set custom timeouts for better performance.
server := &http.Server{ server := &http.Server{
@ -101,13 +97,14 @@ func startHTTPServer(server *http.Server, wg *sync.WaitGroup) {
func listenToStopHTTPServer(server *http.Server, wg *sync.WaitGroup) { func listenToStopHTTPServer(server *http.Server, wg *sync.WaitGroup) {
defer wg.Done() defer wg.Done()
stop := make(chan os.Signal, 1) stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, os.Kill) signal.Notify(stop, os.Interrupt, os.Kill)
<-stop // block til signal is captured.
<-stop
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
if err := server.Shutdown(ctx); err != nil { if err := server.Shutdown(ctx); err != nil {
log.Fatalf("An error happened on the shutdown of the server: %v", err) 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. fmt.Println(`A small server to listen for requests to execute some particular code.
USAGE: USAGE:
whook [FLAGS] whook [FLAGS]
FLAGS:`) FLAGS:`)
flags.PrintDefaults() flags.PrintDefaults()
} }
// validateParams if required params are present. // validate if required params are presend.
func validateParams() error { func validate() error {
if cmdPath == "" { if cmdPath == "" {
return errors.New("Missing required 'cmd' parameter") return errors.New("Missing required 'cmd' parameter")
} }
if commandExecDir == "" {
return errors.New("Missing required 'dir' parameter")
}
return nil return nil
} }