Created
August 11, 2019 19:35
-
-
Save selfup/a2e39837b48eea4a6b55ce7694a4682a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"os/exec" | |
"path/filepath" | |
"time" | |
) | |
func main() { | |
currentTime := time.Now() | |
scnnr := FindGoFiles{ | |
Directory: ".", | |
} | |
poll(scnnr, currentTime) | |
} | |
func poll(scnnr FindGoFiles, currentTime time.Time) { | |
err := scnnr.Scan() | |
if err != nil { | |
panic(err) | |
} | |
for _, file := range scnnr.Found { | |
info, err := os.Stat(file) | |
if err != nil { | |
fmt.Println(err) | |
} | |
if info.ModTime().Unix() > currentTime.Unix() { | |
cmd := exec.Command("go", "test") | |
cmd.Stdin = os.Stdin | |
cmd.Stdout = os.Stdout | |
cmd.Stderr = os.Stderr | |
cmdErr := cmd.Run() | |
if cmdErr != nil { | |
log.Fatal(cmdErr) | |
} | |
currentTime = time.Now() | |
} | |
} | |
time.Sleep(1 * time.Second) | |
poll(scnnr, currentTime) | |
} | |
// FindGoFiles holds cli args | |
type FindGoFiles struct { | |
Directory string | |
Found []string | |
} | |
// Scan walks the given directory tree | |
func (f *FindGoFiles) Scan() error { | |
err := filepath.Walk(f.Directory, f.scan) | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
func (f *FindGoFiles) scan(path string, info os.FileInfo, err error) error { | |
if err != nil { | |
return err | |
} | |
if !info.IsDir() { | |
if filepath.Ext(path) == ".go" { | |
f.Found = append(f.Found, path) | |
} | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment