package main
import (
"encoding/json"
"log"
"io/ioutil"
)var Repos []Repository
// Loads the configuration file. The configuration file is assumed to// be a json file. Currently just loads all repositories with their// corresponding file paths. It also reads in the SCM type, which// I intend to identify through the program in the future without// needing it to be specified in the configfunc LoadConfig() {
const CONF_PATH = "config.json"
type JsonRepo struct {
Name string
Path string
Scm string
}
type Configuration struct {
Repositories []JsonRepo
}
var conf Configuration
content, err := ioutil.ReadFile(CONF_PATH)
if err != nil {
log.Fatal("ReadFile: ", err)
}
if json.Unmarshal(content, &conf) != nil {
log.Fatal("Unmarshal: ", err)
}
for _, r := range conf.Repositories {
switch r.Scm {
// to implement more later.. for now it's just git
Util
package main
import (
"os/exec"
"fmt"
)type GitRepository struct {
RepositoryInfo
}func (repo *GitRepository) GetName() string {
return repo.Name
}func (repo *GitRepository) GetPath() string {
return repo.Path
}// GetFile implementation - returns the file bytes// after executing cd /path/to/repo ; git show sha// This cmd is returning an empty byte[] for me atm, need to investigate laterfunc (repo *GitRepository) GetFile(id string) []byte {
out, _ := exec.Command("cd", repo.Path, ";", "git", "show", id).Output()
fmt.Println(out)
return out
}// GetFileExists implementation - returns true if the file exists// after executing cd /path/to/repo ; git show shafunc (repo *GitRepository) GetFileExists(id string) bool {
cmd := exec.Command("cd", repo.Path, ";", "git", "show", id)
return cmd.Run() == nil
}Git Repository
| Main | |
|---|---|
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "log" |
| 6 | ) |
| 7 | |
| 8 | func main() { |
| 9 | // this calls loadConfig in util.go. |
| 10 | // I might put util.go in another package in the near future |
| 11 | LoadConfig() |
| 12 | |
| 13 | // this calls service.go's route to handle the routing. |
| 14 | Route() |
| 15 | |
| 16 | // Start the server on port 8888 |
| 17 | err := http.ListenAndServe(":8888", nil) |
| 18 | if err != nil { |
| 19 | log.Fatal("ListenAndServe: ", err) |
| 20 | } |
| 21 | } |