base commit

This commit is contained in:
Barak Michener 2016-10-31 15:10:02 -07:00
parent 4d7acdc014
commit 5d40de4bf3
3 changed files with 125 additions and 0 deletions

11
cmd/hoboken/main.go Normal file
View file

@ -0,0 +1,11 @@
package main
import "git.barakmich.com/barak/hoboken"
func main() {
p, err := hoboken.NewProjects("./")
if err != nil {
panic(err)
}
hoboken.WatchDir("./...", p)
}

95
types.go Normal file
View file

@ -0,0 +1,95 @@
package hoboken
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
log "github.com/Sirupsen/logrus"
"gopkg.in/yaml.v2"
)
type Project struct {
Name string `yaml:"name"`
Script string `yaml:"script"`
GitURL string `yaml:"gitURL"`
path string
scriptPath string
}
func (p Project) Exec() error {
tmpdir, err := ioutil.TempDir("", "hoboken")
if err != nil {
return err
}
defer os.RemoveAll(tmpdir)
cmd := exec.Command(p.scriptPath, tmpdir)
out, err := cmd.CombinedOutput()
log.Infoln(string(out))
return err
}
type Projects struct {
sync.RWMutex
projects []Project
path string
}
func NewProjects(path string) (*Projects, error) {
p := &Projects{
path: path,
}
return p, p.Reload()
}
func (p *Projects) Reload() error {
p.Lock()
defer p.Unlock()
p.projects = nil
filepath.Walk(p.path, walkPath(p))
log.Infof("%#v", p.projects)
return nil
}
var suffixes = []string{".yaml", ".yml"}
func HasYamlSuffix(s string) bool {
for _, suffix := range suffixes {
if strings.HasSuffix(strings.ToLower(s), suffix) {
return true
}
}
return false
}
func walkPath(p *Projects) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if info == nil {
return nil
}
if HasYamlSuffix(info.Name()) {
log.Println("Found", path)
f, err := os.Open(path)
if err != nil {
log.Errorf("Error opening %s: %s", path, err)
return nil
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
log.Errorf("Error reading %s: %s", path, err)
return nil
}
var newp Project
yaml.Unmarshal(b, &newp)
newp.scriptPath = filepath.Clean(filepath.Join(filepath.Dir(path), newp.Script))
newp.path = path
p.projects = append(p.projects, newp)
}
return nil
}
}

19
watch.go Normal file
View file

@ -0,0 +1,19 @@
package hoboken
import (
"log"
"github.com/rjeczalik/notify"
)
func WatchDir(path string, p *Projects) {
c := make(chan notify.EventInfo, 1)
if err := notify.Watch(path, c, notify.All); err != nil {
log.Fatal(err)
}
defer notify.Stop(c)
for _ = range c {
p.Reload()
}
}