func main() { // Receive JSON message from stdin msg, err := chrome.Receive(os.Stdin) if err != nil { postError(err) panic(err) } /*data := string(msg) // Obtain Source value sBegin := strings.Index(data, "\"source\":") if sBegin == -1 { panic(nil) // Cannot find source } sEnd := strings.Index(data[sBegin + 10:], "\"") // Todo: handle escaped double quotes if sEnd == -1 { panic(nil) // Cannot find source } source := data[sBegin + 10 : sBegin + 10 + sEnd] if source == "" { panic(nil) // Source is empty }*/ // Make sure the sources directory exists if _, err := os.Stat(string(filepath.Separator) + "sources"); os.IsNotExist(err) { postError(err) os.Mkdir("sources", 0777) } // Save JSON message to sources/<source>.json filename := "sources" + string(filepath.Separator) + "test" + ".json" //filename := "sources" + string(filepath.Separator) + sanitize.BaseName(source) + ".json" err = ioutil.WriteFile(filename, msg, 0644) // Todo: handle long Source if err != nil { postError(err) panic(err) // Error writing to file } chrome.Post(msg, os.Stdout) }
func postError(err error) { chrome.Post([]byte("{\"error\":\""+err.Error()+"\"}"), os.Stdout) }