// ExecuteTemplate executes a template on the source TeX files. func (t *CompileTask) ExecuteTemplate(templ *template.Template, data interface{}, inputFilename string, outputFilename string) { sc := t.context() useTempFile := outputFilename == "" if useTempFile { outputFilename = sc.MustGetTempFile().Name() } inputFilename = sc.AbsPath(t.defaultCompileFilename(inputFilename)) f, err := os.Create(sc.AbsPath(outputFilename)) if err != nil { panic(err) } w := io.Writer(f) err = templ.ExecuteTemplate(w, filepath.Base(inputFilename), data) if err != nil { panic(err) } f.Close() if useTempFile { // copy back, remove temp err = os.Remove(inputFilename) if err != nil { panic(err) } err = sc.CopyFile(outputFilename, inputFilename) if err != nil { panic(err) } } }
// Accept starts a new SMTP session using io.ReadWriteCloser func Accept(remoteAddress string, conn io.ReadWriteCloser, storage storage.Storage, messageChan chan *data.Message, hostname string, monkey monkey.ChaosMonkey) { defer conn.Close() proto := smtp.NewProtocol() proto.Hostname = hostname var link *linkio.Link reader := io.Reader(conn) writer := io.Writer(conn) if monkey != nil { linkSpeed := monkey.LinkSpeed() if linkSpeed != nil { link = linkio.NewLink(*linkSpeed * linkio.BytePerSecond) reader = link.NewLinkReader(io.Reader(conn)) writer = link.NewLinkWriter(io.Writer(conn)) } } session := &Session{conn, proto, storage, messageChan, remoteAddress, false, "", link, reader, writer, monkey} proto.LogHandler = session.logf proto.MessageReceivedHandler = session.acceptMessage proto.ValidateSenderHandler = session.validateSender proto.ValidateRecipientHandler = session.validateRecipient proto.ValidateAuthenticationHandler = session.validateAuthentication proto.GetAuthenticationMechanismsHandler = func() []string { return []string{"PLAIN"} } session.logf("Starting session") session.Write(proto.Start()) for session.Read() == true { if monkey != nil && monkey.Disconnect != nil && monkey.Disconnect() { session.conn.Close() break } } session.logf("Session ended") }
func TestWriteReadReplication(t *testing.T) { SetupCluster() log.Info("Testing TestWriteReadReplication...") _, other := GetProcessForPath("/tests/replication/write/read") resolv := tc.nodes[0].Cluster.Rings.GetGlobalRing().Resolve("/tests/replication/write/read") buf := buffer.NewFromString("write1") err := other.Fss.Write(fs.NewPath("/tests/replication/write/read"), buf.Size, "application/mytest", buf, nil) if err != nil { t.Errorf("1) Got an error while write: %s", err) } // test force local on exists context := tc.nodes[0].Fss.NewContext() context.ForceLocal = true bufwriter := bytes.NewBuffer(make([]byte, 0)) buffer := io.Writer(bufwriter) n, err := other.Fss.Read(fs.NewPath("/tests/replication/write/read"), 0, -1, 0, buffer, context) if err != fs.ErrorFileNotFound { t.Errorf("2) File shouldn't exists on another node: %s", err) } // force local replication on each secondary tc.nodes[resolv.GetOnline(1).Id].Fss.Flush() tc.nodes[resolv.GetOnline(2).Id].Fss.Flush() // check on first secondary secondaryid := resolv.GetOnline(1).Id context = tc.nodes[secondaryid].Fss.NewContext() context.ForceLocal = true bufwriter = bytes.NewBuffer(make([]byte, 0)) buffer = io.Writer(bufwriter) n, err = tc.nodes[secondaryid].Fss.Read(fs.NewPath("/tests/replication/write/read"), 0, -1, 0, buffer, context) if err != nil { t.Errorf("3) Got an error from read: %s", err) } if n != buf.Size || bytes.Compare(bufwriter.Bytes(), buf.Bytes()) != 0 { t.Errorf("4) Didn't read what was written: %s!=%s", buf.Bytes(), bufwriter) } // check on second secondary secondaryid = resolv.GetOnline(2).Id context = tc.nodes[secondaryid].Fss.NewContext() context.ForceLocal = true bufwriter = bytes.NewBuffer(make([]byte, 0)) buffer = io.Writer(bufwriter) n, err = tc.nodes[secondaryid].Fss.Read(fs.NewPath("/tests/replication/write/read"), 0, -1, 0, buffer, context) if err != nil { t.Errorf("3) Got an error from read: %s", err) } if n != buf.Size || bytes.Compare(bufwriter.Bytes(), buf.Bytes()) != 0 { t.Errorf("4) Didn't read what was written: %s!=%s", buf.Bytes(), bufwriter) } }
func SimpleWOFLogger(prefix string) *WOFLogger { logger := NewWOFLogger(prefix) stdout := io.Writer(os.Stdout) stderr := io.Writer(os.Stderr) logger.AddLogger(stdout, "status") logger.AddLogger(stderr, "error") return logger }
// EnvmanJSONPrint ... func EnvmanJSONPrint(envstorePth string) (string, error) { logLevel := log.GetLevel().String() args := []string{"--loglevel", logLevel, "--path", envstorePth, "print", "--format", "json", "--expand"} var outBuffer bytes.Buffer var errBuffer bytes.Buffer if err := cmdex.RunCommandWithWriters(io.Writer(&outBuffer), io.Writer(&errBuffer), "envman", args...); err != nil { return outBuffer.String(), fmt.Errorf("Error: %s, details: %s", err, errBuffer.String()) } return outBuffer.String(), nil }
// StepmanJSONLocalStepInfo ... func StepmanJSONLocalStepInfo(pth string) (string, error) { logLevel := log.GetLevel().String() args := []string{"--debug", "--loglevel", logLevel, "step-info", "--step-yml", pth, "--format", "json"} var outBuffer bytes.Buffer var errBuffer bytes.Buffer if err := cmdex.RunCommandWithWriters(io.Writer(&outBuffer), io.Writer(&errBuffer), "stepman", args...); err != nil { return outBuffer.String(), fmt.Errorf("Error: %s, details: %s", err, errBuffer.String()) } return outBuffer.String(), nil }
// StepmanJSONStepLibStepInfo ... func StepmanJSONStepLibStepInfo(collection, stepID, stepVersion string) (string, error) { logLevel := log.GetLevel().String() args := []string{"--debug", "--loglevel", logLevel, "step-info", "--collection", collection, "--id", stepID, "--version", stepVersion, "--format", "json"} var outBuffer bytes.Buffer var errBuffer bytes.Buffer if err := cmdex.RunCommandWithWriters(io.Writer(&outBuffer), io.Writer(&errBuffer), "stepman", args...); err != nil { return outBuffer.String(), fmt.Errorf("Error: %s, details: %s", err, errBuffer.String()) } return outBuffer.String(), nil }
func (e *Encrypter) InitWriter(fileWriter io.Writer) (io.Writer, error) { if e.streamWriter != nil { return io.Writer(e), fmt.Errorf("Encrypt writer already inited") } encStream, iv, err := getEncryptStream(e.passphrase) if err != nil { return io.Writer(e), err } if _, err := fileWriter.Write(iv); err != nil { return io.Writer(e), err } e.streamWriter = &cipher.StreamWriter{S: encStream, W: fileWriter} return io.Writer(e), nil }
// // Read a CX file and generate SIF. // func TestCx2Sif(t *testing.T) { output := new(bytes.Buffer) w := io.Writer(output) file, err := os.Open("../test_data/galcxStyle2.json") if err != nil { t.Fatal("Error:", err) return } // Close input file at the end of this defer file.Close() c2s := converter.Cx2Sif{} c2s.Convert(bufio.NewReader(file), w) result := output.String() lines := strings.Split(result, "\n") for idx := range lines { if lines[idx] == "" { fmt.Println(idx, ": EMPTY") } else { fmt.Println(idx, ": ", lines[idx]) } } if len(lines) != 363 { t.Errorf("Expected 363, got %d", len(lines)) } }
// CreateBufferedWriter ... func CreateBufferedWriter(buff *bytes.Buffer, writers ...io.Writer) io.Writer { if len(writers) > 0 { allWriters := append([]io.Writer{buff}, writers...) return io.MultiWriter(allWriters...) } return io.Writer(buff) }
func testSif2Cx(fName string, t *testing.T) { output := new(bytes.Buffer) w := io.Writer(output) f, err := os.Open(fName) if err != nil { t.Fatal("Error:", err) return } // Close input file at the end of this defer f.Close() s2c := Sif2Cx{Delimiter: rune(' ')} s2c.Convert(bufio.NewReader(f), w) result := output.String() t.Log(result) t.Log("Output length = ", len(result)) dec := json.NewDecoder(strings.NewReader(result)) var cx []interface{} dec.Decode(&cx) t.Log(cx) }
func StorePost(w http.ResponseWriter, req *http.Request) { filename := uuid.NewV4().String() now := time.Now().Format("20060102") path := repo + "/" + now err := os.MkdirAll(path, 0755) if err == nil { fullpath := path + "/" + filename f, err := os.Create(fullpath) if err == nil { defer f.Close() _, err := io.Copy(io.Writer(f), req.Body) if err == nil { log.Printf("%+v", req) requestUri := "http://" + req.Host + req.RequestURI + now + "/" + filename w.Write([]byte("Store OK : " + requestUri)) } else { ServerError(w, req, err) } } else { ServerError(w, req, err) } } else { ServerError(w, req, err) } }
// WriteLock writes the lock as YAML. // // Params: // - lockfile: A *cfg.Lockfile to render. // - out (io.Writer): An output stream to write to. Default is os.Stdout. func WriteLock(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) { skip := p.Get("skip", false).(bool) if skip { return false, nil } lockfile := p.Get("lockfile", nil).(*cfg.Lockfile) Info("Writing glide.lock file") data, err := lockfile.Marshal() if err != nil { return nil, err } var out io.Writer file, err := os.Create("glide.lock") if err != nil { return false, err } defer file.Close() out = io.Writer(file) out.Write(data) return true, nil }
func TestCx2CyjsAttributes(t *testing.T) { output := new(bytes.Buffer) resultWriter := io.Writer(output) file, err := os.Open("../test_data/Aurora_A_signaling.cx") if err != nil { t.Fatal("Error:", err) return } // Close input file at the end of this defer file.Close() c2c := converter.Cx2Cyjs{} c2c.Convert(bufio.NewReader(file), resultWriter) result := output.String() t.Log("Output length = ", len(result)) pass := checkAttributes(result, t) if pass { t.Log("Pass") } else { t.Error("Failed to validate Cytoscape.js output.") } }
// ExampleLogClient creates a LogClient, injecting a logging object // that will print each log entry in triplicate. // func ExampleLogClient() { // Create an object to Write() to Stdout. o := io.Writer(os.Stdout) // That's not fancy enough, so create an object to // triple-write to Stdout. tri := io.MultiWriter(o, o, o) // Create a LogClient, which will log messages to m. // Note that the c knows nothing about m except that it implements // the loginfo.Writer interface. Also, m knows nothing about // the loginfo.Writer interface: it is sufficient that it // implements a Write([]byte)(int,err) method. c := New(tri) // Cause c to write "Something\n" to its log. c.LogSomething() // Output: // Something // Something // Something }
func (operation *InitGenerateOperation) Run(logger log.Log) bool { logger.Info("running init operation:" + operation.output) var writer io.Writer switch operation.output { case "logger": fallthrough case "": writer = logger default: if strings.HasSuffix(operation.output, ".") { operation.output = operation.output + operation.handler } if fileWriter, err := os.Create(operation.output); err == nil { operation.skip = append(operation.skip, operation.output) writer = io.Writer(fileWriter) defer fileWriter.Close() logger.Message("Opening file for init generation output: " + operation.output) } else { logger.Error("Could not open output file to write init to:" + operation.output) } } initialize.Init_Generate(logger.MakeChild("init-generate"), operation.handler, operation.root, operation.skip, operation.sizeLimit, writer) return true }
func Open(path string) (*Graph, error) { var log *io.ReadWriter if path != "" { l, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666) if err != nil { return nil, err } var rw io.ReadWriter = l log = &rw } g := &Graph{} g.indexes = makeIndexSet() if log != nil { w := io.Writer(*log) g.log = &w r := api.NodeReader{} n := &Node{} for { node, err := r.Read(*log) if err == io.EOF { break } else if err != nil { panic(err) } n.FromApi(node) g.indexes.add(*n) } } return g, nil }
func writeIndex(outPath string) { outFile, err := os.Create(outPath) defer outFile.Close() if err != nil { panic(err) } writer := bufio.NewWriterSize(io.Writer(outFile), writerBufSize) var v *list.List dBuf := make([]byte, 4) tfBuf := make([]byte, 4) pBuf := make([]byte, 4) for _, k := range dictionary.keys { v = dictionary.m[k] writer.WriteString(k + ",") var posting *Posting for el := v.Front(); el != nil; el = el.Next() { posting = el.Value.(*Posting) binary.PutUvarint(dBuf, uint64(posting.doc)) writer.Write(dBuf) dBuf = []byte{0, 0, 0, 0} binary.PutUvarint(tfBuf, uint64(posting.tf)) writer.Write(tfBuf) tfBuf = []byte{0, 0, 0, 0} for posEl := posting.pos.Front(); posEl != nil; posEl = posEl.Next() { pos := posEl.Value.(uint32) binary.PutUvarint(pBuf, uint64(pos)) writer.Write(pBuf) pBuf = []byte{0, 0, 0, 0} } } writer.Write([]byte{0, 0, 0, 0}) } writer.Flush() }
func main() { var ( buffer = flag.Bool("buffer", false, "buffer stdout") ) flag.Parse() if len(flag.Args()) != 1 { log.Fatalf("Usage: %s file.bf\n", os.Args[0]) } data, err := ioutil.ReadFile(flag.Arg(0)) if err != nil { log.Fatalf("Reading %s: %s\n", flag.Arg(0), err.Error()) } out := io.Writer(os.Stdout) if *buffer { out = bufio.NewWriter(out) } f, e := bf.Compile(data, os.Stdin, out) if e != nil { log.Fatalf("compiling: %s", e.Error()) } var memory [4096]byte f(memory[:]) }
// ContainerLogs hooks up a container's stdout and stderr streams // configured with the given struct. func (c *Container) ContainerLogs(name string, config *backend.ContainerLogsConfig, started chan struct{}) error { defer trace.End(trace.Begin("")) // Look up the container name in the metadata cache to get long ID vc := cache.ContainerCache().GetContainer(name) if vc == nil { return NotFoundError(name) } name = vc.ContainerID tailLines, since, err := c.validateContainerLogsConfig(vc, config) if err != nil { return err } // Outstream modification (from Docker's code) so the stream is streamed with the // necessary headers that the CLI expects. This is Docker's scheme. wf := ioutils.NewWriteFlusher(config.OutStream) defer wf.Close() wf.Flush() outStream := io.Writer(wf) if !vc.Config.Tty { outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout) } // Make a call to our proxy to handle the remoting err = c.containerProxy.StreamContainerLogs(name, outStream, started, config.Timestamps, config.Follow, since, tailLines) return err }
// WriteYaml writes the config as YAML. // // Params: // - conf: A *cfg.Config to render. // - out (io.Writer): An output stream to write to. Default is os.Stdout. // - filename (string): If set, the file will be opened and the content will be written to it. func WriteYaml(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) { conf := p.Get("conf", nil).(*cfg.Config) toStdout := p.Get("toStdout", true).(bool) data, err := conf.Marshal() if err != nil { return nil, err } var out io.Writer if nn, ok := p.Has("filename"); ok && len(nn.(string)) > 0 { file, err := os.Create(nn.(string)) if err != nil { } defer file.Close() out = io.Writer(file) //fmt.Fprint(out, yml) out.Write(data) } else if toStdout { out = p.Get("out", os.Stdout).(io.Writer) //fmt.Fprint(out, yml) out.Write(data) } // Otherwise we supress output. return true, nil }
func startUDP() (io.Writer, chan []byte, chan struct{}) { remote, err := net.ResolveUDPAddr("udp", *remoteAddr) if err != nil { log.Fatalln("Can not resolve remote address:", err) } c, err := net.DialUDP("udp", nil, remote) conn := io.Writer(c) if err != nil { log.Fatalln("Can not listen on UDP:", err) } sink := make(chan []byte) ready := make(chan struct{}) go func() { buf := make([]byte, govpn.MTU) var n int var err error for { <-ready c.SetReadDeadline(time.Now().Add(time.Second)) n, err = c.Read(buf) if err != nil { sink <- nil continue } sink <- buf[:n] } }() ready <- struct{}{} return conn, sink, ready }
// получение таблиц func GetTablesProp(bd_name string) string { rows := doQuery("SELECT TABLE_NAME, TABLE_TYPE, ENGINE, IFNULL(TABLE_COMMENT, '') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=?", bd_name) if rows == nil { return "" } var result bytes.Buffer w := io.Writer(&result) Encode := json.NewEncoder(w) for rows.Next() { var row RecordTables // var i interface {} // i = &row rows.Scan(&row.TABLE_NAME, &row.TABLE_TYPE, &row.ENGINE, &row.TABLE_COMMENT) err := Encode.Encode(row) if err != nil { fmt.Println("error:", err) } } return result.String() }
func setCounter(name string, node int64, counter uint64) uint64 { var offset int64 fd, err := os.Open(name, os.O_WRONLY, 0666) if err == nil { defer fd.Close() // node 1 is the first node offset = (node - 1) * 8 _, seekerr := fd.Seek(offset, 0) if seekerr == nil { ior := io.Writer(fd) if err := binary.Write(ior, binary.LittleEndian, counter); err != nil { fmt.Printf("binary.Write failed: %s\n", err) } else { fd.Close() return counter } } else { fmt.Printf("Seek error for node %d to offset %d\n", node, offset) fd.Close() } } else { if cantOpenMessageCounterFile(name) == true { setCounter(name, node, counter) } } return 0 }
func TestToTar(t *testing.T) { content := fmt.Sprintf("a b c\neasy as\n1 2 3") out, err := ToTar("test_tar", 0644, content) if err != nil { t.Errorf("Error %#v while writing tar archive, expected nil", err.Error()) } var buffOut bytes.Buffer writer := io.Writer(&buffOut) for tr := tar.NewReader(out); err != io.EOF; _, err = tr.Next() { if err != nil { t.Errorf("Error %#v while reading tar archive, expected nil", err.Error()) } _, err = io.Copy(writer, tr) if err != nil { t.Errorf("Error %#v while reading tar archive, expected nil", err.Error()) } } actual := buffOut.String() if actual != content { t.Error("Generated incorrect tar archive.") } }
func Push(devices []Transporter, local io.Reader, mode os.FileMode, modtime uint32, remote string) error { d := make([]io.Writer, 0, len(devices)) for _, t := range devices { conn, err := GetPushWriter(t, remote, uint32(mode)) if err != nil { return err } d = append(d, io.Writer(conn)) defer conn.VerifyOk() } reader := bufio.NewReader(local) sections := NewSectionedMultiWriter(d...) writer := bufio.NewWriter(sections) writer.ReadFrom(reader) writer.Flush() sections.Close() wr := bufio.NewWriter(io.MultiWriter(d...)) wr.WriteString("DONE") binary.Write(wr, binary.LittleEndian, modtime) wr.Flush() return nil }
func (a *Archive) writer() (*tharWriter, error) { writer := io.Writer(a.Stream) flushers := []flushableWriter{} closers := []closeableWriter{} if a.Options.GZip { if a.Options.GZipLevel > 0 { gw, err := gzip.NewWriterLevel(writer, a.Options.GZipLevel) if err != nil { return nil, err } flushers = append([]flushableWriter{gw}, flushers...) closers = append([]closeableWriter{gw}, closers...) writer = gw } else { writer = gzip.NewWriter(writer) } } tw := tar.NewWriter(writer) flushers = append([]flushableWriter{tw}, flushers...) return &tharWriter{ Writer: tw, Flushers: flushers, Closers: closers, }, nil }
// GetFromContainer returns a string containing the content of the file named // SRC on the container with id ID. func (dk Client) GetFromContainer(id string, src string) (string, error) { var buffIn bytes.Buffer var buffOut bytes.Buffer err := dk.DownloadFromContainer(id, dkc.DownloadFromContainerOptions{ OutputStream: &buffIn, Path: src, }) if err != nil { return "", err } writer := io.Writer(&buffOut) for tr := tar.NewReader(&buffIn); err != io.EOF; _, err = tr.Next() { if err != nil { return "", err } _, err = io.Copy(writer, tr) if err != nil { return "", err } } return buffOut.String(), nil }
// Run runs go test benchmarks matching regex in the current directory and writes // benchmark data to a PSQL database by calling WriteSet. It returns any error // encountered. func (benchdb *BenchPSQL) Run() error { // Exec a subprocess for go test bench and write // to both stdout and a byte buffer. cmd := exec.Command("go", "test", "-bench", benchdb.Config.Regex, "-test.run", "XXX", "-benchmem") var out bytes.Buffer cmd.Stdout = io.MultiWriter(os.Stdout, &out) cmd.Stderr = io.Writer(os.Stderr) err := cmd.Run() if err != nil { return fmt.Errorf("command failed: %v", err) } benchSet, err := parse.ParseSet(&out) if err != nil { return fmt.Errorf("failed to parse benchmark data: %v", err) } // Writes parse set to sql database. _, err = benchdb.WriteSet(benchSet) if err != nil { return fmt.Errorf("failed to write benchSet to db: %v", err) } return nil }
func main() { mode = flag.String("m", "tcp", "portguard work mode: tcp or udp") debug = flag.Bool("d", false, "debug mode, print log to stderr") portCacheDuration = flag.Int64("duration", 120, "port cache duration") flag.Usage = usage flag.Parse() if *debug { mainLogger = log.New(io.Writer(os.Stderr), "", log.Ldate|log.Lmicroseconds) } else { var err error if mainLogger, err = syslog.NewLogger(syslog.LOG_ERR|syslog.LOG_LOCAL7, log.Ldate|log.Lmicroseconds); err != nil { logMain(true, "open syslog failed:%s", err.Error()) } } args := flag.Args() if len(args) > 0 { readConfigFile(args[0]) } configGuard() configEcho() if *mode == "tcp" { tcpGuard() } else if *mode == "udp" { udpGuard() } else { fmt.Fprintf(os.Stderr, "don't support mode: %s\n", *mode) } }