95 lines
1.8 KiB
Go
95 lines
1.8 KiB
Go
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
|
|
}
|
|
}
|