Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strconv"
"strings"

"github.com/aymanbagabas/go-udiff"
"github.com/google/keep-sorted/keepsorted"
"github.com/rs/zerolog/log"
flag "github.com/spf13/pflag"
Expand Down Expand Up @@ -84,6 +85,7 @@ var (
operations = map[string]operation{
"lint": lint,
"fix": fix,
"diff": diff,
}
)

Expand Down Expand Up @@ -269,6 +271,46 @@ func lint(fixer *keepsorted.Fixer, filenames []string, modifiedLines []keepsorte
return false, nil
}

func diff(fixer *keepsorted.Fixer, filenames []string, modifiedLines []keepsorted.LineRange) (ok bool, err error) {
ok = true

for _, fn := range filenames {
contents, err := read(fn)
if err != nil {
return false, err
}
want, alreadyFixed, warnings := fixer.Fix(fn, contents, modifiedLines)
if !alreadyFixed {
ok = false
inName := fn
outName := fn
if fn == stdin {
inName = "stdin"
outName = "stdout"
}
if _, err := os.Stdout.WriteString(udiff.Unified(inName, outName, contents, want)); err != nil {
return false, err
}
}

for _, warn := range warnings {
ok = false
log := log.Warn()
if warn.Path != stdin {
log = log.Str("file", warn.Path)
}
if warn.Lines.Start == warn.Lines.End {
log = log.Int("line", warn.Lines.Start)
} else {
log = log.Int("start", warn.Lines.Start).Int("end", warn.Lines.End)
}
log.Msg(warn.Message)
}
}

return ok, nil
}

