Allow passing in a configuration file with argparse (fix #6)

This commit is contained in:
Noah 2024-03-08 15:50:30 -06:00
parent 428cedeb45
commit 6e4109cfd5
3 changed files with 17 additions and 3 deletions

1
go.mod
View File

@ -3,6 +3,7 @@ module code.retroedge.tech/noah/lapisdeploy
go 1.22.0 go 1.22.0
require ( require (
github.com/akamensky/argparse v1.4.0 // indirect
github.com/gogs/git-module v1.8.3 // indirect github.com/gogs/git-module v1.8.3 // indirect
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75 // indirect github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75 // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect

2
go.sum
View File

@ -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.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/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= github.com/gogs/git-module v1.8.3 h1:4N9HOLzkmSfb5y4Go4f/gdt1/Z60/aQaAKr8lbsfFps=

17
main.go
View File

@ -7,6 +7,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"github.com/akamensky/argparse"
) )
// Create a configuration struct // Create a configuration struct
@ -38,7 +39,16 @@ func handler(data map[string]interface{}) {
// main is the starting point of the program // main is the starting point of the program
func main() { 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){ http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
var data map[string]interface{} var data map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&data) err := json.NewDecoder(r.Body).Decode(&data)
@ -47,8 +57,9 @@ func main() {
return return
} }
go handler(data) go handler(data) // Run the handler
fmt.Fprint(w, "Received!") fmt.Fprint(w, "Received!")
}) })
go log.Fatal(http.ListenAndServe(":7575", nil)) log.Printf("Starting Lapis Deploy...")
log.Fatal(http.ListenAndServe(":7575", nil))
} }