diff --git a/cmd/hoboken/main.go b/cmd/hoboken/main.go new file mode 100644 index 0000000..f2216aa --- /dev/null +++ b/cmd/hoboken/main.go @@ -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) +} diff --git a/types.go b/types.go new file mode 100644 index 0000000..285e831 --- /dev/null +++ b/types.go @@ -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 + } +} diff --git a/watch.go b/watch.go new file mode 100644 index 0000000..babc491 --- /dev/null +++ b/watch.go @@ -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() + } +}