Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go: update module, various cleanup #18

Merged
merged 7 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: "1.20"
go-version: "1.21"

- name: Build
run: go build -v ./...
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: "1.21"
- uses: actions/setup-python@v3
- uses: pre-commit/action@v3.0.0
10 changes: 6 additions & 4 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (
"fmt"
"io"
"os"
"slices"
katexochen marked this conversation as resolved.
Show resolved Hide resolved
"strconv"
"strings"

"github.com/google/keep-sorted/keepsorted"
flag "github.com/spf13/pflag"
"golang.org/x/exp/maps"
JeffFaer marked this conversation as resolved.
Show resolved Hide resolved
"golang.org/x/exp/slices"
)

type Config struct {
Expand All @@ -41,7 +41,9 @@ func (c *Config) FromFlags(fs *flag.FlagSet) {
}

fs.StringVar(&c.id, "id", "keep-sorted", "The identifier used to enable this tool in files.")
fs.MarkHidden("id")
if err := fs.MarkHidden("id"); err != nil {
panic(err)
}

of := &operationFlag{op: &c.operation}
if err := of.Set("fix"); err != nil {
Expand Down Expand Up @@ -147,7 +149,7 @@ func (f *lineRangeFlag) parse(vals []string) ([]keepsorted.LineRange, error) {
if err != nil {
return nil, fmt.Errorf("invalid line range %q: %w", val, err)
}
end := -1
var end int
if len(sp) == 1 {
end = start
} else {
Expand All @@ -157,7 +159,7 @@ func (f *lineRangeFlag) parse(vals []string) ([]keepsorted.LineRange, error) {
}
}

lrs = append(lrs, keepsorted.LineRange{start, end})
lrs = append(lrs, keepsorted.LineRange{Start: start, End: end})
}
return lrs, nil
}
Expand Down
14 changes: 6 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
module github.com/google/keep-sorted

go 1.20
go 1.21

require (
github.com/Workiva/go-datastructures v1.0.53
github.com/google/go-cmp v0.5.8
github.com/mattn/go-isatty v0.0.14
github.com/rs/zerolog v1.29.0
github.com/mattn/go-isatty v0.0.20
github.com/rs/zerolog v1.31.0
github.com/spf13/pflag v1.0.5
go.uber.org/multierr v1.9.0
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc
google.golang.org/protobuf v1.28.1
)

require (
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/stretchr/testify v1.8.1 // indirect
go.uber.org/atomic v1.7.0 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/sys v0.15.0 // indirect
)
36 changes: 17 additions & 19 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
github.com/Workiva/go-datastructures v1.0.53 h1:J6Y/52yX10Xc5JjXmGtWoSSxs3mZnGSaq37xZZh7Yig=
github.com/Workiva/go-datastructures v1.0.53/go.mod h1:1yZL+zfsztete+ePzZz/Zb1/t5BnDuE2Ya2MMGhzP6A=
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -9,23 +9,24 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
Expand All @@ -34,15 +35,11 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/tinylib/msgp v1.1.5/go.mod h1:eQsjooMTnV42mHu917E26IogZ2930nFyBQdofk10Udg=
github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31/go.mod h1:onvgF043R+lC5RZ8IT9rBXDaEDnpnw/Cl+HFiw+v/7Q=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb h1:PaBZQdo+iSDyHT053FjUCgZQ/9uqVwPOcl7KSWhKn6w=
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc h1:ao2WRsKSzW6KuUY9IWPwWahcHCgR0s52IfwutMfEbdM=
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand All @@ -52,10 +49,11 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
8 changes: 4 additions & 4 deletions goldens/golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package golden_test

import (
"io/ioutil"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestGoldens(t *testing.T) {
if err != nil {
t.Fatalf("Could not open .out file: %v", err)
}
want, err := ioutil.ReadAll(out)
want, err := io.ReadAll(out)
if err != nil {
t.Fatalf("Could not read .out file: %v", err)
}
Expand All @@ -84,13 +84,13 @@ func TestGoldens(t *testing.T) {
t.Errorf("could not start keep-sorted: %v", err)
}

if stderr, err := ioutil.ReadAll(stderr); err != nil {
if stderr, err := io.ReadAll(stderr); err != nil {
t.Errorf("could not read keep-sorted stderr: %v", err)
} else if len(stderr) != 0 {
t.Errorf("keep-sorted stderr: %s", string(stderr))
}

if got, err := ioutil.ReadAll(stdout); err != nil {
if got, err := io.ReadAll(stdout); err != nil {
t.Errorf("could not read keep-sorted stdout: %v", err)
} else if diff := cmp.Diff(strings.Split(string(want), "\n"), strings.Split(string(got), "\n")); diff != "" {
t.Errorf("keep-sorted stdout diff (-want +got):\n%s", diff)
Expand Down
16 changes: 8 additions & 8 deletions keepsorted/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package keepsorted

import (
"cmp"
"fmt"
"slices"
"strings"

"github.com/rs/zerolog/log"
"golang.org/x/exp/constraints"
"golang.org/x/exp/slices"
)

type block struct {
Expand Down Expand Up @@ -248,7 +248,7 @@ func allHaveSuffix(lgs []lineGroup, s string) bool {
return true
}

func (b block) lessFn() func(a, b lineGroup) bool {
func (b block) lessFn() func(a, b lineGroup) int {
// Always put groups that are only comments last.
commentOnlyBlock := comparingProperty(func(lg lineGroup) int {
if len(lg.lines) > 0 {
Expand All @@ -268,8 +268,8 @@ func (b block) lessFn() func(a, b lineGroup) bool {
for i, p := range b.opts.PrefixOrder {
prefixWeights = append(prefixWeights, prefixWeight{p, i - len(b.opts.PrefixOrder)})
}
slices.SortStableFunc(prefixWeights, func(a, b prefixWeight) bool {
return len(a.prefix) > len(b.prefix)
slices.SortStableFunc(prefixWeights, func(a, b prefixWeight) int {
return cmp.Compare(b.prefix, a.prefix)
})

prefixOrder := comparingProperty(func(lg lineGroup) int {
Expand Down Expand Up @@ -310,21 +310,21 @@ func (b block) lessFn() func(a, b lineGroup) bool {
return b.opts.maybeParseNumeric(l)
}, numericTokens.compare)

return func(a, b lineGroup) bool {
return func(a, b lineGroup) int {
for _, cmp := range []func(a, b lineGroup) int{
commentOnlyBlock,
prefixOrder,
transformOrder,
} {
if c := cmp(a, b); c != 0 {
return c < 0
return c
}
}
return a.less(b)
}
}

func comparingProperty[T any, E constraints.Ordered](f func(T) E) func(a, b T) int {
func comparingProperty[T any, E cmp.Ordered](f func(T) E) func(a, b T) int {
return comparingPropertyWith(f, func(a, b E) int {
if a < b {
return -1
Expand Down
7 changes: 4 additions & 3 deletions keepsorted/keep_sorted.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
package keepsorted

import (
"cmp"
"fmt"
"slices"
"strings"

"github.com/Workiva/go-datastructures/augmentedtree"
"golang.org/x/exp/slices"
)

const (
Expand Down Expand Up @@ -147,8 +148,8 @@ func (f *Fixer) findings(filename string, contents []string, modifiedLines []Lin
fs = append(fs, finding(filename, ib.line, ib.line, msg, ""))
}

slices.SortFunc(fs, func(a, b *Finding) bool {
return startLine(a) < startLine(b)
slices.SortFunc(fs, func(a, b *Finding) int {
return cmp.Compare(startLine(a), startLine(b))
})
return fs
}
Expand Down
7 changes: 4 additions & 3 deletions keepsorted/line_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package keepsorted

import (
"cmp"
"fmt"
"regexp"
"strings"
Expand Down Expand Up @@ -265,11 +266,11 @@ func (lg lineGroup) joinedLines() string {
return s.String()
}

func (lg lineGroup) less(other lineGroup) bool {
func (lg lineGroup) less(other lineGroup) int {
if c := strings.Compare(lg.joinedLines(), other.joinedLines()); c != 0 {
return c < 0
return c
}
return strings.Join(lg.comment, "\n") < strings.Join(other.comment, "\n")
return cmp.Compare(strings.Join(lg.comment, "\n"), strings.Join(other.comment, "\n"))
}

func (lg lineGroup) GoString() string {
Expand Down
10 changes: 5 additions & 5 deletions keepsorted/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
package keepsorted

import (
"cmp"
"errors"
"fmt"
"math/big"
"reflect"
"regexp"
"slices"
"strings"
"unicode"

"go.uber.org/multierr"
"golang.org/x/exp/slices"
)

var (
Expand Down Expand Up @@ -99,7 +99,7 @@ func (f *Fixer) parseBlockOptions(startLine string) (blockOptions, error) {

val, err := parseBlockOption(field, startLine)
if err != nil {
errs = multierr.Append(errs, err)
errs = errors.Join(errs, err)
JeffFaer marked this conversation as resolved.
Show resolved Hide resolved
}

opts.Elem().Field(i).Set(val)
Expand All @@ -116,7 +116,7 @@ func (f *Fixer) parseBlockOptions(startLine string) (blockOptions, error) {
}
if len(ret.IgnorePrefixes) > 1 {
// Look at longer prefixes first, in case one of these prefixes is a prefix of another.
slices.SortFunc(ret.IgnorePrefixes, func(a string, b string) bool { return len(a) > len(b) })
slices.SortFunc(ret.IgnorePrefixes, func(a string, b string) int { return cmp.Compare(len(b), len(a)) })
}

return ret, errs
Expand Down