From 6e4109cfd52dec8aca4c56df9f5df46f458f529d Mon Sep 17 00:00:00 2001 From: Noah Date: Fri, 8 Mar 2024 15:50:30 -0600 Subject: [PATCH] Allow passing in a configuration file with argparse (fix #6) --- go.mod | 1 + go.sum | 2 ++ main.go | 17 ++++++++++++++--- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 1251d98..ca9c279 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module code.retroedge.tech/noah/lapisdeploy go 1.22.0 require ( + github.com/akamensky/argparse v1.4.0 // indirect github.com/gogs/git-module v1.8.3 // indirect github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75 // indirect golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect diff --git a/go.sum b/go.sum index 0f7de59..ce445e9 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/akamensky/argparse v1.4.0 h1:YGzvsTqCvbEZhL8zZu2AiA5nq805NZh75JNj4ajn1xc= +github.com/akamensky/argparse v1.4.0/go.mod h1:S5kwC7IuDcEr5VeXtGPRVZ5o/FdhcMlQz4IZQuw64xA= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gogs/git-module v1.8.3 h1:4N9HOLzkmSfb5y4Go4f/gdt1/Z60/aQaAKr8lbsfFps= diff --git a/main.go b/main.go index 9404809..b3509a7 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "os" + "github.com/akamensky/argparse" ) // Create a configuration struct @@ -38,7 +39,16 @@ func handler(data map[string]interface{}) { // main is the starting point of the program func main() { - parseConfig("config.json", &configuration) + // Parse arguments + parser := argparse.NewParser("lapisdeploy", "Easily deploy Lapis web applications through Gitea webhooks") + config_path := parser.String("c", "config", &argparse.Options{Required: false, Help: "Configuration file", Default: "./config.json"}) + if err := parser.Parse(os.Args); err != nil { // Parse arguments + fmt.Print(parser.Usage(err)) // Show usage if there's an error + return + } + + parseConfig(*config_path, &configuration) // Parse JSON configuration file + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){ var data map[string]interface{} err := json.NewDecoder(r.Body).Decode(&data) @@ -47,8 +57,9 @@ func main() { return } - go handler(data) + go handler(data) // Run the handler fmt.Fprint(w, "Received!") }) - go log.Fatal(http.ListenAndServe(":7575", nil)) + log.Printf("Starting Lapis Deploy...") + log.Fatal(http.ListenAndServe(":7575", nil)) }