Commit b9f15d59 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Initial commit

parents
package main
import (
"bufio"
"bytes"
"io/ioutil"
"log"
"strings"
"9fans.net/go/acme"
)
func main() {
l, err := acme.Log()
if err != nil {
log.Fatal(err)
}
for {
event, err := l.Read()
if err != nil {
log.Fatal(err)
}
if event.Name != "" && event.Op == "put" && strings.HasSuffix(event.Name, ".rb") {
stripTrailingWhitespace(event.ID, event.Name)
}
}
}
func stripTrailingWhitespace(id int, name string) {
log.Printf("event ID: %d, name: %q", id, name)
w, err := acme.Open(id, nil)
if err != nil {
log.Print(err)
return
}
defer w.CloseFiles()
oldContent, err := ioutil.ReadFile(name)
if err != nil {
//log.Print(err)
return
}
newContent := make([]byte, len(oldContent))
scanner := bufio.NewScanner(bytes.NewReader(oldContent))
for scanner.Scan() {
newContent = append(newContent, bytes.TrimRight(scanner.Bytes(), " ")...)
newContent = append(newContent, '\n')
}
if scanner.Err() != nil {
log.Print(err)
return
}
if bytes.Equal(oldContent, newContent) {
return
}
w.Write("ctl", []byte("addr=dot"))
q0, q1, err := w.ReadAddr()
if err != nil {
log.Print(err)
return
}
log.Printf("q0,q1 %d,%d", q0, q1)
for _, cmd := range []string{"mark", "nomark"} { //, "addr=dot"} {
if _, err := w.Write("ctl", []byte(cmd)); err != nil {
log.Printf("write ctl: %v", err)
return
}
}
q0, q1, err = w.ReadAddr()
if err != nil {
log.Print(err)
return
}
log.Printf("q0,q1 %d,%d", q0, q1)
if err := w.Addr(","); err != nil {
log.Print(err)
return
}
if _, err := w.Write("data", newContent); err != nil {
log.Print(err)
return
}
if err := w.Addr("%d,%d", q0, q1); err != nil {
log.Print(err)
return
}
if _, err := w.Write("ctl", []byte("dot=addr")); err != nil {
log.Print(err)
return
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment