├── .github └── workflows │ └── main.yml ├── .golangci.yml ├── LICENSE ├── README.md ├── go.mod ├── go.sum ├── internal └── arranger │ ├── arranger.go │ ├── arranger_test.go │ ├── internal │ └── index │ │ ├── index.go │ │ ├── index_test.go │ │ ├── record.go │ │ └── testdata │ │ ├── container │ │ └── ring │ │ │ ├── ring.go │ │ │ └── ring.golden.json │ │ ├── encoding │ │ ├── encoding.go │ │ └── encoding.golden.json │ │ └── io │ │ └── ioutil │ │ ├── ioutil.go │ │ ├── ioutil.golden.json │ │ ├── tempfile.go │ │ └── tempfile.golden.json │ ├── testdata │ ├── container │ │ └── ring │ │ │ ├── ring.go │ │ │ └── ring.golden.go │ ├── encoding │ │ ├── encoding.go │ │ └── encoding.golden.go │ └── io │ │ └── ioutil │ │ ├── ioutil.go │ │ ├── ioutil.golden.go │ │ ├── tempfile.go │ │ └── tempfile.golden.go │ └── util.go ├── main.go └── util.go /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | jobs: 2 | checkout: 3 | runs-on: ubuntu-18.04 4 | steps: 5 | - uses: actions/checkout@v2 6 | - uses: actions/upload-artifact@v2 7 | with: 8 | name: src 9 | path: . 10 | lint: 11 | needs: checkout 12 | runs-on: ubuntu-18.04 13 | steps: 14 | - uses: actions/download-artifact@v2 15 | with: 16 | name: src 17 | - run: curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.26.0 18 | - run: bin/golangci-lint run 19 | test: 20 | needs: checkout 21 | runs-on: ubuntu-18.04 22 | steps: 23 | - uses: actions/download-artifact@v2 24 | with: 25 | name: src 26 | - uses: actions/setup-go@v2 27 | with: 28 | go-version: "1.13" 29 | - run: go test -v ./... 30 | name: main 31 | on: 32 | pull_request: 33 | branches: 34 | - master 35 | -------------------------------------------------------------------------------- /.golangci.yml: -------------------------------------------------------------------------------- 1 | issues: 2 | exclude-use-default: false 3 | 4 | linters: 5 | enable: 6 | - goimports 7 | - golint 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Jasper Deflander 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # goarrange 2 | 3 | Ever wanted a consistent ordering for declarations in your Go code? With `goarrange`, you can automatically follow the 4 | conventions of GoDoc! Constants come first, followed by variables, functions and types with their associated constants, 5 | variables, functions and methods. Within each of these categories, exported declarations precede unexported ones. Lastly 6 | `goarrange` enforces an alphabetic ordering. 7 | 8 | ## Installation 9 | 10 | ```sh 11 | $ go get github.com/jdeflander/goarrange 12 | ``` 13 | 14 | ## Usage 15 | 16 | ```sh 17 | $ goarrange help 18 | Automatic arrangement of Go source code 19 | 20 | Usage: 21 | goarrange help 22 | goarrange run [-d] [-p=] [-r] 23 | 24 | Options: 25 | -d Dry-run listing unarranged files 26 | -p= Path of file or directory to arrange [default: .] 27 | -r Walk directories recursively 28 | ``` 29 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/jdeflander/goarrange 2 | 3 | go 1.13 4 | 5 | require github.com/google/go-cmp v0.4.0 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= 2 | github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 3 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 4 | -------------------------------------------------------------------------------- /internal/arranger/arranger.go: -------------------------------------------------------------------------------- 1 | package arranger 2 | 3 | import ( 4 | "go/ast" 5 | "go/token" 6 | 7 | "github.com/jdeflander/goarrange/internal/arranger/internal/index" 8 | ) 9 | 10 | // Arranger represents a source code arranger for go packages. 11 | type Arranger struct { 12 | index index.Index 13 | set *token.FileSet 14 | } 15 | 16 | // New creates a new arranger for the given package and file set. 17 | // 18 | // The given package must have been parsed with the given file set. 19 | func New(pkg *ast.Package, set *token.FileSet) Arranger { 20 | idx := index.New(pkg) 21 | return Arranger{ 22 | index: idx, 23 | set: set, 24 | } 25 | } 26 | 27 | // Arrange arranges the given file with the given arranger. 28 | // 29 | // The given file must be part of the given arranger's package, and its contents should be represented by the given 30 | // bytes. This method returns an arranged copy of the given contents. 31 | func (a Arranger) Arrange(file *ast.File, src []byte) []byte { 32 | indexes := a.index.Sort(file.Decls) 33 | size := len(src) 34 | dst := make([]byte, size) 35 | dstOffset := 0 36 | mp := ast.NewCommentMap(a.set, file, file.Comments) 37 | srcOffset := 0 38 | 39 | for dstIndex, srcIndex := range indexes { 40 | dstPrefix := dst[dstOffset:] 41 | dstStart, dstEnd := bounds(file.Decls, dstIndex, mp, a.set) 42 | srcPrefix := src[srcOffset:dstStart] 43 | dstOffset += copy(dstPrefix, srcPrefix) 44 | 45 | dstInfix := dst[dstOffset:] 46 | srcStart, srcEnd := bounds(file.Decls, srcIndex, mp, a.set) 47 | srcInfix := src[srcStart:srcEnd] 48 | dstOffset += copy(dstInfix, srcInfix) 49 | 50 | srcOffset = dstEnd 51 | } 52 | 53 | dstSuffix := dst[dstOffset:] 54 | srcSuffix := src[srcOffset:] 55 | copy(dstSuffix, srcSuffix) 56 | return dst 57 | } 58 | 59 | // Arranged checks whether the given file is arranged according to the given arranger. 60 | // 61 | // The given file must be part of the given arranger's package. 62 | func (a Arranger) Arranged(file *ast.File) bool { 63 | return a.index.Sorted(file.Decls) 64 | } 65 | -------------------------------------------------------------------------------- /internal/arranger/arranger_test.go: -------------------------------------------------------------------------------- 1 | package arranger_test 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "go/parser" 7 | "go/token" 8 | "io/ioutil" 9 | "os" 10 | "path/filepath" 11 | "strings" 12 | "testing" 13 | 14 | "github.com/google/go-cmp/cmp" 15 | "github.com/jdeflander/goarrange/internal/arranger" 16 | ) 17 | 18 | func TestArranger(t *testing.T) { 19 | if err := filepath.Walk("testdata", walk); err != nil { 20 | t.Error(err) 21 | } 22 | } 23 | 24 | func notGolden(info os.FileInfo) bool { 25 | name := info.Name() 26 | return !strings.HasSuffix(name, ".golden.go") 27 | } 28 | 29 | func walk(path string, info os.FileInfo, err error) error { 30 | if err != nil { 31 | return fmt.Errorf("failed walking to file at '%s': %w", path, err) 32 | } 33 | if !info.IsDir() { 34 | return nil 35 | } 36 | 37 | set := token.NewFileSet() 38 | packages, err := parser.ParseDir(set, path, notGolden, parser.ParseComments) 39 | if err != nil { 40 | return fmt.Errorf("failed parsing directory at '%s': %v", path, err) 41 | } 42 | 43 | for _, pkg := range packages { 44 | a := arranger.New(pkg, set) 45 | for name, file := range pkg.Files { 46 | bs, err := ioutil.ReadFile(name) 47 | if err != nil { 48 | return fmt.Errorf("failed reading file at '%s': %v", name, err) 49 | } 50 | gotArrange := a.Arrange(file, bs) 51 | golden := fmt.Sprintf("%slden.go", name) 52 | wantArrange, err := ioutil.ReadFile(golden) 53 | if err != nil { 54 | return fmt.Errorf("failed reading golden file at '%s': %v", golden, err) 55 | } 56 | if diff := cmp.Diff(gotArrange, wantArrange); diff != "" { 57 | return fmt.Errorf("arrange output mismatch for file at '%s':\n%s", name, diff) 58 | } 59 | 60 | gotArranged := a.Arranged(file) 61 | wantArranged := bytes.Equal(bs, gotArrange) 62 | if diff := cmp.Diff(gotArranged, wantArranged); diff != "" { 63 | return fmt.Errorf("arranged output mismatch for file at '%s':\n%s", name, diff) 64 | } 65 | } 66 | } 67 | return nil 68 | } 69 | -------------------------------------------------------------------------------- /internal/arranger/internal/index/index.go: -------------------------------------------------------------------------------- 1 | package index 2 | 3 | import ( 4 | "go/ast" 5 | "go/doc" 6 | "sort" 7 | ) 8 | 9 | // Index represents an index of a Go package's arrangeable declarations. 10 | type Index struct { 11 | decls map[ast.Decl]int 12 | } 13 | 14 | // New returns an index for the given Go package. 15 | func New(pkg *ast.Package) Index { 16 | decls := map[ast.Decl]int{} 17 | idx := Index{decls: decls} 18 | p := doc.New(pkg, "", doc.AllDecls|doc.PreserveAST) 19 | 20 | idx.appendValues(p.Consts) 21 | idx.appendValues(p.Vars) 22 | idx.appendFuncs(p.Funcs) 23 | 24 | for _, typ := range p.Types { 25 | idx.append(typ.Decl) 26 | idx.appendValues(typ.Consts) 27 | idx.appendValues(typ.Vars) 28 | idx.appendFuncs(typ.Funcs) 29 | idx.appendFuncs(typ.Methods) 30 | } 31 | return idx 32 | } 33 | 34 | // Sort returns the indices of the given declarations, sorted according to the given index. 35 | func (i Index) Sort(decls []ast.Decl) []int { 36 | records := i.records(decls) 37 | sort.Stable(records) 38 | 39 | var indexes []int 40 | for _, record := range records { 41 | indexes = append(indexes, record.index) 42 | } 43 | return indexes 44 | } 45 | 46 | // Sorted checks whether the given declarations are sorted according to the given index. 47 | func (i Index) Sorted(decls []ast.Decl) bool { 48 | records := i.records(decls) 49 | return sort.IsSorted(records) 50 | } 51 | 52 | func (i Index) append(decl ast.Decl) { 53 | i.decls[decl] = len(i.decls) 54 | } 55 | 56 | func (i Index) appendFuncs(funcs []*doc.Func) { 57 | for _, fun := range funcs { 58 | i.append(fun.Decl) 59 | } 60 | } 61 | 62 | func (i Index) appendValues(values []*doc.Value) { 63 | for _, value := range values { 64 | i.append(value.Decl) 65 | } 66 | } 67 | 68 | func (i Index) records(decls []ast.Decl) records { 69 | var records records 70 | for index, decl := range decls { 71 | key, ok := i.decls[decl] 72 | record := record{ 73 | index: index, 74 | key: key, 75 | ok: ok, 76 | } 77 | records = append(records, record) 78 | } 79 | return records 80 | } 81 | -------------------------------------------------------------------------------- /internal/arranger/internal/index/index_test.go: -------------------------------------------------------------------------------- 1 | package index_test 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "go/parser" 7 | "go/token" 8 | "io/ioutil" 9 | "os" 10 | "path/filepath" 11 | "sort" 12 | "testing" 13 | 14 | "github.com/google/go-cmp/cmp" 15 | "github.com/jdeflander/goarrange/internal/arranger/internal/index" 16 | ) 17 | 18 | func TestIndex(t *testing.T) { 19 | if err := filepath.Walk("testdata", walk); err != nil { 20 | t.Error(err) 21 | } 22 | } 23 | 24 | func walk(path string, info os.FileInfo, err error) error { 25 | if err != nil { 26 | return fmt.Errorf("failed walking to file at '%s': %w", path, err) 27 | } 28 | if !info.IsDir() { 29 | return nil 30 | } 31 | 32 | set := token.NewFileSet() 33 | packages, err := parser.ParseDir(set, path, nil, 0) 34 | if err != nil { 35 | return fmt.Errorf("failed parsing directory at '%s': %v", path, err) 36 | } 37 | 38 | for _, pkg := range packages { 39 | idx := index.New(pkg) 40 | for name, file := range pkg.Files { 41 | gotSort := idx.Sort(file.Decls) 42 | golden := fmt.Sprintf("%slden.json", name) 43 | bytes, err := ioutil.ReadFile(golden) 44 | if err != nil { 45 | return fmt.Errorf("failed reading golden file at '%s': %v", golden, err) 46 | } 47 | var wantSort []int 48 | if err := json.Unmarshal(bytes, &wantSort); err != nil { 49 | return fmt.Errorf("failed unmarshalling golden file at '%s': %v", golden, err) 50 | } 51 | if diff := cmp.Diff(gotSort, wantSort); diff != "" { 52 | return fmt.Errorf("sort mismatch for file at '%s' (-got +want):\n%s", name, diff) 53 | } 54 | 55 | gotSorted := idx.Sorted(file.Decls) 56 | wantSorted := sort.IntsAreSorted(wantSort) 57 | if diff := cmp.Diff(gotSorted, wantSorted); diff != "" { 58 | return fmt.Errorf("sorted mismatch for file at '%s' (-got +want):\n%s", name, diff) 59 | } 60 | } 61 | } 62 | return nil 63 | } 64 | -------------------------------------------------------------------------------- /internal/arranger/internal/index/record.go: -------------------------------------------------------------------------------- 1 | package index 2 | 3 | type record struct { 4 | index int 5 | key int 6 | ok bool 7 | } 8 | 9 | type records []record 10 | 11 | func (rs records) Len() int { 12 | return len(rs) 13 | } 14 | 15 | func (rs records) Less(i, j int) bool { 16 | ri := rs[i] 17 | rj := rs[j] 18 | return ri.ok && rj.ok && ri.key < rj.key 19 | } 20 | 21 | func (rs records) Swap(i, j int) { 22 | rs[i], rs[j] = rs[j], rs[i] 23 | } 24 | -------------------------------------------------------------------------------- /internal/arranger/internal/index/testdata/container/ring/ring.go: -------------------------------------------------------------------------------- 1 | // Copyright 2009 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package ring implements operations on circular lists. 6 | package ring 7 | 8 | // A Ring is an element of a circular list, or ring. 9 | // Rings do not have a beginning or end; a pointer to any ring element 10 | // serves as reference to the entire ring. Empty rings are represented 11 | // as nil Ring pointers. The zero value for a Ring is a one-element 12 | // ring with a nil Value. 13 | // 14 | type Ring struct { 15 | next, prev *Ring 16 | Value interface{} // for use by client; untouched by this library 17 | } 18 | 19 | func (r *Ring) init() *Ring { 20 | r.next = r 21 | r.prev = r 22 | return r 23 | } 24 | 25 | // Next returns the next ring element. r must not be empty. 26 | func (r *Ring) Next() *Ring { 27 | if r.next == nil { 28 | return r.init() 29 | } 30 | return r.next 31 | } 32 | 33 | // Prev returns the previous ring element. r must not be empty. 34 | func (r *Ring) Prev() *Ring { 35 | if r.next == nil { 36 | return r.init() 37 | } 38 | return r.prev 39 | } 40 | 41 | // Move moves n % r.Len() elements backward (n < 0) or forward (n >= 0) 42 | // in the ring and returns that ring element. r must not be empty. 43 | // 44 | func (r *Ring) Move(n int) *Ring { 45 | if r.next == nil { 46 | return r.init() 47 | } 48 | switch { 49 | case n < 0: 50 | for ; n < 0; n++ { 51 | r = r.prev 52 | } 53 | case n > 0: 54 | for ; n > 0; n-- { 55 | r = r.next 56 | } 57 | } 58 | return r 59 | } 60 | 61 | // New creates a ring of n elements. 62 | func New(n int) *Ring { 63 | if n <= 0 { 64 | return nil 65 | } 66 | r := new(Ring) 67 | p := r 68 | for i := 1; i < n; i++ { 69 | p.next = &Ring{prev: p} 70 | p = p.next 71 | } 72 | p.next = r 73 | r.prev = p 74 | return r 75 | } 76 | 77 | // Link connects ring r with ring s such that r.Next() 78 | // becomes s and returns the original value for r.Next(). 79 | // r must not be empty. 80 | // 81 | // If r and s point to the same ring, linking 82 | // them removes the elements between r and s from the ring. 83 | // The removed elements form a subring and the result is a 84 | // reference to that subring (if no elements were removed, 85 | // the result is still the original value for r.Next(), 86 | // and not nil). 87 | // 88 | // If r and s point to different rings, linking 89 | // them creates a single ring with the elements of s inserted 90 | // after r. The result points to the element following the 91 | // last element of s after insertion. 92 | // 93 | func (r *Ring) Link(s *Ring) *Ring { 94 | n := r.Next() 95 | if s != nil { 96 | p := s.Prev() 97 | // Note: Cannot use multiple assignment because 98 | // evaluation order of LHS is not specified. 99 | r.next = s 100 | s.prev = r 101 | n.prev = p 102 | p.next = n 103 | } 104 | return n 105 | } 106 | 107 | // Unlink removes n % r.Len() elements from the ring r, starting 108 | // at r.Next(). If n % r.Len() == 0, r remains unchanged. 109 | // The result is the removed subring. r must not be empty. 110 | // 111 | func (r *Ring) Unlink(n int) *Ring { 112 | if n <= 0 { 113 | return nil 114 | } 115 | return r.Link(r.Move(n + 1)) 116 | } 117 | 118 | // Len computes the number of elements in ring r. 119 | // It executes in time proportional to the number of elements. 120 | // 121 | func (r *Ring) Len() int { 122 | n := 0 123 | if r != nil { 124 | n = 1 125 | for p := r.Next(); p != r; p = p.next { 126 | n++ 127 | } 128 | } 129 | return n 130 | } 131 | 132 | // Do calls function f on each element of the ring, in forward order. 133 | // The behavior of Do is undefined if f changes *r. 134 | func (r *Ring) Do(f func(interface{})) { 135 | if r != nil { 136 | f(r.Value) 137 | for p := r.Next(); p != r; p = p.next { 138 | f(p.Value) 139 | } 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /internal/arranger/internal/index/testdata/container/ring/ring.golden.json: -------------------------------------------------------------------------------- 1 | [ 2 | 0, 3 | 5, 4 | 9, 5 | 8, 6 | 6, 7 | 4, 8 | 2, 9 | 3, 10 | 7, 11 | 1 12 | ] 13 | -------------------------------------------------------------------------------- /internal/arranger/internal/index/testdata/encoding/encoding.go: -------------------------------------------------------------------------------- 1 | // Copyright 2013 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package encoding defines interfaces shared by other packages that 6 | // convert data to and from byte-level and textual representations. 7 | // Packages that check for these interfaces include encoding/gob, 8 | // encoding/json, and encoding/xml. As a result, implementing an 9 | // interface once can make a type useful in multiple encodings. 10 | // Standard types that implement these interfaces include time.Time and net.IP. 11 | // The interfaces come in pairs that produce and consume encoded data. 12 | package encoding 13 | 14 | // BinaryMarshaler is the interface implemented by an object that can 15 | // marshal itself into a binary form. 16 | // 17 | // MarshalBinary encodes the receiver into a binary form and returns the result. 18 | type BinaryMarshaler interface { 19 | MarshalBinary() (data []byte, err error) 20 | } 21 | 22 | // BinaryUnmarshaler is the interface implemented by an object that can 23 | // unmarshal a binary representation of itself. 24 | // 25 | // UnmarshalBinary must be able to decode the form generated by MarshalBinary. 26 | // UnmarshalBinary must copy the data if it wishes to retain the data 27 | // after returning. 28 | type BinaryUnmarshaler interface { 29 | UnmarshalBinary(data []byte) error 30 | } 31 | 32 | // TextMarshaler is the interface implemented by an object that can 33 | // marshal itself into a textual form. 34 | // 35 | // MarshalText encodes the receiver into UTF-8-encoded text and returns the result. 36 | type TextMarshaler interface { 37 | MarshalText() (text []byte, err error) 38 | } 39 | 40 | // TextUnmarshaler is the interface implemented by an object that can 41 | // unmarshal a textual representation of itself. 42 | // 43 | // UnmarshalText must be able to decode the form generated by MarshalText. 44 | // UnmarshalText must copy the text if it wishes to retain the text 45 | // after returning. 46 | type TextUnmarshaler interface { 47 | UnmarshalText(text []byte) error 48 | } 49 | -------------------------------------------------------------------------------- /internal/arranger/internal/index/testdata/encoding/encoding.golden.json: -------------------------------------------------------------------------------- 1 | [ 2 | 0, 3 | 1, 4 | 2, 5 | 3 6 | ] 7 | -------------------------------------------------------------------------------- /internal/arranger/internal/index/testdata/io/ioutil/ioutil.go: -------------------------------------------------------------------------------- 1 | // Copyright 2009 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package ioutil implements some I/O utility functions. 6 | package ioutil 7 | 8 | import ( 9 | "bytes" 10 | "io" 11 | "os" 12 | "sort" 13 | "sync" 14 | ) 15 | 16 | // readAll reads from r until an error or EOF and returns the data it read 17 | // from the internal buffer allocated with a specified capacity. 18 | func readAll(r io.Reader, capacity int64) (b []byte, err error) { 19 | var buf bytes.Buffer 20 | // If the buffer overflows, we will get bytes.ErrTooLarge. 21 | // Return that as an error. Any other panic remains. 22 | defer func() { 23 | e := recover() 24 | if e == nil { 25 | return 26 | } 27 | if panicErr, ok := e.(error); ok && panicErr == bytes.ErrTooLarge { 28 | err = panicErr 29 | } else { 30 | panic(e) 31 | } 32 | }() 33 | if int64(int(capacity)) == capacity { 34 | buf.Grow(int(capacity)) 35 | } 36 | _, err = buf.ReadFrom(r) 37 | return buf.Bytes(), err 38 | } 39 | 40 | // ReadAll reads from r until an error or EOF and returns the data it read. 41 | // A successful call returns err == nil, not err == EOF. Because ReadAll is 42 | // defined to read from src until EOF, it does not treat an EOF from Read 43 | // as an error to be reported. 44 | func ReadAll(r io.Reader) ([]byte, error) { 45 | return readAll(r, bytes.MinRead) 46 | } 47 | 48 | // ReadFile reads the file named by filename and returns the contents. 49 | // A successful call returns err == nil, not err == EOF. Because ReadFile 50 | // reads the whole file, it does not treat an EOF from Read as an error 51 | // to be reported. 52 | func ReadFile(filename string) ([]byte, error) { 53 | f, err := os.Open(filename) 54 | if err != nil { 55 | return nil, err 56 | } 57 | defer f.Close() 58 | // It's a good but not certain bet that FileInfo will tell us exactly how much to 59 | // read, so let's try it but be prepared for the answer to be wrong. 60 | var n int64 = bytes.MinRead 61 | 62 | if fi, err := f.Stat(); err == nil { 63 | // As initial capacity for readAll, use Size + a little extra in case Size 64 | // is zero, and to avoid another allocation after Read has filled the 65 | // buffer. The readAll call will read into its allocated internal buffer 66 | // cheaply. If the size was wrong, we'll either waste some space off the end 67 | // or reallocate as needed, but in the overwhelmingly common case we'll get 68 | // it just right. 69 | if size := fi.Size() + bytes.MinRead; size > n { 70 | n = size 71 | } 72 | } 73 | return readAll(f, n) 74 | } 75 | 76 | // WriteFile writes data to a file named by filename. 77 | // If the file does not exist, WriteFile creates it with permissions perm 78 | // (before umask); otherwise WriteFile truncates it before writing. 79 | func WriteFile(filename string, data []byte, perm os.FileMode) error { 80 | f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) 81 | if err != nil { 82 | return err 83 | } 84 | _, err = f.Write(data) 85 | if err1 := f.Close(); err == nil { 86 | err = err1 87 | } 88 | return err 89 | } 90 | 91 | // ReadDir reads the directory named by dirname and returns 92 | // a list of directory entries sorted by filename. 93 | func ReadDir(dirname string) ([]os.FileInfo, error) { 94 | f, err := os.Open(dirname) 95 | if err != nil { 96 | return nil, err 97 | } 98 | list, err := f.Readdir(-1) 99 | f.Close() 100 | if err != nil { 101 | return nil, err 102 | } 103 | sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() }) 104 | return list, nil 105 | } 106 | 107 | type nopCloser struct { 108 | io.Reader 109 | } 110 | 111 | func (nopCloser) Close() error { return nil } 112 | 113 | // NopCloser returns a ReadCloser with a no-op Close method wrapping 114 | // the provided Reader r. 115 | func NopCloser(r io.Reader) io.ReadCloser { 116 | return nopCloser{r} 117 | } 118 | 119 | type devNull int 120 | 121 | // devNull implements ReaderFrom as an optimization so io.Copy to 122 | // ioutil.Discard can avoid doing unnecessary work. 123 | var _ io.ReaderFrom = devNull(0) 124 | 125 | func (devNull) Write(p []byte) (int, error) { 126 | return len(p), nil 127 | } 128 | 129 | func (devNull) WriteString(s string) (int, error) { 130 | return len(s), nil 131 | } 132 | 133 | var blackHolePool = sync.Pool{ 134 | New: func() interface{} { 135 | b := make([]byte, 8192) 136 | return &b 137 | }, 138 | } 139 | 140 | func (devNull) ReadFrom(r io.Reader) (n int64, err error) { 141 | bufp := blackHolePool.Get().(*[]byte) 142 | readSize := 0 143 | for { 144 | readSize, err = r.Read(*bufp) 145 | n += int64(readSize) 146 | if err != nil { 147 | blackHolePool.Put(bufp) 148 | if err == io.EOF { 149 | return n, nil 150 | } 151 | return 152 | } 153 | } 154 | } 155 | 156 | // Discard is an io.Writer on which all Write calls succeed 157 | // without doing anything. 158 | var Discard io.Writer = devNull(0) 159 | -------------------------------------------------------------------------------- /internal/arranger/internal/index/testdata/io/ioutil/ioutil.golden.json: -------------------------------------------------------------------------------- 1 | [ 2 | 0, 3 | 15, 4 | 10, 5 | 13, 6 | 8, 7 | 2, 8 | 5, 9 | 3, 10 | 4, 11 | 1, 12 | 9, 13 | 14, 14 | 11, 15 | 12, 16 | 6, 17 | 7 18 | ] 19 | -------------------------------------------------------------------------------- /internal/arranger/internal/index/testdata/io/ioutil/tempfile.go: -------------------------------------------------------------------------------- 1 | // Copyright 2010 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package ioutil 6 | 7 | import ( 8 | "os" 9 | "path/filepath" 10 | "strconv" 11 | "strings" 12 | "sync" 13 | "time" 14 | ) 15 | 16 | // Random number state. 17 | // We generate random temporary file names so that there's a good 18 | // chance the file doesn't exist yet - keeps the number of tries in 19 | // TempFile to a minimum. 20 | var rand uint32 21 | var randmu sync.Mutex 22 | 23 | func reseed() uint32 { 24 | return uint32(time.Now().UnixNano() + int64(os.Getpid())) 25 | } 26 | 27 | func nextRandom() string { 28 | randmu.Lock() 29 | r := rand 30 | if r == 0 { 31 | r = reseed() 32 | } 33 | r = r*1664525 + 1013904223 // constants from Numerical Recipes 34 | rand = r 35 | randmu.Unlock() 36 | return strconv.Itoa(int(1e9 + r%1e9))[1:] 37 | } 38 | 39 | // TempFile creates a new temporary file in the directory dir, 40 | // opens the file for reading and writing, and returns the resulting *os.File. 41 | // The filename is generated by taking pattern and adding a random 42 | // string to the end. If pattern includes a "*", the random string 43 | // replaces the last "*". 44 | // If dir is the empty string, TempFile uses the default directory 45 | // for temporary files (see os.TempDir). 46 | // Multiple programs calling TempFile simultaneously 47 | // will not choose the same file. The caller can use f.Name() 48 | // to find the pathname of the file. It is the caller's responsibility 49 | // to remove the file when no longer needed. 50 | func TempFile(dir, pattern string) (f *os.File, err error) { 51 | if dir == "" { 52 | dir = os.TempDir() 53 | } 54 | 55 | prefix, suffix := prefixAndSuffix(pattern) 56 | 57 | nconflict := 0 58 | for i := 0; i < 10000; i++ { 59 | name := filepath.Join(dir, prefix+nextRandom()+suffix) 60 | f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) 61 | if os.IsExist(err) { 62 | if nconflict++; nconflict > 10 { 63 | randmu.Lock() 64 | rand = reseed() 65 | randmu.Unlock() 66 | } 67 | continue 68 | } 69 | break 70 | } 71 | return 72 | } 73 | 74 | // prefixAndSuffix splits pattern by the last wildcard "*", if applicable, 75 | // returning prefix as the part before "*" and suffix as the part after "*". 76 | func prefixAndSuffix(pattern string) (prefix, suffix string) { 77 | if pos := strings.LastIndex(pattern, "*"); pos != -1 { 78 | prefix, suffix = pattern[:pos], pattern[pos+1:] 79 | } else { 80 | prefix = pattern 81 | } 82 | return 83 | } 84 | 85 | // TempDir creates a new temporary directory in the directory dir. 86 | // The directory name is generated by taking pattern and applying a 87 | // random string to the end. If pattern includes a "*", the random string 88 | // replaces the last "*". TempDir returns the name of the new directory. 89 | // If dir is the empty string, TempDir uses the 90 | // default directory for temporary files (see os.TempDir). 91 | // Multiple programs calling TempDir simultaneously 92 | // will not choose the same directory. It is the caller's responsibility 93 | // to remove the directory when no longer needed. 94 | func TempDir(dir, pattern string) (name string, err error) { 95 | if dir == "" { 96 | dir = os.TempDir() 97 | } 98 | 99 | prefix, suffix := prefixAndSuffix(pattern) 100 | 101 | nconflict := 0 102 | for i := 0; i < 10000; i++ { 103 | try := filepath.Join(dir, prefix+nextRandom()+suffix) 104 | err = os.Mkdir(try, 0700) 105 | if os.IsExist(err) { 106 | if nconflict++; nconflict > 10 { 107 | randmu.Lock() 108 | rand = reseed() 109 | randmu.Unlock() 110 | } 111 | continue 112 | } 113 | if os.IsNotExist(err) { 114 | if _, err := os.Stat(dir); os.IsNotExist(err) { 115 | return "", err 116 | } 117 | } 118 | if err == nil { 119 | name = try 120 | } 121 | break 122 | } 123 | return 124 | } 125 | -------------------------------------------------------------------------------- /internal/arranger/internal/index/testdata/io/ioutil/tempfile.golden.json: -------------------------------------------------------------------------------- 1 | [ 2 | 0, 3 | 1, 4 | 2, 5 | 7, 6 | 5, 7 | 4, 8 | 6, 9 | 3 10 | ] 11 | -------------------------------------------------------------------------------- /internal/arranger/testdata/container/ring/ring.go: -------------------------------------------------------------------------------- 1 | // Copyright 2009 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package ring implements operations on circular lists. 6 | package ring 7 | 8 | // A Ring is an element of a circular list, or ring. 9 | // Rings do not have a beginning or end; a pointer to any ring element 10 | // serves as reference to the entire ring. Empty rings are represented 11 | // as nil Ring pointers. The zero value for a Ring is a one-element 12 | // ring with a nil Value. 13 | // 14 | type Ring struct { 15 | next, prev *Ring 16 | Value interface{} // for use by client; untouched by this library 17 | } 18 | 19 | func (r *Ring) init() *Ring { 20 | r.next = r 21 | r.prev = r 22 | return r 23 | } 24 | 25 | // Next returns the next ring element. r must not be empty. 26 | func (r *Ring) Next() *Ring { 27 | if r.next == nil { 28 | return r.init() 29 | } 30 | return r.next 31 | } 32 | 33 | // Prev returns the previous ring element. r must not be empty. 34 | func (r *Ring) Prev() *Ring { 35 | if r.next == nil { 36 | return r.init() 37 | } 38 | return r.prev 39 | } 40 | 41 | // Move moves n % r.Len() elements backward (n < 0) or forward (n >= 0) 42 | // in the ring and returns that ring element. r must not be empty. 43 | // 44 | func (r *Ring) Move(n int) *Ring { 45 | if r.next == nil { 46 | return r.init() 47 | } 48 | switch { 49 | case n < 0: 50 | for ; n < 0; n++ { 51 | r = r.prev 52 | } 53 | case n > 0: 54 | for ; n > 0; n-- { 55 | r = r.next 56 | } 57 | } 58 | return r 59 | } 60 | 61 | // New creates a ring of n elements. 62 | func New(n int) *Ring { 63 | if n <= 0 { 64 | return nil 65 | } 66 | r := new(Ring) 67 | p := r 68 | for i := 1; i < n; i++ { 69 | p.next = &Ring{prev: p} 70 | p = p.next 71 | } 72 | p.next = r 73 | r.prev = p 74 | return r 75 | } 76 | 77 | // Link connects ring r with ring s such that r.Next() 78 | // becomes s and returns the original value for r.Next(). 79 | // r must not be empty. 80 | // 81 | // If r and s point to the same ring, linking 82 | // them removes the elements between r and s from the ring. 83 | // The removed elements form a subring and the result is a 84 | // reference to that subring (if no elements were removed, 85 | // the result is still the original value for r.Next(), 86 | // and not nil). 87 | // 88 | // If r and s point to different rings, linking 89 | // them creates a single ring with the elements of s inserted 90 | // after r. The result points to the element following the 91 | // last element of s after insertion. 92 | // 93 | func (r *Ring) Link(s *Ring) *Ring { 94 | n := r.Next() 95 | if s != nil { 96 | p := s.Prev() 97 | // Note: Cannot use multiple assignment because 98 | // evaluation order of LHS is not specified. 99 | r.next = s 100 | s.prev = r 101 | n.prev = p 102 | p.next = n 103 | } 104 | return n 105 | } 106 | 107 | // Unlink removes n % r.Len() elements from the ring r, starting 108 | // at r.Next(). If n % r.Len() == 0, r remains unchanged. 109 | // The result is the removed subring. r must not be empty. 110 | // 111 | func (r *Ring) Unlink(n int) *Ring { 112 | if n <= 0 { 113 | return nil 114 | } 115 | return r.Link(r.Move(n + 1)) 116 | } 117 | 118 | // Len computes the number of elements in ring r. 119 | // It executes in time proportional to the number of elements. 120 | // 121 | func (r *Ring) Len() int { 122 | n := 0 123 | if r != nil { 124 | n = 1 125 | for p := r.Next(); p != r; p = p.next { 126 | n++ 127 | } 128 | } 129 | return n 130 | } 131 | 132 | // Do calls function f on each element of the ring, in forward order. 133 | // The behavior of Do is undefined if f changes *r. 134 | func (r *Ring) Do(f func(interface{})) { 135 | if r != nil { 136 | f(r.Value) 137 | for p := r.Next(); p != r; p = p.next { 138 | f(p.Value) 139 | } 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /internal/arranger/testdata/container/ring/ring.golden.go: -------------------------------------------------------------------------------- 1 | // Copyright 2009 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package ring implements operations on circular lists. 6 | package ring 7 | 8 | // A Ring is an element of a circular list, or ring. 9 | // Rings do not have a beginning or end; a pointer to any ring element 10 | // serves as reference to the entire ring. Empty rings are represented 11 | // as nil Ring pointers. The zero value for a Ring is a one-element 12 | // ring with a nil Value. 13 | // 14 | type Ring struct { 15 | next, prev *Ring 16 | Value interface{} // for use by client; untouched by this library 17 | } 18 | 19 | // New creates a ring of n elements. 20 | func New(n int) *Ring { 21 | if n <= 0 { 22 | return nil 23 | } 24 | r := new(Ring) 25 | p := r 26 | for i := 1; i < n; i++ { 27 | p.next = &Ring{prev: p} 28 | p = p.next 29 | } 30 | p.next = r 31 | r.prev = p 32 | return r 33 | } 34 | 35 | // Do calls function f on each element of the ring, in forward order. 36 | // The behavior of Do is undefined if f changes *r. 37 | func (r *Ring) Do(f func(interface{})) { 38 | if r != nil { 39 | f(r.Value) 40 | for p := r.Next(); p != r; p = p.next { 41 | f(p.Value) 42 | } 43 | } 44 | } 45 | 46 | // Len computes the number of elements in ring r. 47 | // It executes in time proportional to the number of elements. 48 | // 49 | func (r *Ring) Len() int { 50 | n := 0 51 | if r != nil { 52 | n = 1 53 | for p := r.Next(); p != r; p = p.next { 54 | n++ 55 | } 56 | } 57 | return n 58 | } 59 | 60 | // Link connects ring r with ring s such that r.Next() 61 | // becomes s and returns the original value for r.Next(). 62 | // r must not be empty. 63 | // 64 | // If r and s point to the same ring, linking 65 | // them removes the elements between r and s from the ring. 66 | // The removed elements form a subring and the result is a 67 | // reference to that subring (if no elements were removed, 68 | // the result is still the original value for r.Next(), 69 | // and not nil). 70 | // 71 | // If r and s point to different rings, linking 72 | // them creates a single ring with the elements of s inserted 73 | // after r. The result points to the element following the 74 | // last element of s after insertion. 75 | // 76 | func (r *Ring) Link(s *Ring) *Ring { 77 | n := r.Next() 78 | if s != nil { 79 | p := s.Prev() 80 | // Note: Cannot use multiple assignment because 81 | // evaluation order of LHS is not specified. 82 | r.next = s 83 | s.prev = r 84 | n.prev = p 85 | p.next = n 86 | } 87 | return n 88 | } 89 | 90 | // Move moves n % r.Len() elements backward (n < 0) or forward (n >= 0) 91 | // in the ring and returns that ring element. r must not be empty. 92 | // 93 | func (r *Ring) Move(n int) *Ring { 94 | if r.next == nil { 95 | return r.init() 96 | } 97 | switch { 98 | case n < 0: 99 | for ; n < 0; n++ { 100 | r = r.prev 101 | } 102 | case n > 0: 103 | for ; n > 0; n-- { 104 | r = r.next 105 | } 106 | } 107 | return r 108 | } 109 | 110 | // Next returns the next ring element. r must not be empty. 111 | func (r *Ring) Next() *Ring { 112 | if r.next == nil { 113 | return r.init() 114 | } 115 | return r.next 116 | } 117 | 118 | // Prev returns the previous ring element. r must not be empty. 119 | func (r *Ring) Prev() *Ring { 120 | if r.next == nil { 121 | return r.init() 122 | } 123 | return r.prev 124 | } 125 | 126 | // Unlink removes n % r.Len() elements from the ring r, starting 127 | // at r.Next(). If n % r.Len() == 0, r remains unchanged. 128 | // The result is the removed subring. r must not be empty. 129 | // 130 | func (r *Ring) Unlink(n int) *Ring { 131 | if n <= 0 { 132 | return nil 133 | } 134 | return r.Link(r.Move(n + 1)) 135 | } 136 | 137 | func (r *Ring) init() *Ring { 138 | r.next = r 139 | r.prev = r 140 | return r 141 | } 142 | -------------------------------------------------------------------------------- /internal/arranger/testdata/encoding/encoding.go: -------------------------------------------------------------------------------- 1 | // Copyright 2013 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package encoding defines interfaces shared by other packages that 6 | // convert data to and from byte-level and textual representations. 7 | // Packages that check for these interfaces include encoding/gob, 8 | // encoding/json, and encoding/xml. As a result, implementing an 9 | // interface once can make a type useful in multiple encodings. 10 | // Standard types that implement these interfaces include time.Time and net.IP. 11 | // The interfaces come in pairs that produce and consume encoded data. 12 | package encoding 13 | 14 | // BinaryMarshaler is the interface implemented by an object that can 15 | // marshal itself into a binary form. 16 | // 17 | // MarshalBinary encodes the receiver into a binary form and returns the result. 18 | type BinaryMarshaler interface { 19 | MarshalBinary() (data []byte, err error) 20 | } 21 | 22 | // BinaryUnmarshaler is the interface implemented by an object that can 23 | // unmarshal a binary representation of itself. 24 | // 25 | // UnmarshalBinary must be able to decode the form generated by MarshalBinary. 26 | // UnmarshalBinary must copy the data if it wishes to retain the data 27 | // after returning. 28 | type BinaryUnmarshaler interface { 29 | UnmarshalBinary(data []byte) error 30 | } 31 | 32 | // TextMarshaler is the interface implemented by an object that can 33 | // marshal itself into a textual form. 34 | // 35 | // MarshalText encodes the receiver into UTF-8-encoded text and returns the result. 36 | type TextMarshaler interface { 37 | MarshalText() (text []byte, err error) 38 | } 39 | 40 | // TextUnmarshaler is the interface implemented by an object that can 41 | // unmarshal a textual representation of itself. 42 | // 43 | // UnmarshalText must be able to decode the form generated by MarshalText. 44 | // UnmarshalText must copy the text if it wishes to retain the text 45 | // after returning. 46 | type TextUnmarshaler interface { 47 | UnmarshalText(text []byte) error 48 | } 49 | -------------------------------------------------------------------------------- /internal/arranger/testdata/encoding/encoding.golden.go: -------------------------------------------------------------------------------- 1 | // Copyright 2013 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package encoding defines interfaces shared by other packages that 6 | // convert data to and from byte-level and textual representations. 7 | // Packages that check for these interfaces include encoding/gob, 8 | // encoding/json, and encoding/xml. As a result, implementing an 9 | // interface once can make a type useful in multiple encodings. 10 | // Standard types that implement these interfaces include time.Time and net.IP. 11 | // The interfaces come in pairs that produce and consume encoded data. 12 | package encoding 13 | 14 | // BinaryMarshaler is the interface implemented by an object that can 15 | // marshal itself into a binary form. 16 | // 17 | // MarshalBinary encodes the receiver into a binary form and returns the result. 18 | type BinaryMarshaler interface { 19 | MarshalBinary() (data []byte, err error) 20 | } 21 | 22 | // BinaryUnmarshaler is the interface implemented by an object that can 23 | // unmarshal a binary representation of itself. 24 | // 25 | // UnmarshalBinary must be able to decode the form generated by MarshalBinary. 26 | // UnmarshalBinary must copy the data if it wishes to retain the data 27 | // after returning. 28 | type BinaryUnmarshaler interface { 29 | UnmarshalBinary(data []byte) error 30 | } 31 | 32 | // TextMarshaler is the interface implemented by an object that can 33 | // marshal itself into a textual form. 34 | // 35 | // MarshalText encodes the receiver into UTF-8-encoded text and returns the result. 36 | type TextMarshaler interface { 37 | MarshalText() (text []byte, err error) 38 | } 39 | 40 | // TextUnmarshaler is the interface implemented by an object that can 41 | // unmarshal a textual representation of itself. 42 | // 43 | // UnmarshalText must be able to decode the form generated by MarshalText. 44 | // UnmarshalText must copy the text if it wishes to retain the text 45 | // after returning. 46 | type TextUnmarshaler interface { 47 | UnmarshalText(text []byte) error 48 | } 49 | -------------------------------------------------------------------------------- /internal/arranger/testdata/io/ioutil/ioutil.go: -------------------------------------------------------------------------------- 1 | // Copyright 2009 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package ioutil implements some I/O utility functions. 6 | package ioutil 7 | 8 | import ( 9 | "bytes" 10 | "io" 11 | "os" 12 | "sort" 13 | "sync" 14 | ) 15 | 16 | // readAll reads from r until an error or EOF and returns the data it read 17 | // from the internal buffer allocated with a specified capacity. 18 | func readAll(r io.Reader, capacity int64) (b []byte, err error) { 19 | var buf bytes.Buffer 20 | // If the buffer overflows, we will get bytes.ErrTooLarge. 21 | // Return that as an error. Any other panic remains. 22 | defer func() { 23 | e := recover() 24 | if e == nil { 25 | return 26 | } 27 | if panicErr, ok := e.(error); ok && panicErr == bytes.ErrTooLarge { 28 | err = panicErr 29 | } else { 30 | panic(e) 31 | } 32 | }() 33 | if int64(int(capacity)) == capacity { 34 | buf.Grow(int(capacity)) 35 | } 36 | _, err = buf.ReadFrom(r) 37 | return buf.Bytes(), err 38 | } 39 | 40 | // ReadAll reads from r until an error or EOF and returns the data it read. 41 | // A successful call returns err == nil, not err == EOF. Because ReadAll is 42 | // defined to read from src until EOF, it does not treat an EOF from Read 43 | // as an error to be reported. 44 | func ReadAll(r io.Reader) ([]byte, error) { 45 | return readAll(r, bytes.MinRead) 46 | } 47 | 48 | // ReadFile reads the file named by filename and returns the contents. 49 | // A successful call returns err == nil, not err == EOF. Because ReadFile 50 | // reads the whole file, it does not treat an EOF from Read as an error 51 | // to be reported. 52 | func ReadFile(filename string) ([]byte, error) { 53 | f, err := os.Open(filename) 54 | if err != nil { 55 | return nil, err 56 | } 57 | defer f.Close() 58 | // It's a good but not certain bet that FileInfo will tell us exactly how much to 59 | // read, so let's try it but be prepared for the answer to be wrong. 60 | var n int64 = bytes.MinRead 61 | 62 | if fi, err := f.Stat(); err == nil { 63 | // As initial capacity for readAll, use Size + a little extra in case Size 64 | // is zero, and to avoid another allocation after Read has filled the 65 | // buffer. The readAll call will read into its allocated internal buffer 66 | // cheaply. If the size was wrong, we'll either waste some space off the end 67 | // or reallocate as needed, but in the overwhelmingly common case we'll get 68 | // it just right. 69 | if size := fi.Size() + bytes.MinRead; size > n { 70 | n = size 71 | } 72 | } 73 | return readAll(f, n) 74 | } 75 | 76 | // WriteFile writes data to a file named by filename. 77 | // If the file does not exist, WriteFile creates it with permissions perm 78 | // (before umask); otherwise WriteFile truncates it before writing. 79 | func WriteFile(filename string, data []byte, perm os.FileMode) error { 80 | f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) 81 | if err != nil { 82 | return err 83 | } 84 | _, err = f.Write(data) 85 | if err1 := f.Close(); err == nil { 86 | err = err1 87 | } 88 | return err 89 | } 90 | 91 | // ReadDir reads the directory named by dirname and returns 92 | // a list of directory entries sorted by filename. 93 | func ReadDir(dirname string) ([]os.FileInfo, error) { 94 | f, err := os.Open(dirname) 95 | if err != nil { 96 | return nil, err 97 | } 98 | list, err := f.Readdir(-1) 99 | f.Close() 100 | if err != nil { 101 | return nil, err 102 | } 103 | sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() }) 104 | return list, nil 105 | } 106 | 107 | type nopCloser struct { 108 | io.Reader 109 | } 110 | 111 | func (nopCloser) Close() error { return nil } 112 | 113 | // NopCloser returns a ReadCloser with a no-op Close method wrapping 114 | // the provided Reader r. 115 | func NopCloser(r io.Reader) io.ReadCloser { 116 | return nopCloser{r} 117 | } 118 | 119 | type devNull int 120 | 121 | // devNull implements ReaderFrom as an optimization so io.Copy to 122 | // ioutil.Discard can avoid doing unnecessary work. 123 | var _ io.ReaderFrom = devNull(0) 124 | 125 | func (devNull) Write(p []byte) (int, error) { 126 | return len(p), nil 127 | } 128 | 129 | func (devNull) WriteString(s string) (int, error) { 130 | return len(s), nil 131 | } 132 | 133 | var blackHolePool = sync.Pool{ 134 | New: func() interface{} { 135 | b := make([]byte, 8192) 136 | return &b 137 | }, 138 | } 139 | 140 | func (devNull) ReadFrom(r io.Reader) (n int64, err error) { 141 | bufp := blackHolePool.Get().(*[]byte) 142 | readSize := 0 143 | for { 144 | readSize, err = r.Read(*bufp) 145 | n += int64(readSize) 146 | if err != nil { 147 | blackHolePool.Put(bufp) 148 | if err == io.EOF { 149 | return n, nil 150 | } 151 | return 152 | } 153 | } 154 | } 155 | 156 | // Discard is an io.Writer on which all Write calls succeed 157 | // without doing anything. 158 | var Discard io.Writer = devNull(0) 159 | -------------------------------------------------------------------------------- /internal/arranger/testdata/io/ioutil/ioutil.golden.go: -------------------------------------------------------------------------------- 1 | // Copyright 2009 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package ioutil implements some I/O utility functions. 6 | package ioutil 7 | 8 | import ( 9 | "bytes" 10 | "io" 11 | "os" 12 | "sort" 13 | "sync" 14 | ) 15 | 16 | // Discard is an io.Writer on which all Write calls succeed 17 | // without doing anything. 18 | var Discard io.Writer = devNull(0) 19 | 20 | // devNull implements ReaderFrom as an optimization so io.Copy to 21 | // ioutil.Discard can avoid doing unnecessary work. 22 | var _ io.ReaderFrom = devNull(0) 23 | 24 | var blackHolePool = sync.Pool{ 25 | New: func() interface{} { 26 | b := make([]byte, 8192) 27 | return &b 28 | }, 29 | } 30 | 31 | // NopCloser returns a ReadCloser with a no-op Close method wrapping 32 | // the provided Reader r. 33 | func NopCloser(r io.Reader) io.ReadCloser { 34 | return nopCloser{r} 35 | } 36 | 37 | // ReadAll reads from r until an error or EOF and returns the data it read. 38 | // A successful call returns err == nil, not err == EOF. Because ReadAll is 39 | // defined to read from src until EOF, it does not treat an EOF from Read 40 | // as an error to be reported. 41 | func ReadAll(r io.Reader) ([]byte, error) { 42 | return readAll(r, bytes.MinRead) 43 | } 44 | 45 | // ReadDir reads the directory named by dirname and returns 46 | // a list of directory entries sorted by filename. 47 | func ReadDir(dirname string) ([]os.FileInfo, error) { 48 | f, err := os.Open(dirname) 49 | if err != nil { 50 | return nil, err 51 | } 52 | list, err := f.Readdir(-1) 53 | f.Close() 54 | if err != nil { 55 | return nil, err 56 | } 57 | sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() }) 58 | return list, nil 59 | } 60 | 61 | // ReadFile reads the file named by filename and returns the contents. 62 | // A successful call returns err == nil, not err == EOF. Because ReadFile 63 | // reads the whole file, it does not treat an EOF from Read as an error 64 | // to be reported. 65 | func ReadFile(filename string) ([]byte, error) { 66 | f, err := os.Open(filename) 67 | if err != nil { 68 | return nil, err 69 | } 70 | defer f.Close() 71 | // It's a good but not certain bet that FileInfo will tell us exactly how much to 72 | // read, so let's try it but be prepared for the answer to be wrong. 73 | var n int64 = bytes.MinRead 74 | 75 | if fi, err := f.Stat(); err == nil { 76 | // As initial capacity for readAll, use Size + a little extra in case Size 77 | // is zero, and to avoid another allocation after Read has filled the 78 | // buffer. The readAll call will read into its allocated internal buffer 79 | // cheaply. If the size was wrong, we'll either waste some space off the end 80 | // or reallocate as needed, but in the overwhelmingly common case we'll get 81 | // it just right. 82 | if size := fi.Size() + bytes.MinRead; size > n { 83 | n = size 84 | } 85 | } 86 | return readAll(f, n) 87 | } 88 | 89 | // WriteFile writes data to a file named by filename. 90 | // If the file does not exist, WriteFile creates it with permissions perm 91 | // (before umask); otherwise WriteFile truncates it before writing. 92 | func WriteFile(filename string, data []byte, perm os.FileMode) error { 93 | f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) 94 | if err != nil { 95 | return err 96 | } 97 | _, err = f.Write(data) 98 | if err1 := f.Close(); err == nil { 99 | err = err1 100 | } 101 | return err 102 | } 103 | 104 | // readAll reads from r until an error or EOF and returns the data it read 105 | // from the internal buffer allocated with a specified capacity. 106 | func readAll(r io.Reader, capacity int64) (b []byte, err error) { 107 | var buf bytes.Buffer 108 | // If the buffer overflows, we will get bytes.ErrTooLarge. 109 | // Return that as an error. Any other panic remains. 110 | defer func() { 111 | e := recover() 112 | if e == nil { 113 | return 114 | } 115 | if panicErr, ok := e.(error); ok && panicErr == bytes.ErrTooLarge { 116 | err = panicErr 117 | } else { 118 | panic(e) 119 | } 120 | }() 121 | if int64(int(capacity)) == capacity { 122 | buf.Grow(int(capacity)) 123 | } 124 | _, err = buf.ReadFrom(r) 125 | return buf.Bytes(), err 126 | } 127 | 128 | type devNull int 129 | 130 | func (devNull) ReadFrom(r io.Reader) (n int64, err error) { 131 | bufp := blackHolePool.Get().(*[]byte) 132 | readSize := 0 133 | for { 134 | readSize, err = r.Read(*bufp) 135 | n += int64(readSize) 136 | if err != nil { 137 | blackHolePool.Put(bufp) 138 | if err == io.EOF { 139 | return n, nil 140 | } 141 | return 142 | } 143 | } 144 | } 145 | 146 | func (devNull) Write(p []byte) (int, error) { 147 | return len(p), nil 148 | } 149 | 150 | func (devNull) WriteString(s string) (int, error) { 151 | return len(s), nil 152 | } 153 | 154 | type nopCloser struct { 155 | io.Reader 156 | } 157 | 158 | func (nopCloser) Close() error { return nil } 159 | -------------------------------------------------------------------------------- /internal/arranger/testdata/io/ioutil/tempfile.go: -------------------------------------------------------------------------------- 1 | // Copyright 2010 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package ioutil 6 | 7 | import ( 8 | "os" 9 | "path/filepath" 10 | "strconv" 11 | "strings" 12 | "sync" 13 | "time" 14 | ) 15 | 16 | // Random number state. 17 | // We generate random temporary file names so that there's a good 18 | // chance the file doesn't exist yet - keeps the number of tries in 19 | // TempFile to a minimum. 20 | var rand uint32 21 | var randmu sync.Mutex 22 | 23 | func reseed() uint32 { 24 | return uint32(time.Now().UnixNano() + int64(os.Getpid())) 25 | } 26 | 27 | func nextRandom() string { 28 | randmu.Lock() 29 | r := rand 30 | if r == 0 { 31 | r = reseed() 32 | } 33 | r = r*1664525 + 1013904223 // constants from Numerical Recipes 34 | rand = r 35 | randmu.Unlock() 36 | return strconv.Itoa(int(1e9 + r%1e9))[1:] 37 | } 38 | 39 | // TempFile creates a new temporary file in the directory dir, 40 | // opens the file for reading and writing, and returns the resulting *os.File. 41 | // The filename is generated by taking pattern and adding a random 42 | // string to the end. If pattern includes a "*", the random string 43 | // replaces the last "*". 44 | // If dir is the empty string, TempFile uses the default directory 45 | // for temporary files (see os.TempDir). 46 | // Multiple programs calling TempFile simultaneously 47 | // will not choose the same file. The caller can use f.Name() 48 | // to find the pathname of the file. It is the caller's responsibility 49 | // to remove the file when no longer needed. 50 | func TempFile(dir, pattern string) (f *os.File, err error) { 51 | if dir == "" { 52 | dir = os.TempDir() 53 | } 54 | 55 | prefix, suffix := prefixAndSuffix(pattern) 56 | 57 | nconflict := 0 58 | for i := 0; i < 10000; i++ { 59 | name := filepath.Join(dir, prefix+nextRandom()+suffix) 60 | f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) 61 | if os.IsExist(err) { 62 | if nconflict++; nconflict > 10 { 63 | randmu.Lock() 64 | rand = reseed() 65 | randmu.Unlock() 66 | } 67 | continue 68 | } 69 | break 70 | } 71 | return 72 | } 73 | 74 | // prefixAndSuffix splits pattern by the last wildcard "*", if applicable, 75 | // returning prefix as the part before "*" and suffix as the part after "*". 76 | func prefixAndSuffix(pattern string) (prefix, suffix string) { 77 | if pos := strings.LastIndex(pattern, "*"); pos != -1 { 78 | prefix, suffix = pattern[:pos], pattern[pos+1:] 79 | } else { 80 | prefix = pattern 81 | } 82 | return 83 | } 84 | 85 | // TempDir creates a new temporary directory in the directory dir. 86 | // The directory name is generated by taking pattern and applying a 87 | // random string to the end. If pattern includes a "*", the random string 88 | // replaces the last "*". TempDir returns the name of the new directory. 89 | // If dir is the empty string, TempDir uses the 90 | // default directory for temporary files (see os.TempDir). 91 | // Multiple programs calling TempDir simultaneously 92 | // will not choose the same directory. It is the caller's responsibility 93 | // to remove the directory when no longer needed. 94 | func TempDir(dir, pattern string) (name string, err error) { 95 | if dir == "" { 96 | dir = os.TempDir() 97 | } 98 | 99 | prefix, suffix := prefixAndSuffix(pattern) 100 | 101 | nconflict := 0 102 | for i := 0; i < 10000; i++ { 103 | try := filepath.Join(dir, prefix+nextRandom()+suffix) 104 | err = os.Mkdir(try, 0700) 105 | if os.IsExist(err) { 106 | if nconflict++; nconflict > 10 { 107 | randmu.Lock() 108 | rand = reseed() 109 | randmu.Unlock() 110 | } 111 | continue 112 | } 113 | if os.IsNotExist(err) { 114 | if _, err := os.Stat(dir); os.IsNotExist(err) { 115 | return "", err 116 | } 117 | } 118 | if err == nil { 119 | name = try 120 | } 121 | break 122 | } 123 | return 124 | } 125 | -------------------------------------------------------------------------------- /internal/arranger/testdata/io/ioutil/tempfile.golden.go: -------------------------------------------------------------------------------- 1 | // Copyright 2010 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package ioutil 6 | 7 | import ( 8 | "os" 9 | "path/filepath" 10 | "strconv" 11 | "strings" 12 | "sync" 13 | "time" 14 | ) 15 | 16 | // Random number state. 17 | // We generate random temporary file names so that there's a good 18 | // chance the file doesn't exist yet - keeps the number of tries in 19 | // TempFile to a minimum. 20 | var rand uint32 21 | var randmu sync.Mutex 22 | 23 | // TempDir creates a new temporary directory in the directory dir. 24 | // The directory name is generated by taking pattern and applying a 25 | // random string to the end. If pattern includes a "*", the random string 26 | // replaces the last "*". TempDir returns the name of the new directory. 27 | // If dir is the empty string, TempDir uses the 28 | // default directory for temporary files (see os.TempDir). 29 | // Multiple programs calling TempDir simultaneously 30 | // will not choose the same directory. It is the caller's responsibility 31 | // to remove the directory when no longer needed. 32 | func TempDir(dir, pattern string) (name string, err error) { 33 | if dir == "" { 34 | dir = os.TempDir() 35 | } 36 | 37 | prefix, suffix := prefixAndSuffix(pattern) 38 | 39 | nconflict := 0 40 | for i := 0; i < 10000; i++ { 41 | try := filepath.Join(dir, prefix+nextRandom()+suffix) 42 | err = os.Mkdir(try, 0700) 43 | if os.IsExist(err) { 44 | if nconflict++; nconflict > 10 { 45 | randmu.Lock() 46 | rand = reseed() 47 | randmu.Unlock() 48 | } 49 | continue 50 | } 51 | if os.IsNotExist(err) { 52 | if _, err := os.Stat(dir); os.IsNotExist(err) { 53 | return "", err 54 | } 55 | } 56 | if err == nil { 57 | name = try 58 | } 59 | break 60 | } 61 | return 62 | } 63 | 64 | // TempFile creates a new temporary file in the directory dir, 65 | // opens the file for reading and writing, and returns the resulting *os.File. 66 | // The filename is generated by taking pattern and adding a random 67 | // string to the end. If pattern includes a "*", the random string 68 | // replaces the last "*". 69 | // If dir is the empty string, TempFile uses the default directory 70 | // for temporary files (see os.TempDir). 71 | // Multiple programs calling TempFile simultaneously 72 | // will not choose the same file. The caller can use f.Name() 73 | // to find the pathname of the file. It is the caller's responsibility 74 | // to remove the file when no longer needed. 75 | func TempFile(dir, pattern string) (f *os.File, err error) { 76 | if dir == "" { 77 | dir = os.TempDir() 78 | } 79 | 80 | prefix, suffix := prefixAndSuffix(pattern) 81 | 82 | nconflict := 0 83 | for i := 0; i < 10000; i++ { 84 | name := filepath.Join(dir, prefix+nextRandom()+suffix) 85 | f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) 86 | if os.IsExist(err) { 87 | if nconflict++; nconflict > 10 { 88 | randmu.Lock() 89 | rand = reseed() 90 | randmu.Unlock() 91 | } 92 | continue 93 | } 94 | break 95 | } 96 | return 97 | } 98 | 99 | func nextRandom() string { 100 | randmu.Lock() 101 | r := rand 102 | if r == 0 { 103 | r = reseed() 104 | } 105 | r = r*1664525 + 1013904223 // constants from Numerical Recipes 106 | rand = r 107 | randmu.Unlock() 108 | return strconv.Itoa(int(1e9 + r%1e9))[1:] 109 | } 110 | 111 | // prefixAndSuffix splits pattern by the last wildcard "*", if applicable, 112 | // returning prefix as the part before "*" and suffix as the part after "*". 113 | func prefixAndSuffix(pattern string) (prefix, suffix string) { 114 | if pos := strings.LastIndex(pattern, "*"); pos != -1 { 115 | prefix, suffix = pattern[:pos], pattern[pos+1:] 116 | } else { 117 | prefix = pattern 118 | } 119 | return 120 | } 121 | 122 | func reseed() uint32 { 123 | return uint32(time.Now().UnixNano() + int64(os.Getpid())) 124 | } 125 | -------------------------------------------------------------------------------- /internal/arranger/util.go: -------------------------------------------------------------------------------- 1 | package arranger 2 | 3 | import ( 4 | "go/ast" 5 | "go/token" 6 | ) 7 | 8 | func bounds(decls []ast.Decl, index int, mp ast.CommentMap, set *token.FileSet) (int, int) { 9 | decl := decls[index] 10 | minStart := minStart(decls, index, mp) 11 | start := decl.Pos() 12 | for _, group := range mp.Filter(decl).Comments() { 13 | if group.Pos() > minStart && group.Pos() < start { 14 | start = group.Pos() 15 | } 16 | } 17 | end := end(decl, mp) 18 | return offset(start, set), offset(end, set) 19 | } 20 | 21 | func end(decl ast.Decl, mp ast.CommentMap) token.Pos { 22 | end := decl.End() 23 | for _, group := range mp.Filter(decl).Comments() { 24 | if group.End() > end { 25 | end = group.End() 26 | } 27 | } 28 | return end 29 | } 30 | 31 | func minStart(decls []ast.Decl, index int, mp ast.CommentMap) token.Pos { 32 | if index == 0 { 33 | return token.NoPos 34 | } 35 | decl := decls[index-1] 36 | return end(decl, mp) 37 | } 38 | 39 | func offset(pos token.Pos, set *token.FileSet) int { 40 | position := set.Position(pos) 41 | return position.Offset 42 | } 43 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "io/ioutil" 7 | "log" 8 | "os" 9 | "path/filepath" 10 | ) 11 | 12 | const usage = `Automatic arrangement of Go source code 13 | 14 | Usage: 15 | %[1]s help 16 | %[1]s run [-d] [-p=] [-r] 17 | 18 | Options: 19 | -d Dry-run listing unarranged files 20 | -p= Path of file or directory to arrange [default: .] 21 | -r Walk directories recursively 22 | ` 23 | 24 | func main() { 25 | log.SetFlags(0) 26 | path := os.Args[0] 27 | name := filepath.Base(path) 28 | prefix := fmt.Sprintf("%s: ", name) 29 | log.SetPrefix(prefix) 30 | 31 | if len(os.Args) < 2 { 32 | log.Fatalf("missing command, run '%s help' for usage information", name) 33 | } 34 | 35 | switch os.Args[1] { 36 | case "help": 37 | if _, err := fmt.Printf(usage, name); err != nil { 38 | log.Fatalf("failed printing usage information: %v", err) 39 | } 40 | 41 | case "run": 42 | set := flag.NewFlagSet("", flag.ContinueOnError) 43 | var dryRun bool 44 | set.BoolVar(&dryRun, "d", false, "") 45 | var path string 46 | set.StringVar(&path, "p", ".", "") 47 | var recursive bool 48 | set.BoolVar(&recursive, "r", false, "") 49 | set.SetOutput(ioutil.Discard) 50 | 51 | args := os.Args[2:] 52 | if err := set.Parse(args); err != nil { 53 | log.Fatalf("invalid options, run '%s help' for usage information", name) 54 | } 55 | 56 | directory, filename, err := split(path) 57 | if err != nil { 58 | log.Fatalf("failed splitting path: %v", err) 59 | } 60 | 61 | if filename == "" && recursive { 62 | walk := func(path string, info os.FileInfo, err error) error { 63 | if err != nil { 64 | return fmt.Errorf("failed walking to file at '%s': %w", path, err) 65 | } 66 | if !info.IsDir() { 67 | return nil 68 | } 69 | 70 | if err := arrange(path, "", dryRun); err != nil { 71 | return fmt.Errorf("failed arranging directory at '%s': %w", path, err) 72 | } 73 | return nil 74 | } 75 | if err := filepath.Walk(directory, walk); err != nil { 76 | log.Fatalf("failed walking directory at '%s': %v", directory, err) 77 | } 78 | } else if err := arrange(directory, filename, dryRun); err != nil { 79 | log.Fatalf("failed arranging directory at '%s': %v", directory, err) 80 | } 81 | 82 | default: 83 | log.Fatalf("invalid command, run '%s help' for usage information", name) 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /util.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "go/parser" 6 | "go/token" 7 | "io/ioutil" 8 | "os" 9 | "path/filepath" 10 | 11 | "github.com/jdeflander/goarrange/internal/arranger" 12 | ) 13 | 14 | func arrange(directory, filename string, dryRun bool) error { 15 | set := token.NewFileSet() 16 | packages, err := parser.ParseDir(set, directory, nil, parser.ParseComments) 17 | if err != nil { 18 | return fmt.Errorf("failed parsing directory at '%s': %v", directory, err) 19 | } 20 | 21 | for _, pkg := range packages { 22 | a := arranger.New(pkg, set) 23 | for path, file := range pkg.Files { 24 | if filename != "" && filepath.Base(path) != filename || a.Arranged(file) { 25 | continue 26 | } 27 | 28 | if dryRun { 29 | if _, err := fmt.Println(path); err != nil { 30 | return fmt.Errorf("failed printing path '%s': %w", path, err) 31 | } 32 | } else { 33 | src, err := ioutil.ReadFile(path) 34 | if err != nil { 35 | return fmt.Errorf("failed reading file at '%s': %v", path, err) 36 | } 37 | dst := a.Arrange(file, src) 38 | if err := ioutil.WriteFile(path, dst, 0644); err != nil { 39 | return fmt.Errorf("failed writing file at '%s': %v", path, err) 40 | } 41 | } 42 | } 43 | } 44 | return nil 45 | } 46 | 47 | func split(path string) (string, string, error) { 48 | info, err := os.Stat(path) 49 | if err != nil { 50 | return "", "", fmt.Errorf("failed checking status of file at '%s': %w", path, err) 51 | } 52 | if info.IsDir() { 53 | return path, "", nil 54 | } 55 | dir := filepath.Dir(path) 56 | filename := filepath.Base(path) 57 | return dir, filename, nil 58 | } 59 | --------------------------------------------------------------------------------