func read(fn string) (string, error) {
if fn == stdin {
b, err := io.ReadAll(os.Stdin)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ go 1.23.1

require (
github.com/Workiva/go-datastructures v1.0.53
github.com/aymanbagabas/go-udiff v0.3.1
github.com/google/go-cmp v0.5.8
github.com/mattn/go-isatty v0.0.20
github.com/rs/zerolog v1.31.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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/aymanbagabas/go-udiff v0.3.1 h1:LV+qyBQ2pqe0u42ZsUEtPiCaUoqgA9gYRDs3vj1nolY=
github.com/aymanbagabas/go-udiff v0.3.1/go.mod h1:G0fsKmG+P6ylD0r6N/KgQD/nWzgfnl8ZBcNLgcbrw8E=
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=
Expand Down
295 changes: 295 additions & 0 deletions goldens/block.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
--- stdin
+++ stdout
@@ -1,157 +1,159 @@
widgets := []widget{
// keep-sorted-test start block=yes
{
- Name: "foo",
+ Name: "bar",
},
{
- Name: "bar",
+ Name: "foo",
},
// keep-sorted-test end
}

withStickComments := []widget{
// keep-sorted-test start block=yes sticky_comments=yes
- {
+ // Comment about bar.
+ {
- Name: "foo",
+ Name: "bar",
- SomeOtherField: 123,
+ SomeOtherField: 456,
},
- // Comment about bar.
- {
- Name: "bar",
- SomeOtherField: 456,
- },
- // keep-sorted-test end
-}
-
-strings := []string{
- // Note: since we're sorting based on the entire line, a new line comes before the
- // closing quote and thus a-b-c is sorted before a-b.
- // keep-sorted-test start block=yes
- `a
- c`,
- `a
- b`,
- `a
- b
- c`,
- // keep-sorted-test end
-}
-
-noIncreasedIndentation := []widget{
- // keep-sorted-test start block=yes
- {
- Name: "foo",
- },
- {
- Name: "bar",
- },
- {
- Name: "baz",
- },
- // keep-sorted-test end
-}
-
-unbalancedCommentsDoNotMatter := []widget{
- // keep-sorted-test start block=yes sticky_comments=yes
- // Unbalanced comment: {
- {
- Name: "bar",
- },
- {
- Name: "foo",
- },
- // Also unbalanced: ]
- {
- Name: "baz",
- },
- // keep-sorted-test end
-}
-
-withOneLiners := []widget{
- // keep-sorted-test start block=yes
- {
- Name: "foo",
- },
- {
- Name: "baz",
- },
- {Name: "bar"},
- // keep-sorted-test end
-}
-
-// gcl style code:
-// keep-sorted-test start block=yes
-Group xyz = external_groups.acl_group {
- group_expansion = 'self'
-}
-Group ijk = external_groups.acl_group {
- group_expansion = """'''
- Nested triple quotes aren\'t uncommon
- '''"""
-}
-Group lmn = external_groups.acl_group {
- group_expansion = """
- Including another " shouldn't break groups
- """
-}
-Group abc = external_groups.acl_group {
- group_expansion = "It's the members"
-}
-// keep-sorted-test end
-
-// gcl style code with trailing comments:
-// keep-sorted-test start block=yes
-Experiment xyz = templates.ExperimentBase {
- name = "XYZ" // Should be the last one :(
- description = "A bunch of opening brackets [{[ here"
- percent = 10
-}
-Experiment ikj =
- templates.ExperimentBaseWithTestAccounts {
- name = "IJK"
- description = "A single quote in a trailing comment"
- test_accounts = [
- 123, // Bobby's test account
- ]
-}
-Experiment abc = templates.LAUNCHED {
- name = "ABC"
- description = "Simple block"
-}
-// keep-sorted-test end
-
-// BUILD rule style
-// keep-sorted-test start block=yes newline_separated=yes
-some_build_rule(
- name = "def",
- src = "some-source",
-)
-
-some_build_rule(
- name = "xyz",
- src = "another-source",
-)
-
-some_build_rule(
- name = "abc",
- src = "one-more-source",
+ {
+ Name: "foo",
+ SomeOtherField: 123,
+ },
+ // keep-sorted-test end
+}
+
+strings := []string{
+ // Note: since we're sorting based on the entire line, a new line comes before the
+ // closing quote and thus a-b-c is sorted before a-b.
+ // keep-sorted-test start block=yes
+ `a
+ b
+ c`,
+ `a
+ b`,
+ `a
+ c`,
+ // keep-sorted-test end
+}
+
+noIncreasedIndentation := []widget{
+ // keep-sorted-test start block=yes
+ {
+ Name: "bar",
+ },
+ {
+ Name: "baz",
+ },
+ {
+ Name: "foo",
+ },
+ // keep-sorted-test end
+}
+
+unbalancedCommentsDoNotMatter := []widget{
+ // keep-sorted-test start block=yes sticky_comments=yes
+ // Unbalanced comment: {
+ {
+ Name: "bar",
+ },
+ // Also unbalanced: ]
+ {
+ Name: "baz",
+ },
+ {
+ Name: "foo",
+ },
+ // keep-sorted-test end
+}
+
+withOneLiners := []widget{
+ // keep-sorted-test start block=yes
+ {Name: "bar"},
+ {
+ Name: "baz",
+ },
+ {
+ Name: "foo",
+ },
+ // keep-sorted-test end
+}
+
+// gcl style code:
+// keep-sorted-test start block=yes
+Group abc = external_groups.acl_group {
+ group_expansion = "It's the members"
+}
+Group ijk = external_groups.acl_group {
+ group_expansion = """'''
+ Nested triple quotes aren\'t uncommon
+ '''"""
+}
+Group lmn = external_groups.acl_group {
+ group_expansion = """
+ Including another " shouldn't break groups
+ """
+}
+Group xyz = external_groups.acl_group {
+ group_expansion = 'self'
+}
+// keep-sorted-test end
+
+// gcl style code with trailing comments:
+// keep-sorted-test start block=yes
+Experiment abc = templates.LAUNCHED {
+ name = "ABC"
+ description = "Simple block"
+}
+Experiment ikj =
+ templates.ExperimentBaseWithTestAccounts {
+ name = "IJK"
+ description = "A single quote in a trailing comment"
+ test_accounts = [
+ 123, // Bobby's test account
+ ]
+}
+Experiment xyz = templates.ExperimentBase {
+ name = "XYZ" // Should be the last one :(
+ description = "A bunch of opening brackets [{[ here"
+ percent = 10
+}
+// keep-sorted-test end
+
+// BUILD rule style
+// keep-sorted-test start block=yes newline_separated=yes
+some_build_rule(
+ name = "abc",
+ src = "one-more-source",
+)
+
+some_build_rule(
+ name = "def",
+ src = "some-source",
+)
+
+some_build_rule(
+ name = "xyz",
+ src = "another-source",
)
// keep-sorted-test end

// BUILD rule style with newlines=2
// keep-sorted-test start block=yes newline_separated=2
some_build_rule(
- name = "def",
+ name = "abc",
- src = "some-source",
+ src = "one-more-source",
-)
+)
+

some_build_rule(
- name = "xyz",
+ name = "def",
- src = "another-source",
+ src = "some-source",
-)
+)
+

some_build_rule(
- name = "abc",
+ name = "xyz",
- src = "one-more-source",
+ src = "another-source",
)
// keep-sorted-test end
Loading