func ExampleWalk() { in := `echo $foo "and $bar"` f, err := syntax.Parse([]byte(in), "", 0) if err != nil { return } syntax.Walk(paramUpper{}, f) syntax.Fprint(os.Stdout, f) // Output: echo $FOO "and $BAR" }
func Example() { in := "{ foo;bar; }" f, err := syntax.Parse([]byte(in), "", 0) if err != nil { return } syntax.Fprint(os.Stdout, f) // Output: // { // foo // bar // } }
func formatStdin() error { if *write || *list { return fmt.Errorf("-w and -l can only be used on files") } readBuf.Reset() if _, err := io.Copy(&readBuf, os.Stdin); err != nil { return err } src := readBuf.Bytes() prog, err := syntax.Parse(src, "", parseMode) if err != nil { return err } return printConfig.Fprint(out, prog) }
func formatPath(path string, checkShebang bool) error { openMode := os.O_RDONLY if *write { openMode = os.O_RDWR } f, err := os.OpenFile(path, openMode, 0) if err != nil { return err } defer f.Close() readBuf.Reset() if _, err := io.Copy(&readBuf, f); err != nil { return err } src := readBuf.Bytes() if checkShebang && !validShebang.Match(src[:32]) { return nil } prog, err := syntax.Parse(src, path, parseMode) if err != nil { return err } writeBuf.Reset() printConfig.Fprint(&writeBuf, prog) res := writeBuf.Bytes() if !bytes.Equal(src, res) { if *list { fmt.Fprintln(out, path) } if *write { if err := empty(f); err != nil { return err } if _, err := f.Write(res); err != nil { return err } } } if !*list && !*write { if _, err := out.Write(res); err != nil { return err } } return nil }