feat: CLI

This commit is contained in:
HeshamTB 2023-11-07 22:20:35 +03:00
parent 71d36d63fe
commit c348a0ba67
3 changed files with 79 additions and 10 deletions

8
go.mod
View File

@ -1,3 +1,11 @@
module gitea.hbanafa.com/hesham/hooker module gitea.hbanafa.com/hesham/hooker
go 1.21.1 go 1.21.1
require github.com/urfave/cli/v2 v2.25.7
require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
)

8
go.sum Normal file
View File

@ -0,0 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=

View File

@ -8,12 +8,16 @@ import (
"strconv" "strconv"
"sync" "sync"
"time" "time"
"github.com/urfave/cli/v2"
) )
/* /*
Listen for git web hooks and then execute a script. Listen for git web hooks and then execute a script.
*/ */
var verbose bool
type TaskDefErr struct { type TaskDefErr struct {
message string message string
} }
@ -67,16 +71,7 @@ func InitTask() TaskManifest {
} }
func init() { func init() {
TAG := "[ init ]"
l := func(msg string) {
log.Println(TAG, msg)
}
l("starting")
l("Validating Task")
task = InitTask()
l("Registering handlers")
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", handleRoot) mux.HandleFunc("/", handleRoot)
@ -90,12 +85,70 @@ func init() {
} }
func main() { func runServer() error {
log.Println("Task", task) log.Println("Task", task)
log.Printf("Listening on %s", httpServer.Addr) log.Printf("Listening on %s", httpServer.Addr)
log.Fatal(httpServer.ListenAndServe()) log.Fatal(httpServer.ListenAndServe())
log.Println("Server Stopped") log.Println("Server Stopped")
return nil
}
func main() {
app := cli.App{
Name: "hooker",
Description: "Listen for git web hooks and then execute a script or executable",
Authors: []*cli.Author{
{
Name: "Hesham T. Banafa",
Email: "hishaminv@gmail.com",
},
},
Flags: []cli.Flag{
&cli.Uint64Flag{
Name: "repo_id",
Usage: "Target Repo ID reported by git webhook",
Destination: &task.RepoID,
Required: true,
},
&cli.StringFlag{
Name: "owner",
Usage: "Repo Owner username",
Destination: &task.Owner,
Required: true,
},
&cli.StringFlag{
Name: "auth",
Usage: "Auth Secret",
Destination: &task.Auth,
Required: true,
},
&cli.StringFlag{
Name: "command",
Aliases: []string{"c"},
Usage: "Command is the script or executable to be run upon webhook",
Destination: &task.Command,
Required: true,
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Value: false,
Usage: "Produce more verbose output",
Destination: &verbose,
},
},
Action: func(ctx *cli.Context) error {
err := runServer()
return err
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
} }