Skip to content

Commit

Permalink
remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainStandby committed Jan 3, 2023
1 parent fcd5e9c commit 6b53d0d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 29 deletions.
3 changes: 2 additions & 1 deletion cmd/dev/headers/comments/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func FileContentWithoutHeader(path, token string) (string, error) {
if !knowsFormat {
return text, nil
}
return format.remove(text, token), nil
_, content := format.SplitHeaderFromContent(text, token)
return content, nil
}

func FileContent(path string) (string, error) {
Expand Down
25 changes: 0 additions & 25 deletions cmd/dev/headers/comments/formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,6 @@ type Format struct {
endToken string
}

// removes the comment block in the given format containing the given token from the given text
func (f Format) remove(text string, token string) string {
commentWithToken := f.renderLineStart(token)
inComment := false
result := []string{}
for _, line := range strings.Split(text, "\n") {
if strings.HasPrefix(line, commentWithToken) {
inComment = true
}
if inComment && line == "" {
// the type of comment blocks we remove here is separated by an empty line
// --> empty line marks the end of our comment block
inComment = false
continue
}
if inComment && !strings.HasPrefix(line, f.startToken) {
inComment = false
}
if !inComment {
result = append(result, line)
}
}
return strings.Join(result, "\n")
}

func (f Format) SplitHeaderFromContent(text string, token string) (header, content string) {
commentWithToken := f.renderLineStart(token)
inComment := false
Expand Down
6 changes: 3 additions & 3 deletions cmd/dev/headers/comments/formats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestDoubleSlashCommentsRemove(t *testing.T) {
t.Parallel()
give := "// Copyright © 1997 Ory Corp Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n// another comment\n\nname: test\nhello: world"
want := "// another comment\n\nname: test\nhello: world"
have := doubleSlashComments.remove(give, "Copyright ©")
_, have := doubleSlashComments.SplitHeaderFromContent(give, "Copyright ©")
assert.Equal(t, want, have)
}

Expand Down Expand Up @@ -65,7 +65,7 @@ hello: world`, "\n")
name: test
hello: world`, "\n")
have := poundComments.remove(give, "Copyright ©")
_, have := poundComments.SplitHeaderFromContent(give, "Copyright ©")
assert.Equal(t, want, have)
}

Expand Down Expand Up @@ -114,6 +114,6 @@ hello: world`, "\n")
name: test
hello: world`, "\n")
have := htmlComments.remove(give, "Copyright ©")
_, have := htmlComments.SplitHeaderFromContent(give, "Copyright ©")
assert.Equal(t, want, have)
}

0 comments on commit 6b53d0d

Please sign in to comment.