Add configuration file (fix #5)

This commit is contained in:
Noah 2024-03-08 15:16:33 -06:00
parent afcbcdd1a7
commit a58eb557ab
3 changed files with 25 additions and 5 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
lapisdeploy
config.json
sites/

3
config.json Normal file
View File

@ -0,0 +1,3 @@
{
"sites_dir": "./sites"
}

26
main.go
View File

@ -13,7 +13,10 @@ import (
"github.com/gogs/git-module"
)
const sites_dir = "./sites"
type Configuration struct {
sites_dir string
}
var configuration = Configuration{}
type DeployError struct {
code int
@ -39,7 +42,7 @@ func restartServer(name string) DeployError {
old_cwd, _ := os.Getwd()
defer syscall.Chdir(old_cwd)
syscall.Chdir(sites_dir+"/"+name)
syscall.Chdir(configuration.sites_dir+"/"+name)
out, err := exec.Command("lapis", "build").CombinedOutput()
if err != nil {
@ -54,19 +57,19 @@ func restartServer(name string) DeployError {
// pullRepo attempts to pull or clone a repository using the given name and url
func pullRepo(url string, name string) DeployError {
exists, err := fileExists(sites_dir+"/"+name)
exists, err := fileExists(configuration.sites_dir+"/"+name)
if err != nil {
return newDeployError(1, "fileExists",
fmt.Sprintf("Failed to check whether folder '%s' exists while pulling down repository '%s'!", name, name), "")
}
if !exists {
log.Printf("Cloning repository %s...", name)
if err = git.Clone(url, sites_dir+"/"+name); err != nil {
if err = git.Clone(url, configuration.sites_dir+"/"+name); err != nil {
return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to pull down repository '%s'", name), "")
}
} else {
log.Printf("Pulling down repository %s...", name)
repo,err := git.Open(sites_dir+"/"+name)
repo,err := git.Open(configuration.sites_dir+"/"+name)
if err != nil {
return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to open git repository '%s'", name), "")
}
@ -75,6 +78,18 @@ func pullRepo(url string, name string) DeployError {
return DeployError{}
}
// parseConfig parses the JSON configuration file at 'path' and stores the contents in 'config'
func parseConfig(path string, config *Configuration) {
file, _ := os.Open(path)
var data map[string]interface{}
err := json.NewDecoder(file).Decode(&data)
if err != nil {
log.Panic("Failed to parse config:", err)
}
file.Close()
config.sites_dir = data["sites_dir"].(string)
}
func handler(data map[string]interface{}) {
repository := data["repository"].(map[string]interface{})
log.Default().Printf("Repo: %s", repository["full_name"])
@ -91,6 +106,7 @@ func handler(data map[string]interface{}) {
}
func main() {
parseConfig("config.json", &configuration)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
var data map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&data)