diff --git a/.gitignore b/.gitignore index 4f52390..c5dfc4a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ lapisdeploy +config.json sites/ diff --git a/config.json b/config.json new file mode 100644 index 0000000..6b23d7b --- /dev/null +++ b/config.json @@ -0,0 +1,3 @@ +{ + "sites_dir": "./sites" +} diff --git a/main.go b/main.go index 9acc727..6a93906 100644 --- a/main.go +++ b/main.go @@ -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)